How to add a Chip (Filter type) dynamically (via code) ? (Kotlin)

Asked

Viewed 38 times

0

I need to add Chips Filter type (image below) dynamically

Attempt 1

I tried to create the Chip in a totally dynamic way:

    fun onAddChip(name: String){
        val chip = Chip(this)

        chip.TYPE = Filter //Eu esperava que tivesse como fazer isso
        chip.text = name
        chip.setTextColor(Color.WHITE)
        chip.setChipBackgroundColorResource(R.color.marsala)
        chip.chipCornerRadius = 32F

        servicos_container_chip_group.addView(chip)
    }

But as you can see in the comment on line 4, there is no way to set the Chip type as filter.

Attempt 2

I found here a way to inflate a pre-defined layout, the problem is that I don’t understand what that would be view that it inserts into the second parameter. I imagine it to be a chip_group, so I added did as follows:

val chip = layoutInflater.inflate(R.layout.chip_layout, view!!.parent.parent as ViewGroup, false) as Chip

and it didn’t work either. It makes a mistake.

Does anyone have any suggestions?

PS. I hope Maniero does not put my question as out of context, again!

inserir a descrição da imagem aqui

1 answer

0

I found a solution that worked legal!

Follows the Link

Browser other questions tagged

You are not signed in. Login or sign up in order to post.