The solution by dglmtn doesn't work when you have a 9-patch drawable with padding as background. strange things happen, I don't even want to talk about it, if you have such a problem, you know them.
Now, if you want to have a listview with different States and 9-patch drawables (it wocould work with any drawables and colors, I think) You have to do 2 things:
- Set the selector for the items in the list.
- Get rid of the default selector for the list.
What you shoshould do is first set the row_selector.xml:
<? XML version = "1.0" encoding = "UTF-8" ?>
< Selector Xmlns: Android = "Http://schemas.android.com/apk/res/android" >
< Item Android: state_enabled = "True"
Android: state_pressed = "True" Android: drawable = "@ Drawable/list_item_bg_pressed" />
< Item Android: state_enabled = "True"
Android: state_focused = "True" Android: drawable = "@ Drawable/list_item_bg_focused" />
< Item Android: state_enabled = "True"
Android: state_selected = "True" Android: drawable = "@ Drawable/list_item_bg_focused" />
< Item
Android: drawable = "@ Drawable/list_item_bg_normal" />
</ Selector >
Don't forgetAndroid: state_selected
. It works likeAndroid: state_focused
For the list, but it's applied for the list item.
Now apply the selector to the items (row. XML ):
<? XML version = "1.0" encoding = "UTF-8" ?>
< Relativelayout Xmlns: Android = "Http://schemas.android.com/apk/res/android"
Android: layout_width = "Fill_parent"
Android: layout_height = "Wrap_content"
Android: Orientation = "Horizontal"
Android: Background = "@ Drawable/row_selector"
>
...
</ Relativelayout >
Make a selector for the list (to get rid of the default one which ruins the effect)-list_selector.xml:
<? XML version = "1.0" encoding = "UTF-8" ?>
< Selector Xmlns: Android = "Http://schemas.android.com/apk/res/android" >
< Item Android: state_enabled = "True"
Android: state_pressed = "True" Android: drawable = "@ Color/Android: Transparent" />
< Item Android: state_enabled = "True"
Android: state_focused = "True" Android: drawable = "@ Color/Android: Transparent" />
< Item
Android: drawable = "@ Color/Android: Transparent" />
</ Selector >
Notice that it makes all the States transparent. That's what I meant by getting rid of the default states.
Last, but not least-apply the selector to the listview:
<Listview
Android: ID= "@ + ID/Android: List"
...
Android: listselector= "@ Drawable/list_selector"
/>