Setting TextView color to a <selector> programmatically
Asked Answered
I

5

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?

Illfated answered 12/4, 2011 at 23:40 Comment(0)
C
70

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));
Cassiodorus answered 13/4, 2011 at 0:19 Comment(6)
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.Illfated
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));Cassiodorus
@Cassiodorus : This is not correct, one should use setTextColor ( getResources ().getColorStateList ( R.color.redeemlist_item_color ) );Companionable
but the getResourse() method didn''t get in the CustomAdapter for a listViewEvildoer
@Evildoer - it sounds like you need to pass the context to your listview adapter and then you can do android:textColor="@drawable/selector_listview_text"Herbivorous
Resources#getColorStateList(int) was deprecated in API level 23. Use either Resources#getColorStateList(int, Theme) or ContextCompat.getColorStateList(Context, int) insteadMalliemallin
A
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)).

Azucenaazure answered 30/6, 2013 at 1:37 Comment(2)
See #15543686 for setting the ColorStateList programmatically.Steamheated
Resources#getColorStateList(int) was deprecated in API level 23. Use either Resources#getColorStateList(int, Theme) or ContextCompat.getColorStateList(Context, int) insteadMalliemallin
I
4

You can try:

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

instead of :

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

Imprudent answered 13/7, 2011 at 3:57 Comment(1)
This is not correct, one should you the getColorStateList method and not getColor.Companionable
D
1
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

Decato answered 17/8, 2023 at 8:30 Comment(0)
C
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.

Celka answered 13/4, 2011 at 0:23 Comment(1)
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.Illfated

© 2022 - 2024 — McMap. All rights reserved.