Reputation: 3
I want the text color of an item in a ListView to change when selected. I read the following post (How to set ListView selected item alternet text color in android) and made the following changes:
Created a ColorStateList resource @drawable/list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#ffffff00" /> <!-- default -->
<item
android:state_pressed="true"
android:color="#ff000000" />
<item
android:state_selected="true"
android:color="#ffff0000" />
<item
android:state_focused="true"
android:color="#ff0000ff" />
Assigned the resource to textColor attribute in the target TextView
<TextView
android:id="@+id/person_active_list_name"
android:paddingLeft="@dimen/person_list_name_left_padding"
android:layout_width="wrap_content"
android:layout_height="@dimen/person_list_image_size"
android:layout_toEndOf="@id/person_active_thumbnail"
android:layout_toLeftOf="@id/person_active_list_arrow"
android:layout_toRightOf="@id/person_active_thumbnail"
android:layout_toStartOf="@id/person_active_list_arrow"
android:gravity="center|start"
android:textColor="@drawable/list_item" />
For some reason, only the default color in @drawable/list_item.xml is applied (so the ColorStateList resource is indeed being consumed by the TextView). There is no change, however, in text color when a list item is pressed, selected, etc. Any help is appreciated!
Upvotes: 0
Views: 256
Reputation: 54214
From the documentation:
Note: Remember that the first item in the state list that matches the current state of the object will be applied. So if the first item in the list contains none of the state attributes above, then it will be applied every time, which is why your default value should always be last
So, change your definition to this instead:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_pressed="true"
android:color="#ff000000" />
<item
android:state_selected="true"
android:color="#ffff0000" />
<item
android:state_focused="true"
android:color="#ff0000ff" />
<item android:color="#ffffff00" /> <!-- default -->
</selector>
Upvotes: 2