I want to change the background of a listview item when the user clicks it. Kind of like the Honeycomb settings page (Although I\'m not dealing with just settings so I\'m no
Ok, so I tried the solution above from where it says "This is implementation of sgarman idea:" This only works if SetFocusListener is an OnTouchListner. Ohterwise the onClick method consumes the click. I had to pair this solution with an OnItemClick listener on my list item to get the list to actually show the highlighted item.
By default, 'Selected' isn't the same as 'Clicked' when you're using a touch interface - something that cause me some real headaches when I started Android development.
To support both users that navigate by touch and users that use scrollwheels/trackballs, you might want to use setSelection, and do your manipulation in an AdapterView.OnItemSelectedListener implementation (set with setOnItemSelectedListener).
Another gotcha is that setSelection won't highlight an item if the last event was a touch event.
I'd recommend that you create a custom View for your list items, and handle highlighting in there.
Hope this helps,
Phil Lello
try
android:background="?android:attr/activatedBackgroundIndicator"
;)
Hope this help,
1.- Create a shape file for focused item: \drawable\list_selector_focused.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<gradient android:angle="90" android:startColor="#f5c98c" android:endColor="#f7ddb8"/>
</shape>
2.- Create a shape file for pressed item: \drawable\list_selector_pressed.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<gradient android:angle="90" android:startColor="#fb9d23" android:endColor="#ffc579" />
</shape>
3.- Create list selector file: \drawable\list_selector.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@drawable/list_selector_pressed" />
<item android:state_focused="true" android:drawable="@drawable/list_selector_focused" />
<item android:drawable="@drawable/list_selector_focused" />
</selector>
4.- Add this attribute to your ListView in the layout file:
android:choiceMode="singleChoice"
android:listSelector="@drawable/list_selector"
You can use colors instead of gradient shapes,
The android state checked is best used to resolve this issue.
Someone mentioned using android:background="?android:attr/activatedBackgroundIndicator".
This just points to one of the activated_background_* resources in frameworks/base/core/res/res/drawable of the android source code. For example activated_background_holo_dark.xml:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_activated="true" android:drawable="@android:drawable/list_activated_holo" />
<item android:drawable="@color/transparent" />
</selector>
So essentially you want to use state_activated to represent when the user presses the button as well when it is in a checked (i.e. in a persistent selected state) state. Note that activated was only introduced after Honeycomb, if you are targeting older devices you'll need to rely on state_checked (more details here).
Now if you want to set an item as checked, you need to call listView.setItemChecked(position, true)
. You'll likely want to set the android:choiceMode
property on your ListView to the appropriate value (e.g. if you want only one thing selected at a time use singleChoice
). You don't need to invalidate, the call to setItemChecked will trigger a relayout which will update the view.
Also be careful if you allow reordering items in your ListView as the current checked item(s) will need to be updated. If you use stable Ids, this will be handled automatically.
To see an example of this in action, check out the NavigationDrawer sample code found in the training series: http://developer.android.com/training/implementing-navigation/nav-drawer.html.
I used android:state_activated="true"
instead of state_selected
. It works like a charm!