I’m currently using a drawable for all of my buttons, which gets a gradient over it when you press the button. I achieve this effect by using the various pressed-states and a layer-list.
Now I’m trying to create the same effect with a ListView. I’m able to show a different picture as the background for an object in the list when I click it, but whenever I try to add a picture with a separate gradient it shows up with the default Android color when clicking an object in the ListView. But, it does show my custom gradient on this default color.
I know this sounds a bit vague, so I took some screenshots to show the effect:
Button with gradient overlay, works fine
ListView with a background per object, does not work when using that image plus a gradient overlay

The drawable I’m using as the background image for the ListView

What could be the reason for the gradient working on the button, but not on the ListView? Like I said, using a completely different image for the pressed state works fine, but using the background image plus a gradient doesn’t.
Code used for the ListView pressed states
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Active tab -->
<item android:drawable="@drawable/list_gradient" android:state_focused="false" android:state_pressed="false" android:state_selected="true"/>
<!-- Inactive tab -->
<item android:drawable="@drawable/button_listpage_77px" android:state_focused="false" android:state_pressed="false" android:state_selected="false"/>
<!-- Pressed tab -->
<item android:drawable="@drawable/list_gradient" android:state_pressed="true"/>
<item android:drawable="@drawable/button_listpage_77px" />
</selector>
list_gradient.xml
</item>
<item>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient android:angle="90" android:startColor="#880f0f10" android:centerColor="#8858585a" android:endColor="#88a9a9a9"/>
</shape>
</item>
</layer-list>
After fiddling around for a few hours today, I still haven’t been able to fix my problem. I know I’m missing something but I can’t seem to figure out what. Does anyone have an idea?
After going over everything again, and reading all similar questions here on SO again, I noticed that nowhere in my files I used ‘android:listSelector`.
After setting the following in my
ListView:android:listSelector="#00000000", myListViewbehaved as I wanted to!I figured that the
ListViewwould work the same as all buttons, but I was wrong. Learn from my mistake, it might save you a whole lot of time.