As seen in the tablet version of gmail and google talk I am trying to show the current selection in a listview. I know this is not standard practice and should be avoided wh
Your other option is to set the background of your custom list item to android:background="?android:attr/activatedBackgroundIndicator"
What Gmail and similar apps use is the activated
state, with an appropriate row layout. See:
In a nutshell, you:
android.R.layout.simple_list_item_activated_1
)setChoiceMode(ListView.CHOICE_MODE_SINGLE)
on your ListView
setItemChecked()
on your ListView
to enable the "activated" state and have the persistent highlightIf you use a custom layout for each row:
an example of the selector drawable:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
android:exitFadeDuration="@android:integer/config_shortAnimTime">
<item android:state_activated="true" android:drawable="@color/android_green" />
<item android:drawable="@android:color/transparent" />
</selector>
What worked for me
override the global ListView Style in your theme
<item name="android:listViewStyle">@style/MyListView</item>
define your style
<style name="MyListView" parent="@android:style/Widget.ListView">
<item name="android:listSelector">@drawable/listview_background_selector</item>
</style>
define the selector listview_background_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" android:enterFadeDuration="@android:integer/config_mediumAnimTime" android:exitFadeDuration="@android:integer/config_mediumAnimTime">
<item android:drawable="@color/ListViewBackGroundColorPressed" android:state_activated="true" />
</selector>
set the background of your listview-item-layout
android:background="?android:attr/activatedBackgroundIndicator"