Chip chip = new Chip(context);
chip.setBackgroundcolor(getResources().getColor(R.color.blue));
The above line gives the error:
java.lang.UnsupportedOperationException: Do not set the background resource; Chip manages its own background drawable.
Chip chip = new Chip(context); chip. setBackgroundcolor(getResources(). getColor(R. color.
xml which is under the values folder,then you should call the following: root. setBackgroundColor(getResources(). getColor(R.color.name));
So you can use the setChipBackgroundColor(ColorStateList cl) method to set the color of your chip and then you can add an setOnClickListener(new ...) to toggle with selection and non-selection like the following code: yourchip. setOnClickListener(new View.
You can set background color of material chip by following line (Kotlin)
chip.chipBackgroundColor = getColorStateList(/*your preferred color*/)
For Kotlin, you should use this:
chip.chipBackgroundColor = ColorStateList.valueOf(ContextCompat.getColor(context, R.color.yourColor))
For Java:
chip.setChipBackgroundColor(ColorStateList.valueOf(ContextCompat.getColor(context, R.color.yourColor)));
Use the method setChipBackgroundColorResource
:
chip.setChipBackgroundColorResource(R.color.chip_selector_color);
Otherwise use the method setChipBackgroundColor
chip.setChipBackgroundColor(AppCompatResources.getColorStateList(context, R.color.chip_selector_color));
chip.setChipBackgroundColor(getResources().getColorStateList(R.color.Green));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With