Set background color of the material chip programmatically

Ganesh Acharya picture Ganesh Acharya · Nov 20, 2018 · Viewed 10.1k times · Source
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.

Answer

Ravi Kumar picture Ravi Kumar · Nov 20, 2018

You can set background color of material chip by following line (Kotlin)

chip.chipBackgroundColor = getColorStateList(/*your preferred color*/)