How to add Material Design Chips to input field using autocomplete in android?

Maxi66

I am trying to implement Material Design Chips with AutoCompleteTextView in order to add Contact Chips in an input field when the user clicks on an autocomplete suggestion (like Gmail Recipient Chips).

The desired behaviour can be seen on Material Design Website.

I decided to implement Chips alongside AutoCompleteTextView in my project from scratch, without external libraries. However, I didn't find any guide that shows how to do that.

I tried to create a Standalone ChipDrawable and then add it to AutoCompleteTextView as follows:

Layout

<chip
    xmlns:app="http://schemas.android.com/apk/res-auto"
    app:chipIcon="@drawable/ic_avatar_circle_24"
    android:text="@string/contact_name"/>

Java code

ChipDrawable chip = ChipDrawable.createFromResource(getContext(), R.xml.standalone_chip);

chip.setBounds(0, 0, chip.getIntrinsicWidth(), chip.getIntrinsicHeight());
DrawableMarginSpan span = new DrawableMarginSpan(chip, 25);

Editable text = editText.getText();
text.setSpan(span, 0, text.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

Unfortunately, it doesn't work as expected. First, I can't add more than on Chip. Furthermore, the Chip has a very strange style (height too big, not centered).

So how can I create Contact Chips like in Gmail or SMS apps with Material Design Input Chips? Perhaps someone knows some guide to implement it?

Thanks in advance, your help will be truly appreciated!

Udelabs

If you are looking for a Recipient Edit box just like Gmail Contact Edit box, here is an implementation video that should help you :

How to implement Chips with AutoCompleteTextView for Android

Assuming you have a contact data class, here is how to do that:

MultiAutoCompleteTextView setup

MultiAutoCompleteTextView contactAutoCompleteTextView = findViewById(R.id.recipient_auto_complete_text_view);
List<Contact> contacts = new ArrayList<Contact>() {{
    add(new Contact("Adam Ford", R.drawable.adam_ford));
    add(new Contact("Adele McCormick", R.drawable.adele_mccormick));
    add(new Contact("Alexandra Hollander", R.drawable.alexandra_hollander));
    add(new Contact("Alice Paul", R.drawable.alice_paul));
    add(new Contact("Arthur Roch", R.drawable.arthur_roch));
}};

contactAutoCompleteTextView.setAdapter(new ContactAdapter(this,
        R.layout.contact_layout, contacts));
contactAutoCompleteTextView.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
// Minimum number of characters the user has to type before the drop-down list is shown
contactAutoCompleteTextView.setThreshold(1);
contactAutoCompleteTextView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        Contact selectedContact = (Contact) adapterView.getItemAtPosition(i);
        createRecipientChip(selectedContact);
    }
});

Chip resource

<chip style="@style/Widget.MaterialComponents.Chip.Action"/>

Chip creation

