views:

511

answers:

1

Hi, i have a listview with a custom item_row.xml. I've defined a selector in this way:

<?xml version="1.0" encoding="utf-8"?>

<item
    android:state_pressed="false"
    android:drawable="@drawable/list_bg" >
</item>
<item 
    android:state_pressed="true"
    android:drawable="@drawable/header_bg" >
</item>
<item
    android:state_focused="false"
    android:drawable="@drawable/list_bg" >
</item>
<item
    android:state_focused="true"
    android:drawable="@drawable/header_bg">
</item>

and then put into item_row.xml in this way:

    <LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="70dip"
  android:orientation="vertical"
  android:background="@drawable/list_selector"> 

I want 2 things:

  • When i move with arrow keys, the item selected changed its background. It's works fine with the actual implementation of selector.
  • When i press a item, the item changed its background too, but it doesn't work with the actual selector.

Any idea? I try to set also into the ListView android:listSelector="@drawable/list_selector" but it doesn't work neither.

Thanks

+1  A: 

You should model your StateListDrawable after the one used by Android itself for ListView selectors. You can find this in $ANDROID_HOME/platforms/$VERSION/data/res/drawable/list_selector_background.xml, where $ANDROID_HOME is where your Android SDK is installed and $VERSION is some Android version (e.g., android-2.1). Then, apply the list selector via android:listSelector in the ListView in your layout XML.

CommonsWare
Now i have the selector working fine, but the problem is.. if i have a background for the item_row_list, the selector doesn't appear. and i can't put on top because it covers the information of the row..thanks!
xger86x
Make the selector transparent or translucent, and put it on top.
CommonsWare
it still doesn't work. If i use <item android:state_pressed="true" android:drawable="@android:color/transparent" ></item>i can't see the selector and it covers the text too.What i want is: - See a selector traslucent which appears when i move along the list with arrow keys and when i press one item.If you could help me i would be very grateful
xger86x