How to set TextColor using setTextColor(ColorsStateList colors)?
Asked Answered
P

3

49

I need to change text color when state change(pressed, focus)...

How to set the text color of a TextView using ColorsStateList?

Pandarus answered 13/7, 2011 at 12:15 Comment(3)
Did you read this? #3506819Hoelscher
Your question is ambiguous- are you simply looking for a tutorial on the use of ColorStateList? Have you searched around at all? #3827916Nominate
Yes, but i cant use XML. I need do this programaticallyPandarus
D
79

If you need to set the colors in code (using ColorStateList), but still want to keep the color states in an XML, you might want to use this:

try {
    XmlResourceParser parser = getResources().getXml(R.color.your_colors);
    ColorStateList colors = ColorStateList.createFromXml(getResources(), parser);
    mText.setTextColor(colors);
} catch (Exception e) {
    // handle exceptions
}

res/color/your_colors.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="#222222"/>
    <item android:state_selected="true"
          android:color="#222222"/>
    <item android:state_focused="true"
          android:color="#222222"/>
    <item android:color="#0000ff"/>
</selector>
Darceldarcey answered 6/8, 2011 at 4:41 Comment(5)
There is a much simpler way to achieve this: mText.setTextColor(getResources().getColorStateList(R.color.your_colors));Gerrygerrymander
@Gerrygerrymander it doesn't seem to retain the color states if you set it that way (at least it wasn't for me). I had to use the method above in order to retain the selected color state, etc.Biopsy
When does it doesn't retain the color states? When an configuration change occurs?Gerrygerrymander
Ol_v_er's method (in the first comment, not in the answer) worked perfectly for me, all the states are retained (Android 2.3 & 4.4).Latinity
Just to update this for 2017, the better way would now be: mText.setTextColor(ContextCompat.getColorStateList(getContext(), R.color.my_color_statelist);Segregate
A
28

You have to use getColorStateList()

I was also struggling with this problem, if you want to use a state list, you need to declared it in the color resources folder, instead of the drawable folder, and use the setTextColor(getResources().getColorStateList(R.color.your_colors)).

Abb answered 30/6, 2013 at 1:36 Comment(1)
This was the solution for me. Android Studio 2.3 was showing very inconsistent behaviour with the selector in the 'drawable' folder. Sometimes it would work, sometimes it would get confused and just colour all the text pink. By adding a 'color' resource folder and putting the selector XML file in there, everything came good.Herzog
H
6

You can also use ContextCompat to load a color state list

ColorStateList colors = ContextCompat.getColorStateList(this,R.color.my_color_list);
Hellenhellene answered 19/12, 2016 at 11:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.