Skip to content

Using a ColorStateList does not update color on state changed #5

@adamsp

Description

@adamsp

The color of the text does not update when used with a ColorStateList:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:color="@color/gray_6"/>
    <item android:state_selected="true" android:color="@color/gray_6"/>
    <item android:state_focused="true" android:color="@color/gray_6"/>
    <item android:color="@color/highlight_blue"/>
</selector>
textDrawable.setTextColor(getResources.getColorStateList(R.color.highlight_text));

This can be fixed by invalidating the view on state changed:

@Override
protected boolean onStateChange(int[] state) {
    // Upon state changes, grab the correct text color
    if (updateTextColors(state)) {
        invalidateSelf();
        return true;
    }
    return false;
}

I've tested this on 4.0.4, 4.4.2 and 6.0.1. Happy to send a PR if you'd like.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions