55

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.

Rafael Carrillo
  • 2,772
  • 9
  • 43
  • 64

11 Answers11

107

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" />
Komal12
  • 3,340
  • 4
  • 16
  • 25
Sagar Shah
  • 4,272
  • 2
  • 25
  • 36
  • 15
    android:textColor="@drawable/tab_text_selector" to be exact – BAZTED May 13 '13 at 14:11
  • 6
    getColor will not work - you need to use getColorStateList() as mentioned in the answer by @ffeandro. Verified on 4.4.2 – Ben Clayton Jul 02 '14 at 17:08
  • 8
    @BAZTED did you even try `android:textColor="@drawable/tab_text_selector"` to change text color? By actually doing some code you would realize that `android:textColor="@color/tab_text_selector"` is actually the correct answer. I have no clue how anyone would upvote your comment when its misleading. – Andy Aug 13 '17 at 22:35
  • 1
    @Andy It depends where you put the `tab_text_selector.xml` file. In my experience these go in the `res/drawable` folder, but this answer says to put it in `res/color` which apparently works, but is a bit unusual. – k2col Jul 19 '18 at 17:15
62

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.

Dara
  • 21
  • 1
  • 3
ffleandro
  • 4,039
  • 4
  • 33
  • 48
  • 1
    I went ahead and made an edit to include the xml version. This answer was the most helpful out of all the other answers on changing the textcolor of a `TextView` based on the selector state. Literally all of them say `android:color` in the drawable selector. I wonder if any of them actually tried it out before answering thinking it was right. – Andy Aug 13 '17 at 22:29
30

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

spaaarky21
  • 6,524
  • 7
  • 52
  • 65
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
  • 6
    Is there a reason you are setting the TextView's `textColor` programmatically? Why not just add `android:textColor="@drawable/tab_text_selector"` to the layout? – spaaarky21 Jun 20 '13 at 20:02
  • 2
    Android Studio (and I assume Eclipse) will complain that getXml() is expecting an XML resource and throw a red underline error, but you can still compile and run your app just fine. If this is bugging you, move the selector XML file from R.drawable or R.color wherever you have it to R.xml and your IDE should stop complaining. – micnguyen Nov 26 '14 at 03:03
10

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" />
Shabbir Dhangot
  • 8,954
  • 10
  • 58
  • 80
7

@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);
SBotirov
  • 13,872
  • 7
  • 59
  • 81
2
<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" />
Shabbir Dhangot
  • 8,954
  • 10
  • 58
  • 80
Samir Mangroliya
  • 39,918
  • 16
  • 117
  • 134
1

Here is reference on android developers http://developer.android.com/guide/topics/resources/color-list-resource.html

snapix
  • 344
  • 2
  • 8
1

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.

Rahul
  • 3,293
  • 2
  • 31
  • 43
0

Easiest and effective solution:

  1. create your Xml color selector into res/color/your_color.xml
  2. Use ContextCompat util

Ex.: txtview.setTextColor( ContextCompat.getColorStateList(context, R.color.tab_tv_selector) );

Rahul
  • 3,293
  • 2
  • 31
  • 43
0

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 the color 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 with android:textColor="@color/text_selector"
Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98
0

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.

Shabbir Dhangot
  • 8,954
  • 10
  • 58
  • 80
Arpit Garg
  • 8,476
  • 6
  • 34
  • 59