I've seen that in android-P google add new material components library which contains material chips:
Material components for android
So I decided to add material input chips to my project, but unfortunately didn't find any tutorial how to make that. I want to create something like Gmail chips but without image on the start.
Because I'm using appcompat library I tried to use material chips by android.support.design.chip.Chip
and android.support.design.chip.ChipGroup
.
But result was just chips without any input field. Also I tried to create a Standalone ChipDrawable and then add it to EditText
using
Editable text = editText.getText();
text.setSpan(span, 0, text.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
But I got empty EditText without any chips. So how can I create chips input like in Gmail using this material components library? Maybe someone has expreience or knows any tutorials where I could see how to create this?
Thanks in advance!
You can use material chip "com.google.android.material.chip.Chip" and "implementation 'com.google.android.material:material:1.0.0' " add in build.gradle
Filter style="@style/Widget.MaterialComponents.Chip.Filter"
Choice Chips style="@style/Widget.MaterialComponents.Chip.Choice"
Entry input: style="@style/Widget.MaterialComponents.Chip.Entry"
Answer
No default input field for adding chips in android. They mentioned input chips but i didn't find any layout or viewgroup for input chips. So i do with
Chipdrawable
method to add chips in edittext. Here am using AppCompatEdittext you can change to anyview which listening the text inputs. Reference.Step 1
Add chip xml resource. chip.xml
Then add textappearance style in style.xml(For change textStyle)
Step 2
Add your view here am using AppCompatEdittext
Step 3
Add this code to your view to get the desired behaviour.
Change chipLength according to your need when new chip need to be added in edittext.
OUTPUT
EDITED
You can find more about how to align center the text with span Here.
Here i added some code from the solution will fix for you..
And change your imagespan class like below
We can do this by using material chips design itself without adding any extra styles.
Add it on app gradle For AndroidX
For earlier than AndroidX use this
Fragment
XML File:
For more reference Click Here