Android ListView Selector Color
Asked Answered
N

2

73

Hi All,

I have 2 questions regarding a ListView in Android:

  1. How can I get the color of the listview's focused row ? I tried to use the ListView.getSelector() method, which according to its documentation should give me what I'm looking for, but it's giving me a Drawable object which I don't know how to retrieve the color from (if possible...).

  2. How can I set the color of the listview's focused row ? Here I tried to use the setSelector() method on the listview, passing it a ColorDrawable object, but the result of doing it is that the whole background of the list view is painted in that color... and this is not what I wanted of course...

Thanks!

Naked answered 10/1, 2010 at 18:33 Comment(2)
This may help (there is both question and solution): #2065930Filicide
It seems there is a bug in Android 2 that causes the colour to affect the whole background - check my answer that links to a workaround provided on another question.Eugeniusz
P
89

The list selector drawable is a StateListDrawable — it contains reference to multiple drawables for each state the list can be, like selected, focused, pressed, disabled...

While you can retrieve the drawable using getSelector(), I don't believe you can retrieve a specific Drawable from a StateListDrawable, nor does it seem possible to programmatically retrieve the colour directly from a ColorDrawable anyway.

As for setting the colour, you need a StateListDrawable as described above. You can set this on your list using the android:listSelector attribute, defining the drawable in XML like this:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:state_enabled="false" android:state_focused="true"
        android:drawable="@drawable/item_disabled" />
  <item android:state_pressed="true"
        android:drawable="@drawable/item_pressed" />
  <item android:state_focused="true"
        android:drawable="@drawable/item_focused" />
</selector>
Pillsbury answered 10/1, 2010 at 18:45 Comment(4)
I'd also move assigning the list selector to a custom theme, so all lists in your application would look same <item name="android:listSelector">@drawable/my_list_highlight</item>Prevost
This does not work with API 7/8 at least. It seems to change the whole background to the same pressed color when pressed, including background of other rows, and also the margin lines (I have black) between rows.Thermolabile
@Thermolabile it works for me when using drawables, but not when using colours. Have added answer referencing a good workaround that works for colours.Eugeniusz
Hello I am trying to use a selector in my listview like you have suggested but it has got 9 pngs and not colours , Also I have a custom layout that I inflate in my listview and use selector as background of that custom layout , Can you suggest?Mallett
E
11

TO ADD: @Christopher's answer does not work on API 7/8 (as per @Jonny's correct comment) IF you are using colours, instead of drawables. (In my testing, using drawables as per Christopher works fine)

Here is the FIX for 2.3 and below when using colours:

As per @Charles Harley, there is a bug in 2.3 and below where filling the list item with a colour causes the colour to flow out over the whole list. His fix is to define a shape drawable containing the colour you want, and to use that instead of the colour.

I suggest looking at this link if you want to just use a colour as selector, and are targeting Android 2 (or at least allow for Android 2).

Eugeniusz answered 8/4, 2013 at 7:36 Comment(1)
This bit me again just now, thank you for reminding me what the real issue is.Alanalana

© 2022 - 2024 — McMap. All rights reserved.