How to define ColorStateList for TextView?
Asked Answered
U

3

32

When my ListViewItem is highlighted, I want the text to turn white. How can I define this?

<selector xmlns:android="http://schemas.android.com/apk/res/android">
   <item android:state_focused="true" android:color="@color/testcolor1"/>
   <item android:state_pressed="true" android:state_enabled="false" android:color="@color/testcolor2" />
   <item android:state_enabled="false" android:color="@color/testcolor3" />
   <item android:color="@color/testcolor5"/>
 </selector>
Ubiquitous answered 30/9, 2010 at 3:9 Comment(3)
I'm not sure I understand what you mean?Ubiquitous
Why did you posted the code? What it shows? (how do you use the drawable?)Idealist
Well I'm not sure how to change the textcolor. I assumed it would be in the selector code, but it's probably not. So I'm hoping someone can answer the question because I don't know.Ubiquitous
I
69

Create file res/drawable/text_color.xml:

 <selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_focused="true" android:state_pressed="false" android:color="#ffffff" />
    <item android:state_focused="true" android:state_pressed="true" android:color="#ffffff" />
    <item android:state_focused="false" android:state_pressed="true" android:color="#ffffff" />
    <item android:color="#000000" />
</selector>

Then use @drawable/text_color from xml (or R.drawable.text_color from code) as text color for your list view items.

Idealist answered 30/9, 2010 at 6:36 Comment(6)
Thanks! can you show me an example of what a @drawable/text_color might look like?Ubiquitous
Somewhat like android:textColor="@drawable/text_color" on a TextView.Idealist
that should go in res/colorConspiracy
yes, and be referred to as @color/text_color. A @color can point to either a color or a ColorStateList.Melantha
If you set the text colour programmatically, call Context.getResources().getColorStateList(R.drawable.text_color) to get a ColorStateList, and pass that into the EditText.setTextColor method that takes a ColorStateList. Don't just pass the resource id in as an int (like I did...duh!).Rede
@KonstantinBurov How to define custom state attributes instead of frameworks's android:state_*?Girder
V
8

Try this...

First, create a color state list text_color.xml placed in res/color directory.

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"
    tools:ignore="MissingDefaultResource">
  <item android:color="#000000" android:state_enabled="false"/>
  <item android:color="#FFFFFF"/>
</selector>

Second, use

getColorStateList(@NonNull Context context,
            @ColorRes int id)

method to get color state list.

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

Third, enable(true) or disable(false) based on your requirements,

textView.isEnabled = true //when item is highlighted
Verbify answered 11/2, 2019 at 13:48 Comment(0)
H
3

In addition to what others have stated above, I would like to highlight one point, taken from the below url.

https://developer.android.com/reference/android/content/res/ColorStateList.html

Note: The list of state specs will be matched against in the order that they appear in the XML file. For this reason, more-specific items should be placed earlier in the file. An item with no state spec is considered to match any set of states and is generally useful as a final item to be used as a default.

It's important that you have the broader condition towards the bottom in the selector tag.

Highline answered 19/9, 2016 at 23:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.