Material-components-android: How to get selected chips from a chipgroup?

Created on 16 Sep 2018  路  5Comments  路  Source: material-components/material-components-android

I have a chip group for post tags. The chip group contains chips for tags, example: "Announcement", "Meeting", "Question". The problem is I can't find a way to get checked chips from the chip group. A user, can select multiple chips in a one post. But according to the docs,

Call setOnCheckedChangeListener(OnCheckedChangeListener) to register a callback to be invoked when the checked chip changes in this group. This callback is only invoked in single selection mode.

Most helpful comment

Wouldn't it be nice if the API exposed it in a more direct way?

All 5 comments

Here is my xml;

` android:id="@+id/chipGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:singleLine="true"
android:layout_marginTop="@dimen/margin_small"
android:layout_marginBottom="@dimen/margin_small">

            <com.google.android.material.chip.Chip
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                style="@style/Widget.MaterialComponents.Chip.Entry"
                app:closeIconEnabled="false"
                android:textColor="@color/colorWhite"
                app:chipBackgroundColor="@color/light_blue_500"
                android:text="Announcement"/>

            <com.google.android.material.chip.Chip
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                style="@style/Widget.MaterialComponents.Chip.Entry"
                app:closeIconEnabled="false"
                android:textColor="@color/colorWhite"
                app:chipBackgroundColor="@color/pink_500"
                android:text="Meeting"/>

            <com.google.android.material.chip.Chip
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                style="@style/Widget.MaterialComponents.Chip.Entry"
                app:closeIconEnabled="false"
                android:textColor="@color/colorWhite"
                app:chipBackgroundColor="@color/lime_500"
                android:text="General"/>

            <com.google.android.material.chip.Chip
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                style="@style/Widget.MaterialComponents.Chip.Entry"
                app:closeIconEnabled="false"
                android:textColor="@color/colorWhite"
                app:chipBackgroundColor="@color/indigo_500"
                android:text="Feedback"/>

            <com.google.android.material.chip.Chip
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                style="@style/Widget.MaterialComponents.Chip.Entry"
                app:closeIconEnabled="false"
                android:textColor="@color/colorWhite"
                app:chipBackgroundColor="@color/blue_grey_500"
                android:text="Question"/>
        </com.google.android.material.chip.ChipGroup>`

private fun getSelectedText(chipGroup: ChipGroup, id: Int): String {
val mySelection = chipGroup.findViewById(id)
return mySelection?.text?.toString() ?: ""
}

You can use this val mySelection = chipGroup.findViewById<Chip>(id) to get the selectedChip and the complete fuction will look like

filterChips.setOnCheckedChangeListener { chipGroup, i ->
getSelectedText(chipGroup, i)
}

You should be able to iterate through the children of the ChipGroup and check chip.isChecked().

Wouldn't it be nice if the API exposed it in a more direct way?

To be honest it is not clear why we should have this method (which works only in a single selection mode):

```
@IdRes
public int getCheckedChipId() {
return singleSelection ? checkedId : View.NO_ID;
}

and we don't have a method like:

/**

  • Returns the identifiers of the selected {@link Chip}s in this group. Upon empty
  • selection, the returned value is an empty list.
    *
  • @return The unique IDs of the selected {@link Chip}s in this group. When in {@link
  • #isSingleSelection() single selection mode}, returns a list with a single ID. When no
  • {@link Chip}s are selected, returns an empty list.
    */
    @NonNull
    public List getCheckedChipIds() {
    ArrayList checkedIds = new ArrayList<>();
    for (int i = 0; i < getChildCount(); i++) {
    View child = getChildAt(i);
    if (child instanceof Chip) {
    if (((Chip) child).isChecked()) {
    checkedIds.add(child.getId());
    }
    }
    }
return checkedIds;

}
```

I could make a PR but before that I would like to understand if there are some reasons to avoid this kind of method.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

JavierSegoviaCordoba picture JavierSegoviaCordoba  路  3Comments

aarontwf picture aarontwf  路  3Comments

Sanusy picture Sanusy  路  3Comments

sepehr-alipour picture sepehr-alipour  路  3Comments

mnayef95 picture mnayef95  路  3Comments