Using selector to change TextView text color
AndroidAndroid WidgetAndroid Problem Overview
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" />
</selector>
And this is the code when i'm trying to use on a textView:
TextView txtTab=new TextView(this);
txtTab.setTextColor(R.drawable.tab_text_selector);
txtTab.setBackgroundResource(R.drawable.tab_bg_selector);
txtTab.setGravity(Gravity.CENTER);
txtTab.setText("Agregar Idea");
I know the text color must be white in any case but it doesn't.
Android Solutions
Solution 1 - Android
- 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_selector" />
Solution 2 - Android
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.
Solution 3 - Android
Use this way:
tab_text_selector.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="#FF111111"/>
<item android:state_focused="true" android:color="#FF222222"/>
<item android:state_selected="true" android:color="#FF333333"/>
</selector>
TextView:
TextView txtTab = new TextView(this);
XmlResourceParser xrp = getResources().getXml(R.drawable.tab_text_selector);
try {
ColorStateList csl = ColorStateList.createFromXml(getResources(), xrp);
txtTab.setTextColor(csl);
} catch (Exception e) { }
txtTab.setBackgroundResource(R.drawable.tab_bg_selector);
txtTab.setGravity(Gravity.CENTER);
txtTab.setText("Agregar Idea");
But it's better to put color in /res/color/yourcolor.xml
Solution 4 - Android
Just make Selector for textcolor
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:color="@color/drawer_color" />
<item android:state_focused="false" android:color="@android:color/white" />
<item android:state_selected="true" android:color="@color/drawer_color" />
Then set textColor property of Textview an set clickable=true
<TextView
android:clickable="true"
android:textColor="@drawable/text_selector"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/registration"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
Solution 5 - Android
@ffleandro,@ρяσѕρєя K answers best variant, I think using ColorStateList best choice to older and latest versions of Android.
int[][] states = new int[][] {
new int[] { android.R.attr.state_pressed}, // pressed
new int[] { android.R.attr.state_focused}, // focused
new int[] {}
};
int[] colors = new int[] {
getResources().getColor(R.color.green_color), // green
getResources().getColor(R.color.green_color), // green
getResources().getColor(R.color.white) // white
};
ColorStateList list = new ColorStateList(states, colors);
mTextView.setFocusable(true);
mTextView.setClickable(true);
mTextView.setTextColor(list);
Solution 6 - Android
<item android:state_selected="true" android:color="@android:color/white" />
<item android:state_focused="true" android:color="@android:color/red" />
<item android:state_pressed="true" android:color="@android:color/blue" />
Solution 7 - Android
Here is reference on android developers http://developer.android.com/guide/topics/resources/color-list-resource.html
Solution 8 - Android
Create a selector (text_color_selector.xml) and put it into res/color folder:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/colorPrimary" android:state_pressed="true" /> <!-- pressed -->
<item android:color="@color/colorPrimary" android:state_focused="true" /> <!-- focused -->
<item android:color="@color/light_gray_b" /> <!-- default -->
</selector>
add this into your Textview as:
<TextView
android:id="@+id/tvMenuName"
style="@style/TextViewStyle"
android:layout_marginLeft="@dimen/dimen_15"
android:layout_marginStart="@dimen/dimen_15"
android:textColor="@color/text_color_selector"
android:textSize="@dimen/text_size_16"
tools:text="Home" />
programmatically you can set Textview like this:
TextView tv = (TextView) findViewById(R.id.textView) ;
tv.setTextColor(context.getResources().getColor(R.color.text_color_selector));
Hope it helps.
Solution 9 - Android
>You have used the white color in all cases focus , selected and pressed..
Please use and test with different color.
Also must use a default case with certain color say black along with the all case.. when no state is used default will be applied.
Solution 10 - Android
Easiest and effective solution:
- create your Xml color selector into res/color/your_color.xml
- Use ContextCompat util
Ex.: txtview.setTextColor( ContextCompat.getColorStateList(context, R.color.tab_tv_selector) );
Solution 11 - Android
I had a different problem. I tried with everything which is suggested in this thread including the following
- Moving the selector from the
drawable
folder to thecolor
folder. - Setting the
android:clickable="true"
- Setting the
android:duplicateParentState="true"
None of these seems to be working!
However, finally, I had to clean the project and got the selector working on my device. Thought putting this experience as an answer will help other developers.
Hence, the final working version had the following.
- The selector file in the
/res/color
folder - And was added in the
TextView
withandroid:textColor="@color/text_selector"