38

I have the following selector defined in an XML file under res/color/redeemlist_item_color.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="#FFFFFF" /> <!-- pressed -->

      <item android:state_selected="true"
            android:color="#FFFFFF" /> <!-- focused -->

      <item android:color="#000000" /> <!-- default -->

   </selector>

I also have a TextView in a ListView item layout. When I set android:textColor on this TextView to the above selector in XML, then the color changes correctly when the item is selected. However, I am trying to set this resource programmatically in the following way:

holder.label.setTextColor(R.color.redeemlist_item_color);

When set in this way, the color no longer changes. Can a selector be assigned to a TextView in this way?

Vasily Kabunov
  • 6,511
  • 13
  • 49
  • 53
Neil Goodman
  • 1,276
  • 2
  • 16
  • 21

5 Answers5

69

I think you might need to add findViewById or something of that variety


Edit: the above is incorrect as per my comment the proper answer is

setTextColor(getResources().getColorStateList(R.color.redeemlist_item_color));
fehbari
  • 1,429
  • 2
  • 16
  • 22
Rasman
  • 5,349
  • 1
  • 25
  • 38
  • Here is my comment from HD_Mouse: Ok, maybe I left out too much code. I have already made a call to findViewById(). My problem is not a null pointer exception or anything, the view loads fine. This is code that is in a sub-class of BaseAdapter. – Neil Goodman Apr 13 '11 at 13:23
  • 2
    Let me rephrase it: you need to convert R. into a value. I thought the proper function was getViewbyid. I was wrong... try this: setTextColor(getResources().getColor(R.color.redeemlist_item_color)); – Rasman Apr 13 '11 at 21:14
  • 27
    @Rasman : This is not correct, one should use **setTextColor ( getResources ().getColorStateList ( R.color.redeemlist_item_color ) );** – Leeeeeeelo Jan 04 '13 at 13:10
  • but the getResourse() method didn''t get in the CustomAdapter for a listView – Vikky Aug 02 '13 at 06:55
  • @Vikky - it sounds like you need to pass the context to your listview adapter and then you can do `android:textColor="@drawable/selector_listview_text"` – Someone Somewhere Apr 29 '14 at 21:14
  • `Resources#getColorStateList(int)` was deprecated in API level 23. Use either `Resources#getColorStateList(int, Theme)` or `ContextCompat.getColorStateList(Context, int)` instead – Christian García Nov 25 '16 at 10:23
42

You have to use getColorStateList()

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.redeemlist_item_color)).

ffleandro
  • 4,039
  • 4
  • 33
  • 48
  • See http://stackoverflow.com/questions/15543186/colorstatelist-android-programatically for setting the ColorStateList programmatically. – Rock Lee Nov 25 '15 at 12:21
  • `Resources#getColorStateList(int)` was deprecated in API level 23. Use either `Resources#getColorStateList(int, Theme)` or `ContextCompat.getColorStateList(Context, int)` instead – Christian García Nov 25 '16 at 10:23
4

You can try:

holder.label.setTextColor(getResources().getColor(R.color.redeemlist_item_color));

instead of :

holder.label.setTextColor(R.color.redeemlist_item_color);

0

Rasman is correct. You need to give the TextView an ID, android:id="@+/something". You retrieve a reference to that particular using that ID and findViewById, and then you may set the text color.

HD_Mouse
  • 567
  • 1
  • 7
  • 19
  • Ok, maybe I left out too much code. I have already made a call to findViewById(). My problem is not a null pointer exception or anything, the view loads fine. This is code that is in a sub-class of BaseAdapter. – Neil Goodman Apr 13 '11 at 01:13
0
Can use 

context.getColorStateList(colorSelectorId) // if your app minAPI level >= 23
// or 
ContextCompat.getColorStateList(context, colorSelectorId) // for any API


because resources.getColorStateList(id) deprecated: @deprecated Use {@link #getColorStateList(int, Theme)} instead.

Example

textView.setTextColor(context.getColorStateList(R.color.text_color_selector))
textView.setTextColor(ContextCompat.getColorStateList(context, R.color.text_color_selector))

I prefer ContextCompat.getColorStateList as it work for all API

Linh
  • 57,942
  • 23
  • 262
  • 279