Giter VIP home page Giter VIP logo

nelzkiecoder / materialchipsinputdemo Goto Github PK

View Code? Open in Web Editor NEW

This project forked from sourin00/materialchipsinputdemo

0.0 0.0 0.0 142 KB

Using the new material library from google to implement Chips just like the Gmail app. Using commas to generate new chips from EditText inside ChipGroup and deleting them on close icon click or backspace press from soft keypad.

License: MIT License

Kotlin 100.00%

materialchipsinputdemo's Introduction

MaterialChipsInputDemo

Using the new material library from google to implement Chips just like the Gmail app. Using commas to generate new chips from EditText inside ChipGroup and deleting them on close icon click or backspace press from soft keypad.

Getting Started

Use this if you want to implement material chips without any 3rd party library dependency just using EditText, Chip, ChipGroup, HorizontalScrollView

Prerequisites

Must have SDK 28 included in dependency either support library or androidX and material. I have used androidX and material

compileSdkVersion 28

implementation 'com.google.android.material:material:1.0.0-rc01'

Implementation

Layout code

    <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/textInputLayout"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginTop="16dp"
        android:layout_marginEnd="16dp"
        android:hint="@string/enter_chip_txt"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <!--Horizontal ScrollView to accomodate chips overflow by making them scroll-->

        <HorizontalScrollView
            android:id="@+id/horizontal_scroll_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:scrollbars="none">

            <androidx.constraintlayout.widget.ConstraintLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                tools:layout_editor_absoluteX="8dp"
                tools:layout_editor_absoluteY="503dp">

                <!--ChipGroup to hold multiple chips and help in removal by getting child elements-->

                <com.google.android.material.chip.ChipGroup
                    android:id="@+id/chipGroup"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    app:layout_constraintBottom_toBottomOf="@+id/textInputEditText"
                    app:layout_constraintEnd_toStartOf="@+id/textInputEditText"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="@+id/textInputEditText" />

                <!--Entering chip generation text and using commas to detect when a new chip is to be generated-->

                <com.google.android.material.textfield.TextInputEditText
                    android:id="@+id/textInputEditText"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:hint="@string/enter_chip_txt"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toEndOf="@+id/chipGroup"
                    app:layout_constraintTop_toTopOf="parent" />

            </androidx.constraintlayout.widget.ConstraintLayout>

        </HorizontalScrollView>
    </com.google.android.material.textfield.TextInputLayout>
    

TextWatcher for adding chips on enter comma

override fun afterTextChanged(s: Editable?) {
                val trimmed = s.toString().trim { it <= ' ' }
                if (trimmed.length > 1 && trimmed.endsWith(",")) {
                    val chip = Chip(this@MainActivity)
                    chip.text = trimmed.substring(0, trimmed.length - 1)
                    chip.isCloseIconVisible = true
                    
                    //Callback fired when chip close icon is clicked 
                    chip.setOnCloseIconClickListener {
                        chipGroup.removeView(chip)
                    }
                    
                    chipGroup.addView(chip)
                    s?.clear()
                }
            }
            

Deletion on backspace press

textInputEditText.setOnKeyListener { _, _, event ->
            if (event != null && event.action == KeyEvent.ACTION_DOWN && event.keyCode == KeyEvent.KEYCODE_DEL) {
                if (textInputEditText.length() == 0 && chipGroup.childCount > 0) {
                    val chip = chipGroup.getChildAt(chipGroup.childCount - 1) as Chip
                    chipGroup.removeView(chip)
                }
            }
            false
        }

Reference

Material Chips

Authors

materialchipsinputdemo's People

Contributors

sourin00 avatar pichayawatfa avatar sourinwb avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.