Material-components-android: How to set chip selected color? (single selection)

Created on 28 Jun 2018  路  7Comments  路  Source: material-components/material-components-android

How to set chip selected color? (single selection)

On https://material.io/develop/android/components/chip/, there is only app:chipBackgroundColor, which is when is the color even when it is not selected.

https://stackoverflow.com/questions/51089150/set-com-google-android-material-chip-chip-selected-color

Most helpful comment

I answered on StackOverflow, but I will repeat it here just in case.

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

All 7 comments

I answered on StackOverflow, but I will repeat it here just in case.

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

Now almost the same question, how to do the same with text color?

@jeffreyliu8
in Chip's xml:
android:textAppearance="@style/ChipUncheckedText"

and then in styles.xml

    <style name="ChipUncheckedText" parent="TextAppearance.MaterialComponents.Chip"">
        <item name="android:textColor">@color/colorWhite</item>
    </style>

also, regarding the original question, I also had to set android:checkable="true" to make the color state list work.

where do i make the file bg color state list file?

@pranjal012 You can check the official docs about color state list resources: https://developer.android.com/guide/topics/resources/color-list-resource

Was this page helpful?
0 / 5 - 0 ratings

Related issues

MrCreeper1008 picture MrCreeper1008  路  3Comments

TdevM picture TdevM  路  3Comments

gabrielemariotti picture gabrielemariotti  路  3Comments

gabrielemariotti picture gabrielemariotti  路  3Comments

Sanusy picture Sanusy  路  3Comments