Seeing as this is getting a bit of attention again, I will post the solution I found (which I had previously mentioned in a comment):
I found android:drawSelectorOnTop="true"
in the ListView solved the problem.
Question
I have a ListView
item which has a set background. This overrides the default blue highlight that appears when the item is pressed/selected. Is there a way to have both the background and the selector?
This is my attempt at merging both a background and selector...
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:color="@color/red"/>
</selector>
<item>
<shape
android:dither="true"
android:shape="rectangle" >
<solid android:color="#ccc" />
</shape>
</item>
<item android:bottom="2dp">
<shape
android:dither="true"
android:shape="rectangle" >
<corners android:radius="6dp" />
<solid android:color="@android:color/white" />
</shape>
</item>
</layer-list>
This is in my drawable
folder, and I set it with this in my ListItem xml:
android:background="@drawable/my_background
Solution 2
Seeing as this is getting a bit of attention again, I will post the solution I found (which I had previously mentioned in a comment):
I found android:drawSelectorOnTop="true"
in the ListView solved the problem.
OTHER TIPS
To have a custom background and the default selector effect (another drawalbe when pressed / selected) is a little difficult, after a few tries, I made it.
You should define two selectors in separated xml file: listitem_background.xml
and listitem_selector.xml
.
The first one is used to the background of the list item, it will make the effect when the item is pressed and in normal state.
The second one is used to the selector of the list, it will get rid of the default selector of the list view by setting all the state to transparent
.
The default selector effect is defined in the first xml file: listitem_background.xml.
color_drawable.xml
, in res/values
directory:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- The color of the normal state. -->
<drawable name="listitem_normal">#E671B8</drawable>
<!-- The two color below show when the item is pressed, you should change that to the color you want. -->
<drawable name="listitem_pressed">#e7eeab</drawable>
<drawable name="listitem_selected">#e7eeab</drawable>
</resources>
listitem_background.xml
in res/drawable
:<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/listitem_pressed" android:state_enabled="true" android:state_pressed="true"/>
<item android:drawable="@drawable/listitem_selected" android:state_enabled="true" android:state_focused="true"/>
<item android:drawable="@drawable/listitem_selected" android:state_enabled="true" android:state_selected="true"/>
<item android:drawable="@drawable/listitem_normal"/>
</selector>
listitem_selector.xml
in res/drawable
:<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/android:transparent" android:state_enabled="true" android:state_pressed="true"/>
<item android:drawable="@color/android:transparent" android:state_enabled="true" android:state_focused="true"/>
<item android:drawable="@color/android:transparent"/>
</selector>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/listitem_background" >
...
</RelativeLayout>
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:listSelector="@drawable/listitem_selector" />
Just use of this in ListView to match the color combination
android:cacheColorHint="#e7eeab"