8
I have a listview of simple text items. The TextView in those items looks like this (it's wrapped inside a RelativeLayout):
<TextView
android:id="@+id/text_language_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:duplicateParentState="true"
android:textSize="16sp"
android:textStyle="bold" />
I want to use the following color state selector for the text, which I have called "dark_list_text_states.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="@android:color/white"/>
<item android:color="@color/dark_list_text"/>
</selector>
Normally, of course, I could just set that to the android:textColor property in the xml, but in this case, I need to set one particular item programmatically to a different color, using this selector ("blue_text_states.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="@android:color/white"/>
<item android:color="@color/selected_blue"/>
</selector>
So I set the text color in the Adapter (language_name and selected are defined earlier in the code):
TextView text_language_name = (TextView)view.findViewById(R.id.text_language_name);
if (text_language_name != null) {
text_language_name.setText(language_name);
int text_color = selected
? getResources().getColor(R.color.blue_text_states)
: getResources().getColor(R.color.dark_list_text_states);