textdrawable icon indicating copy to clipboard operation
textdrawable copied to clipboard

Using a ColorStateList does not update color on state changed

Open adamsp opened this issue 9 years ago • 0 comments

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.

adamsp avatar Sep 12 '16 15:09 adamsp