0

I would like to get the color of my Chip but this way I can't because what I get is ColorStateList.

 colors.children.forEach {
                val chip = it as Chip
                chip.setOnCheckedChangeListener { buttonView, isChecked ->
                    handleSelection(chip)
                }
            }
//...
  private fun handleSelection(chip : Chip) {
    val intColor = chip.chipBackgroundColor?.defaultColor
    val hexColor = "#" + Integer.toHexString(intColor!!).substring(2)

    Log.d("BottomSheetFragment", hexColor)
}

How can I get the current color of my chip ?

Antonio
  • 31
  • 6
  • What type of value do you expect? If you want to get a hex color string then check how to convert color int value to hex https://stackoverflow.com/questions/6539879/how-to-convert-a-color-integer-to-a-hex-string-in-android – sergpetrov Aug 15 '22 at 10:20
  • Also, you don't need to use `ColorStateList.valueOf()` because `chip.chipBackgroundColor` is already ColorStateList type. – sergpetrov Aug 15 '22 at 10:23
  • Thank you all, I have updated the code with the working version – Antonio Aug 15 '22 at 12:05

0 Answers0