Reputation: 6676
I have looked at numerous SO questions and cannot figure this out. I have managed to use a selector to change the hilighting color of my ListView -but how do I set the normal (non-pressed) background colour?
The below XML doesn't work as is only changes my row item background to black once the item has been pressed. How should I be doing this?
<selector xmlns:android="http://schemas.android.com/apk/res/android"
android:exitFadeDuration="@android:integer/config_mediumAnimTime">
<item android:state_focused="true" android:drawable="@color/row_hilight"/>
<item android:state_pressed="true" android:drawable="@color/row_hilight"/>
<item android:state_pressed="false" android:drawable="@color/black"/>
</selector>
Upvotes: 0
Views: 378
Reputation: 26198
You need to have a default state which does not have state.
Also you need to remove android:state_pressed="false"
that is because it has the same functionality with the default state.
<selector xmlns:android="http://schemas.android.com/apk/res/android"
android:exitFadeDuration="@android:integer/config_mediumAnimTime">
<item android:state_focused="true" android:drawable="@color/row_hilight"/>
<item android:state_pressed="true" android:drawable="@color/row_hilight"/>
<item android:state_pressed="false" android:drawable="@color/black"/> //delete this
<item android:drawable="@color/black"/> //add this
</selector>
Upvotes: 1
Reputation: 126
make a custom listview with a custom adapter:
this was you can use a view with the background you want in getView
Upvotes: 0
Reputation: 55350
Just add one more item at the end:
<item android:drawable="@color/default_color"/>
From the documentation for state list drawables:
During each state change, the state list is traversed top to bottom and the first item that matches the current state is used—the selection is not based on the "best match," but simply the first item that meets the minimum criteria of the state.
Therefore, having an item with no specific criteria will match if none of the others do.
Upvotes: 1