Using selector to change TextView text color

You have to use getColorStateList(). And for xml, see here.

I was also struggling with this problem. If you want to have use a state list, you need to declare it in the color resources folder, instead of the drawable folder, and use the setTextColor(getResources().getColorStateList(R.color.tab_text_selector)) method.


1) Use tab_text_selector.xml as below and put it into res/color folder:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_selected="true" android:color="@android:color/white" />
    <item android:state_focused="true" android:color="@android:color/white" />
    <item android:state_pressed="true" android:color="@android:color/white" />
    <item android:color="#504f4f" /> <!-- default case -->
</selector>

And set it to your textview as below..

TextView tv = (TextView) findViewById(R.id.TextView1) ;
tv.setTextColor(context.getResources().getColor(R.color.tab_text_selector));

2) The Second option is If you are using textview in xml rather than using programatically then use tab_text_selector.xml as below :

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal"
    android:text="TextView"
    android:textColor="@‌​drawable/tab_text_sel‌​ector" />