How to Set android:textColor using drawable in Android?

bytebiscuit picture bytebiscuit · Nov 21, 2011 · Viewed 16.3k times · Source

I know how to set a drawable as the color of the text using xml but I don't know how it is done in Java.

In xml is something like this:

android:textColor="@drawable/selected_color"

in JAVA ?

Answer

madx picture madx · Jan 31, 2015

Assuming that by "drawable" you mean a selector with color items like this:

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_checked="true" android:color="#ffffffff"/>
    <item android:color="#ff1c5fab"/>
</selector>

You can use this code: mText.setTextColor(getResources().getColorStateList(R.color.your_colors));