Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set TextColor using setTextColor(ColorsStateList colors)?

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

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

like image 662
Rodrigo Avatar asked Jul 13 '11 12:07

Rodrigo


People also ask

How do I change the color of my text messages on Android?

Open your device's Settings app . Text and display. Select Color correction. Turn on Use color correction.

How do I change text color in XML?

In XML, we can set a text color by the textColor attribute, like android:textColor="#FF0000" .

How do I change the color of my text on Kotlin?

Change Text Color of TextView in Kotlin File We can get the reference to TextView widget present in layout file and change the color dynamically with Kotlin code. To set the color to the TextView widget, call setTextColor() method on the TextView widget reference with specific color passed as argument.


1 Answers

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> 
like image 170
imbrizi Avatar answered Sep 20 '22 20:09

imbrizi