1

I am trying to add text color state to Material Chip. It's not even clickable.

layout.xml

<HorizontalScrollView
    android:id="@+id/horizontalScrollView"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <com.google.android.material.chip.ChipGroup
        android:id="@+id/cgServiceList"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:singleLine="true"
        app:singleSelection="true" />
</HorizontalScrollView>

fragment.kt

val chipGroup = view.cgServiceList

for (x in 0..12) {
    val chip = Chip(context, null, R.style.Widget_MaterialComponents_Chip_Choice)

    chip.text = "Nail Care"
    chip.setTextColor(resources.getColorStateList(R.color.txt_chip_state_list, null))
    chip.isClickable = true

    chipGroup.addView(chip)
}

R.color.txt_chip_state_list

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="@color/black" />
    <item android:color="@color/colorPrimary" android:state_checked="true"  />
</selector>
Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
Burak
  • 1,757
  • 3
  • 13
  • 30

3 Answers3

1

Switch the order of the <item> elements in your state list:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="@color/colorPrimary" android:state_checked="true"  />
    <item android:color="@color/black" />
</selector>

The list is parsed from the top down, and the first match is selected. In your version, regardless of the actual state, the first will match (because it defines no states at all).

Ben P.
  • 52,661
  • 6
  • 95
  • 123
  • It's working when I specify on the **layout.xml**. However, it's not working when I try to add the text color state list programmatically. – Burak Jan 13 '20 at 23:03
  • @BurakTaban The issue is in the constructor you are using. You are not applying the correct style and the chip is not checkable. Check the [answer below](https://stackoverflow.com/a/59737472/2016562).In any case the **right order** of the items in a selector is described in this answer. Check also the [official link](https://developer.android.com/guide/topics/resources/color-list-resource). – Gabriele Mariotti Jan 14 '20 at 16:06
0

The issue is here:

val chip = Chip(context, null, R.style.Widget_MaterialComponents_Chip_Choice)

In this way you aren't using the Widget.MaterialComponents.Chip.Choice style since you can't use the constructor val chip = Chip(context, null, R.style.xxxx) to assign a style because the 3rd parameter isn't the style but the attribute in the theme as R.attr.chipStyle.
For more details to change the Chip style programmatically check also this answer.

In your code you are using the default style for a Chip the Widget.MaterialComponents.Chip.Action which provides:

<item name="android:checkable">false</item>

It explains why your Chip is not checkable.
The method chip.setTextColor is correct and works with a selector (but use the selector as described by Ben.P in the other answer).

As you pointed out if you use it in the layout.xml it works since in this case the style declared in the layout is correct (Widget.MaterialComponents.Chip.Choice).

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

Solution :

There several state that you can use, and the default of Chip.Choice is

<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:color="?attr/colorPrimary" android:state_enabled="true" android:state_selected="true"/>
  <item android:color="?attr/colorPrimary" android:state_enabled="true" android:state_checked="true"/>
  <item android:alpha="0.87" android:color="?attr/colorOnSurface" android:state_enabled="true"/>
  <item android:alpha="0.33" android:color="?attr/colorOnSurface"/>
</selector>

So, you can either change your colorPrimary or provide another color list like this by setting android:textColor="@color/color_state_list"

WHY?

Look into <style name="Widget.MaterialComponents.Chip.Choice">

you can see it has <item name="android:textColor">@color/mtrl_choice_chip_text_color</item>

And then look into mtrl_choice_chip_text_color.xml you can found the format I provided above.

Wesely
  • 1,425
  • 14
  • 23