Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Set android:textColor using drawable in Android?

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 ?

like image 595
bytebiscuit Avatar asked Nov 21 '11 15:11

bytebiscuit


4 Answers

color/selector_colors.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:color="@color/white" android:state_checked="true" />
    <item android:color="@color/white" android:state_pressed="true" />
    <item android:color="@color/white" android:state_activated="true" />

    <item android:color="@color/black" />

</selector>

you have to implement it into textview like that:

textview.setTextColor(context.getResources().getColorStateList(R.color.selector_colors));
like image 69
Samet ÖZTOPRAK Avatar answered Nov 03 '22 05:11

Samet ÖZTOPRAK


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));

like image 23
madx Avatar answered Nov 03 '22 05:11

madx


Assuming that by "drawable" you mean a selector with color items, you should refer to this question.

You won't be able to use the textcolor with image drawables, or selectors containing image drawables.

like image 2
alex.zherdev Avatar answered Nov 03 '22 05:11

alex.zherdev


Did you see this, this, or this ?

The last link says to use:

tvImagesTitle.setTextColor( getResources().getColor(R.color.blue) ); 
like image 1
Jack Avatar answered Nov 03 '22 04:11

Jack