Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drawable selector not working in Jelly Bean

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.

like image 755
Kris B Avatar asked Jul 26 '12 01:07

Kris B


4 Answers

From the API Docs:

Note: Remember that Android applies the first item in the state list that matches the current state of the object. So, if the first item in the list contains none of the state attributes above, then it is applied every time, which is why your default value should always be last (as demonstrated in the following example).

Thus, try to reorder the states of your selectors according to the suggested order. In your case that would be this:

<selector xmlns:android="http://schemas.android.com/apk/res/android" >
   <item android:state_pressed="true" android:drawable="@color/blue" />
   <item android:state_focused="true" android:drawable="@color/blue" />
   <item android:state_selected="true" android:drawable="@color/blue_selected" />
   <item android:state_activated="true" android:drawable="@color/blue_selected" />  
   <item android:drawable="@android:color/transparent" />
</selector>
like image 198
Ridcully Avatar answered Nov 15 '22 17:11

Ridcully


Okay, i think its an issue with your selector. Try removing the state_focused and the state_activated. You could try this for your selector:

<!-- Disabled State -->
<item
    android:state_enabled = "false"
    android:state_focused = "true"
    android:state_pressed = "true"
    android:drawable="@android:color/transparent">
</item>
<item
    android:state_enabled = "false"
    android:state_focused = "true"
    android:drawable="@android:color/transparent">
</item>

<!-- Pressed State -->
<item
    android:state_pressed = "true"
    android:state_focused = "true">
    <shape>
        <solid android:color="@color/blue"/>
    </shape>
</item>
<item
    android:state_pressed = "true"
    android:state_focused = "false">
    <shape>
        <solid android:color="@color/blue"/>
    </shape>
</item>

<!-- Normal State -->
<item
    android:drawable="@android:color/transparent">
</item>

I found that I needed to use a shape object too instead of android:drawable, because on pre-ICS phones, the whole list will be highlighted that color instead of the pressed list item.

You can add in your state_selected code too, but I'm not sure how it will be used. Check out the default selector code for jelly bean for the states they use: list_selector_background.xml.

like image 2
Chris Feist Avatar answered Nov 19 '22 06:11

Chris Feist


listing_selector.xml must be in res/drawable folder and set the android:background attribute of your RelativeLayout like this:

<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="@drawable/listing_selector"    
>
like image 1
K_Anas Avatar answered Nov 19 '22 05:11

K_Anas


I cant say the reason why its working in one version and not working in another, but I got a alternative solution.

Define your color as a drawable in resources

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <drawable name="blue">#00F</drawable>
    ...
</resources>

Use this color drawable in your selector

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

Try this and let me know.

like image 1
Ronnie Avatar answered Nov 19 '22 05:11

Ronnie