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.
How do I change text color in programmatically? Android TextView – Text Color. TextView Text Color – To change the color of text in TextView, you can set the color in layout XML file using textColor attribute or change the color dynamically in Kotlin file using setTextColor() method.
There are two methods of changing the color of a TextView and the code for that has been given in both Java and Kotlin Programming Language for Android, so it can be done by directly adding a color attribute in the XML code or we can change it through the MainActivity File.
In XML, we can set a text color by the textColor attribute, like android:textColor="#FF0000" .
<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));
<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" />
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With