0

I need to dynamically change the background color of the Android Material chips container (ChipGroup) component. I tried setBackground() and setBackgroundTint(). I also tried android:background and android:backgroundTint properties in XML (code below) just to check. But they are not working either. Any idea how to do this?

<com.google.android.material.chip.ChipGroup
    android:id="@+id/chips"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="0dp"
    android:background="@color/tw__transparent"
    android:backgroundTint="@color/tw__transparent"
    app:ensureMinTouchTargetSize="false"
    app:chipSpacing="2dp"/>
Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
Kulakshi
  • 11
  • 1
  • 5

3 Answers3

1

try this in java file.

// Defined in colors.xml - #ca8eed
chip.setChipBackgroundColorResource(R.color.chip)

And if you want to change textColor, you can use style

in styles.xml file,

<resources>
   ...
    <style name="ChipTextStyle">
        <item name="android:textSize">18sp</item>
        <item name="android:textColor">#2b90ff</item>
    </style>
  ...

and add style in layout xml file.

S T
  • 1,068
  • 2
  • 8
  • 16
  • I think Kulakshi asked to change ChipGroup background color not the chip background color – Shalu T D Jul 03 '20 at 06:50
  • oh. sorry. then just cover other layout and set background color in covered layout is easy way. I think – S T Jul 03 '20 at 06:52
0

The ChipGroup is just a ViewGroup. You can use :

    <com.google.android.material.chip.ChipGroup
        android:background="@color/...."

or you can use the setBackgroundColor method:

chipGroup.setBackgroundColor(ContextCompat.getColor(this,R.color...));

enter image description here

Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
0

You can set a layout (Linear or Relative) including the chips and set the background color to that layout, easy way.

KalanaChinthaka
  • 323
  • 4
  • 13