I have a clickable TextView that I want to give some colors to. But I don\'t know how. Here are the relevant code snippets from my two files that I\'m working with:
Try this one.. It worked for me:
File name: res/color/bg_tab_text_color.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="false" android:state_selected="false" android:color="@color/tab_unselected_text_color"/>
<item android:state_pressed="true" android:color="@color/tab_selected_text_color"/>
<item android:state_pressed="false" android:state_selected="true" android:color="@color/tab_selected_text_color"/>
<item android:color="@color/tab_unselected_text_color"></item>
</selector>
Try setting the color in xml layout as:
android:textColor="@color/bg_tab_text_color"
Look in R.java class (it's generated automatically). You have something like that:
public static final class color {
public static final int gray_transparent=0x7f050001;
}
So in your code in line:
title.setTextColor(R.color.textcolor);
you're not setting values from textcolor.xml but int from R.java (which contains textcolor.xml address). The valid way to set color is:
title.setTextColor(getResources().getColorStateList(R.color.textcolor));