kotlin – How can I add the new android chips dynamically in Android?-ThrowExceptions

Exception or error:

I have a class Question which contains a String array of tags. I will try to show every question in a recyclerview using Kotlin and every tag in the new chip. This chips, will be included inside chipGroup.

My question is.

How can I add every tag element of the array into a new chip?
I’m trying to do this but It’s obviously not working.

if (tags != null) {
    for (tag in tags) {
        val chip = Chip(itemView.context)

Thanks for all!

How to solve:

You can add Chips 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

for singleSelection don’t forget to add to your chipGroup:

chipGroup.isSingleSelection = true

or in xml


Good Luck and Happy Coding!


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:

Leave a Reply

Your email address will not be published. Required fields are marked *