Drawable selector not working in Jelly Bean

Viewed 8730

I have a drawable selector as a background for each item in a ListView to highlight the selected row. Eveything works fine in Ice Cream Sandwich, but doesn't seem to work in Jelly Bean. Can't find any documentation saying what changes could have caused it to stop working and what I need to do to fix it.

By not working, I mean when I click on a row in the ListView the item's background color isn't turning the @color/blue color, but it does in ICS.

This is the selector code I'm using (listing_selector.xml):

<selector xmlns:android="http://schemas.android.com/apk/res/android" >

   <item android:state_focused="true" android:drawable="@color/blue" />

   <item android:state_pressed="true" android:drawable="@color/blue" />

   <item android:state_activated="true" android:drawable="@color/blue_selected" />  

   <item android:state_selected="true" android:drawable="@color/blue_selected" />

   <item android:drawable="@android:color/transparent" />

</selector>

This is the layout of the ListView item:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"  
    android:layout_height="fill_parent" 
    android:orientation="horizontal"
    android:background="@color/listing_selector"    
>

    <TextView 
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true" 
    />

</RelativeLayout>

This the blue color resource:

<resources>
    <color name="blue">#ff33b5e5</color>       
</resources>

UPDATE 1:

Tried moving the selector from the color folder to the drawable folder and updating the code to this:

android:background="@drawable/listing_selector"

UPDATE 2:

Also, on the ListView, tried adding this:

    <ListView android:id="@android:id/list"
        android:layout_width="wrap_content"  
        android:layout_height="fill_parent" 
        android:listSelector="@drawable/listing_selector"   
    />

UPDATE 3:

I thought it might be something in my code, but I removed all the code from onListItemClick of the ListView and still the selector isn't working.

UPDATE 4:

I've narrowed it down to state_selected or state_activated not working as, state_pressed seems to be working

UPDATE 5:

I think I was mistaken. I don't think the selector is being recognized, at all. I was confusing the built-in ListView highlighting as my selector. I'm now wondering if it has something to do with the way my project is setup. I have the selector in a Library Class. Maybe something changed with that from ICS to JB, however moving the selector to my app's project didn't seem to fix it.

UPDATE 6:

Ok, after some more hair pulling, I've narrowed it down, again, to either state_selected or state_activated not being recognized, as changing the color for state_pressed does work, which means my selector is being recognized. From the comments in seems to be something with my app specifically as others have been able to get selectors working with Jelly Bean.

Though something else that is interesting is that changing the drawable value for the default state is not recognized. Where I have color/transparent, I would think changing that to a color would cause the listing to change to that color, but it doesn't.

Also, this isn't working in ICS either.

UPDATE 7:

After even more hair pulling, I've discovered that long-pressing on a menu item results in that item's color being changed. Just clicking on an item still does not work. Not even sure what the means.

** Final Update:**

I give up, I removed the selector and am just refreshing the ListView on click and remembering the position clicked and highlighting it from code. Not ideal, but not worth the effort to try to fix.

6 Answers

This is a very old question but I am still supporting Jellybean and I've noticed for background selectors to work I need to do something in particular. If I have a viewgroup with a background drawable and I need selector colors on it I need to do things in this order:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/my_drawable_button"/>

my_drawable_button must now reference a selector:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/primary_background_pressed"
        android:state_pressed="true" />
    <item android:drawable="@drawable/primary_background"
        android:state_enabled="true" />
    <item android:drawable="@drawable/primary_background_disabled"
        android:state_enabled="false" />
</selector>

primary_background.xml:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <corners android:radius="8dp" />
    <solid android:color="@color/secondary" />
</shape>

The others are the same with different colors. This is an awful lot of code to do something very basic, but I have found on Jellybean it's the only solution.

Future API levels allow you to specify the color selector on the drawable solid itself, eliminating 4 files:

post api 21:

<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="@color/purple">
    <item>
        <shape android:shape="rectangle">
            <corners android:radius="8dp" />
            <solid android:color="@color/button_selector" />
        </shape>
    </item>
</ripple>

button_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="@color/gray" android:state_enabled="false"/>
    <item android:color="@color/secondary"/>
</selector>
Related