DEV Community

Cover image for How get selected chips text in a chip group
Rohit Jakhar
Rohit Jakhar

Posted on

How get selected chips text in a chip group

While using chip, sometimes you need selecting multiple chip in a chip group.
After that it is very tedious task to get their text in easy way.
I found a way and i am sharing with you
In this code i selected chips inside Alert Dialog

val builder = AlertDialog.Builder(requireContext())
       builder.setTitle("Filter")
       val mView = FilterOptionLayoutBinding.inflate(layoutInflater)
       builder.setView(mView.root)

       builder.setPositiveButton(android.R.string.ok) { d, p ->
           mView.chipGroup.checkedChipIds.forEach {
               val chip = mView.root.findViewById<Chip>(it).text.toString()
               checkedChipsText.add(chip)
           }
           d.dismiss()
       }
       builder.setNegativeButton(android.R.string.cancel) { d, _ ->
           d.cancel()
       }
       builder.create()
       builder.show()
Enter fullscreen mode Exit fullscreen mode

Top comments (0)