0

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>
SparkyNZ
  • 6,266
  • 7
  • 39
  • 80

3 Answers3

1

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.

matiash
  • 54,791
  • 16
  • 125
  • 154
  • Thanks but @color/default_color results in an error in the XML. @color/transparent (defined as #00000000 in my colors.xml file) seems to return the row to its previous background colour. – SparkyNZ Jun 10 '14 at 08:39
  • Ahem, default color was just an example. You were supposed to put your own color in there! :-| – matiash Jun 10 '14 at 10:56
  • OK :-) Wasn't sure if default_color meant 'use system default color' or whatever. Thanks for the help. – SparkyNZ Jun 10 '14 at 20:08
1

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>
Rod_Algonquin
  • 26,074
  • 6
  • 52
  • 63
  • Really? I thought I'd tried doing this and it did the same as the "delete this" line. I'll give it another try when I get home again. Thanks. – SparkyNZ Jun 10 '14 at 02:38
  • Hi Rod. I'm afraid it still does the same thing. My ListView contains rows that are actually white on my Android tablet, yet after I touch the rows, they change to black. If I touch another row, the row goes back to white and the newly touched row changes to black - so it is showing the "selected" state rather than the default row state if you follow. – SparkyNZ Jun 10 '14 at 08:35
  • I found the problem and I'm going to accept your answer. It wasn't behaving as you described because I actually had 2 selectors.. One I had assigned in my row_item.xml file and another, programmatically, I had assigned using getListView().setSelector(). – SparkyNZ Jun 10 '14 at 08:55
0

make a custom listview with a custom adapter:

Custom Adapter for List View

this was you can use a view with the background you want in getView

Community
  • 1
  • 1
micnubinub
  • 116
  • 1
  • 8