I have a list of simple text elements. The TextView in these elements looks like this (it is wrapped inside 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 text that I named "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>
Usually, of course, I could just set this property android:textColorin xml, but in this case I need to set one specific element 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 color of the text in the Adapter ( language_nameand selecteddefined 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);
text_language_name.setTextColor(text_color);
text_language_name.setDuplicateParentStateEnabled(true);
}
, : . , setDuplicateParentStateEnabled(true) setTextColor(), .
, Java, xml, . textColor , .
- ? , Android, ?