Set com.google.android.material.chip.Chip selected color
Just set an attribute app:chipBackgroundColor
and pass a color state list to it:
<android.support.design.chip.Chip
android:id="@+id/test"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checkable="true"
android:clickable="true"
android:focusable="true"
app:chipBackgroundColor="@color/bg_chip_state_list"
app:chipText="Test" />
bg_chip_state_list
looks like this:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/colorSecondaryLight" android:state_checked="true" />
<item android:color="@color/colorPrimaryDark" />
</selector>
However I also had to set android:clickable
to true
to make this work
Using a ColorStateList is a proper way. The only thing I want to add is using custom defined style much more clear to read especially if you want to customise a bunch of properties.
Among other things, one common style applied to all views allows you to make changes in one place that apply immediately to all views
styles.xml
<style name="CustomChipChoice" parent="@style/Widget.MaterialComponents.Chip.Choice">
<item name="chipBackgroundColor">@color/background_color_chip_state_list</item>
<item name="android:textColor">@color/text_color_chip_state_list</item>
</style>
text_color_chip_state_list.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true"
android:color="@color/color_checked" />
<item android:state_checked="false"
android:color="@color/color_unchecked" />
</selector>
background_color_chip_state_list.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/color1" android:state_checked="true" />
<item android:color="@color/color2" />
</selector>
After that all you need is apply your custom style for all the Chip views like this.
<android.support.design.chip.Chip
android:layout_width="wrap_content"
android:layout_height="match_parent"
style="@style/CustomChipChoice"
android:checkable="true"
android:clickable="true"
android:focusable="true"
android:text="Chip text" />