How can I add the new android chips dynamically in Android?
You can add Chip
s the same way as any other ViewGroup
like so:
for (index in tags.indices) {
val chip = Chip(chipGroup.context)
chip.text= "Item ${tags[index]}"
// necessary to get single selection working
chip.isClickable = true
chip.isCheckable = true
chipGroup.addView(chip)
}
for singleSelection don't forget to add to your chipGroup:
chipGroup.isSingleSelection = true
or in xml
app:singleSelection="true"
I always got the following error when trying to create a new Chip:
IllegalArgumentException: This component requires that you specify a valid android:textAppearance attribute
This could be fixed by instead inflating a custom R.layout.chip
with the following line:
android:textAppearance="@style/TextAppearance.MaterialComponents.Chip"