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.
Here is my xml;
`
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:
/**
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.
Most helpful comment
Wouldn't it be nice if the API exposed it in a more direct way?