Change Selection in a ListView from Orange to Green

Viewed 52230

How do I do this per selected list item.

I tried adding this to android:background

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_focused="true" 
        android:state_pressed="false"
        android:drawable="@color/android_green" />
    <item android:state_focused="true" 
        android:state_pressed="true"
        android:drawable="@color/black_alpha" />
    <item android:state_focused="false" 
        android:state_pressed="true"
        android:drawable="@color/black_alpha" />
    <item android:drawable="@color/white_alpha" />
</selector> 

but it does not work, it changes the entire ListView.


Still not working, here is what I have so far

::listview_background

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_focused="true" android:drawable="@color/black_alpha" />
    <item android:drawable="@color/white_alpha" />  
</selector> 

::My view that is using the above

<ListView android:id="@+id/list" android:layout_width="fill_parent"
    android:layout_height="fill_parent" android:layout_below="@+id/round"
    android:listSelector="@drawable/listview_background">
</ListView>

The problem is that the black_alpha will apply the the entire list, not just the selected list item

5 Answers

Here's a good article on how to use selectors with lists.

Instead of setting it to be the android:background of the ListView, I believe you want to set android:listSelector as shown below:

<ListView android:id="@+id/list" 
      android:layout_width="fill_parent"
      android:layout_height="wrap_content" 
      android:layout_gravity="center"
      android:divider="@null" 
      android:dividerHeight="0dip"
      android:listSelector="@drawable/list_selector" />
Related