Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting Android TextView text color programmatically with setTextColor() renders android:duplicateParentState invalid

I have a listview of simple text items. The TextView in those items looks like this (it's wrapped inside a RelativeLayout):

<TextView
  android:id="@+id/text_language_name"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_alignParentLeft="true"
  android:layout_centerVertical="true"
  android:duplicateParentState="true"
  android:textSize="16sp"
  android:textStyle="bold" />

I want to use the following color state selector for the text, which I have called "dark_list_text_states.xml":

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_pressed="true" android:color="@android:color/white"/>
    <item android:color="@color/dark_list_text"/>

</selector>

Normally, of course, I could just set that to the android:textColor property in the xml, but in this case, I need to set one particular item programmatically to a different color, using this selector ("blue_text_states.xml"):

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_pressed="true" android:color="@android:color/white"/>
    <item android:color="@color/selected_blue"/>

</selector>

So I set the text color in the Adapter (language_name and selected are defined earlier in the code):

        TextView text_language_name = (TextView)view.findViewById(R.id.text_language_name);
        if (text_language_name != null) {
            text_language_name.setText(language_name);
            int text_color = selected
                           ? getResources().getColor(R.color.blue_text_states)
                           : getResources().getColor(R.color.dark_list_text_states);
            text_language_name.setTextColor(text_color);
            text_language_name.setDuplicateParentStateEnabled(true);
        }

And this works fine, except for one problem: the text color doesn't change when the item is clicked. Even though I call setDuplicateParentStateEnabled(true) on the item right after setTextColor(), that setting is completely ignored.

I have tested this by commenting out the Java code that sets the text color and just setting it in the xml, and the selector works just fine. But if I set textColor programmatically, it is completely incapable of duplicating the parent state.

Has anyone seen this before? Is there a workaround, or is this just an Android bug I have to live with?

like image 279
Russell Stewart Avatar asked Apr 18 '13 23:04

Russell Stewart


1 Answers

OK, found my mistake. I should be using getResources().getColorStateList() instead of getResources().getColor(). Leaving this here for anyone else who makes the same mistake.

like image 99
Russell Stewart Avatar answered Oct 14 '22 09:10

Russell Stewart