private void createRecipientChip(Contact selectedContact) {
    ChipDrawable chip = ChipDrawable.createFromResource(this, R.xml.standalone_chip);
    CenteredImageSpan span = new CenteredImageSpan(chip, 40f, 40f);
    int cursorPosition = contactAutoCompleteTextView.getSelectionStart();
    int spanLength = selectedContact.getName().length() + 2;
    Editable text = contactAutoCompleteTextView.getText();
    chip.setChipIcon(ContextCompat.getDrawable(MainActivity.this,
        selectedContact.getAvatarResource()));
    chip.setText(selectedContact.getName());
    chip.setBounds(0, 0, chip.getIntrinsicWidth(), chip.getIntrinsicHeight());
    text.setSpan(span, cursorPosition - spanLength, cursorPosition, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
}

CenteredImageSpan is a custom ImageSpan that center the drawable vertically. It also allows us to set Chip padding. The full code is provided in the link.

In this example, contacts can be selected from a list of suggestions as you type. Then, a contact Chip is created (with a name and an avatar) to replace the search query. As for multiple contacts handling, you are looking for the MultiAutoCompleteTextView. It is described in the video.

Hope it helps.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

How to add chips from Material Components library to input field in android?

分類Dev

Material Design Lite * ngIf on Form Input Field Angular 5

分類Dev

How to disallow bad input for an angular material input field

分類Dev

How to use md-icon with md-autocomplete in Angular-Material Design?

分類Dev

How to add notification value for item on NavigationView for Material Design Drawer?

分類Dev

Trying to implement autocomplete Chips with Angular Material. Getting cannot read property 'nextTick' of undefined

分類Dev

Angular material md-chips with md-autocomplete: control.registerOnChange is not a function

分類Dev

How to remove the underline under TextInputLayout in Android Material Design?

分類Dev

How to use the Material Design Theme on pre Android 5.0 Devices

分類Dev

How to start text in input field with lowercase on Android?

分類Dev

Angular 8 material chips. Only allow user to add 1 chip

分類Dev

Angular Material Input and select in one form field

分類Dev

angular material select and input inside the same field

分類Dev

How to add "Android Design Support Library" to Eclipse with ADT-plugin?

分類Dev

Android EditText view Floating Hint in Material Design

分類Dev

Android material design: remove hint animation

分類Dev

kivy floating button android material design style

分類Dev

How to restrict special characters in the input field using Angular 2 / Typescript

分類Dev

How to clear an input field in ionic v3 using typescript

分類Dev

How to get the value of user input field using ReactJS?

分類Dev

How to partition Spark dataset using a field from Kafka input

分類Dev

Using Angular2 With Material Design Lite

分類Dev

Secondary accent color in WPF using Material Design

分類Dev

Secondary accent color in WPF using Material Design

分類Dev

How do you reset/normalize your css using google material design (angular4)?

分類Dev

How to attach a jquery autocomplete to input element

分類Dev

Using Material-UI's Autocomplete component with Formik

分類Dev

how to use own material design icons with materializecss?

分類Dev

How to create a fixed footer in angularjs material design

Related 関連記事

  1. 1

    How to add chips from Material Components library to input field in android?

  2. 2

    Material Design Lite * ngIf on Form Input Field Angular 5

  3. 3

    How to disallow bad input for an angular material input field

  4. 4

    How to use md-icon with md-autocomplete in Angular-Material Design?

  5. 5

    How to add notification value for item on NavigationView for Material Design Drawer?

  6. 6

    Trying to implement autocomplete Chips with Angular Material. Getting cannot read property 'nextTick' of undefined

  7. 7

    Angular material md-chips with md-autocomplete: control.registerOnChange is not a function

  8. 8

    How to remove the underline under TextInputLayout in Android Material Design?

  9. 9

    How to use the Material Design Theme on pre Android 5.0 Devices

  10. 10

    How to start text in input field with lowercase on Android?

  11. 11

    Angular 8 material chips. Only allow user to add 1 chip

  12. 12

    Angular Material Input and select in one form field

  13. 13

    angular material select and input inside the same field

  14. 14

    How to add "Android Design Support Library" to Eclipse with ADT-plugin?

  15. 15

    Android EditText view Floating Hint in Material Design

  16. 16

    Android material design: remove hint animation

  17. 17

    kivy floating button android material design style

  18. 18

    How to restrict special characters in the input field using Angular 2 / Typescript

  19. 19

    How to clear an input field in ionic v3 using typescript

  20. 20

    How to get the value of user input field using ReactJS?

  21. 21

    How to partition Spark dataset using a field from Kafka input

  22. 22

    Using Angular2 With Material Design Lite

  23. 23

    Secondary accent color in WPF using Material Design

  24. 24

    Secondary accent color in WPF using Material Design

  25. 25

    How do you reset/normalize your css using google material design (angular4)?

  26. 26

    How to attach a jquery autocomplete to input element

  27. 27

    Using Material-UI's Autocomplete component with Formik

  28. 28

    how to use own material design icons with materializecss?

  29. 29

    How to create a fixed footer in angularjs material design

ホットタグ

アーカイブ