Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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" />

</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.

like image 824
Rafael Carrillo Avatar asked Apr 02 '12 18:04

Rafael Carrillo


People also ask

How do I change text color in programmatically?

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.

How do I add color to TextView?

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.

What attribute changes the color of TextView?

In XML, we can set a text color by the textColor attribute, like android:textColor="#FF0000" .


3 Answers

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" />
like image 112
Sagar Shah Avatar answered Oct 11 '22 17:10

Sagar Shah


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.

like image 21
ffleandro Avatar answered Oct 11 '22 16:10

ffleandro


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

like image 30
ρяσѕρєя K Avatar answered Oct 11 '22 16:10

ρяσѕρєя K