Android listview selected item layout

155 Views Asked by At

I have this layout for a listview's items

<selector xmlns:android="http://schemas.android.com/apk/res/android">
   <item android:drawable="@drawable/card_layout_bg"
         android:state_pressed="false"/>
   <item android:drawable="@drawable/card_layout_pressed_bg"
         android:state_pressed="true"/>
   <item android:drawable="@drawable/card_layout_pressed_bg"
         android:state_selected="true"/>
</selector>

With the third item i tried to show which item is selected when actionmode context menu appears, but I can not figure out which state do I have to use to get the expected result

2

There are 2 best solutions below

0
On

You should add the activated state background to your selector as well, in order to display the selection of a ListView:

<item android:drawable="@drawable/card_layout_pressed_bg"
     android:state_activated="true"/>
0
On

Pressed is called when you press, selected is called when you use keyboard to select button or use in code "setSelected". The best way to checkout is to use different bg for selected. In your code pressed ad selected have the same bg!