20
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.
Edric
  • 24,639
  • 13
  • 81
  • 91
Ganesh Acharya
  • 303
  • 1
  • 2
  • 7

6 Answers6

30

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)));
Mertcan Seğmen
  • 903
  • 2
  • 11
  • 26
SobaDeveloper
  • 1,197
  • 10
  • 7
28

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

chip.chipBackgroundColor = getColorStateList(/*your preferred color*/)
Ravi Kumar
  • 324
  • 3
  • 4
8

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));
Ljdawson
  • 12,091
  • 11
  • 45
  • 60
Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
6
  • Try this:
chip.setChipBackgroundColor(getResources().getColorStateList(R.color.Green));
Xenolion
  • 12,035
  • 7
  • 33
  • 48
  • 1
    This method is deprecated, please use `chip.setChipBackgroundColor(ColorStateList.valueOf(ContextCompat.getColor(context, R.color.white)));` instead – Amitoj Dec 05 '21 at 16:04
1

use backgroundTint in xml

            <com.google.android.material.chip.Chip
                android:id="@+id/timetext_id"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginHorizontal="40dp"
                android:layout_weight="1"
                android:backgroundTint="#39b8db"
                android:gravity="center"
                android:padding="5dp"
                android:text="Time"
                android:textAlignment="center"
                android:textColor="@color/white" />
Niaj Mahmud
  • 399
  • 3
  • 10
0

In Kotlin

chip.chipBackgroundColor = ContextCompat.getColorStateList(this.context,R.color.**yourcolorid**)

Narendra_Nath
  • 4,578
  • 3
  • 13
  • 31