rafuru rafuru - 1 year ago 155
Android Question

Using selector to change TextView text color

I'm trying to use a TextView to define the style of a TabWidget on a tabhost.

I just created a selector for bgcolor and works fine, but i want to make a selector for textColor but the text color don't change:

This is my tab_text_selector.xml:

<?xml version="1.0" encoding="utf-8"?>
<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" />


And this is the code when i'm trying to use on a textView:

TextView txtTab=new TextView(this);
txtTab.setText("Agregar Idea");

I know the text color must be white in any case but it doesn't.

Answer Source

You have to use getColorStateList()

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download