1

This one is driving me crazy:

I have a ListView where I want to change the child's background when it is selected. The List Items have a default background, but when there is a background on the items, the selected item doesn't change color...

Let me visualize this a bit:

When I set A background color to the ListView's children I get this:

listitems with background

This is the code of a child in the ListView:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:background="@drawable/list_selector_normal">

    <TextView android:id="@+id/title_view"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:paddingBottom="5dp" />

    <TextView android:id="@+id/description_view"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

But when I Set some selectors to the listView in the main layout

    android:listSelector="@drawable/list_selector"

The selected list item does not change color...

But when I remove the background from the list item (line 5 in the first code block) the selectors do work, but my background is gone:

listitems without background

The selectors xml code:

<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:state_pressed="true"
          android:drawable="@drawable/list_selector_pressed" /> <!-- pressed -->
    <item android:state_focused="true"
          android:drawable="@drawable/list_selector_focussed" /> <!-- focused -->
    <item android:state_hovered="true"
          android:drawable="@color/blue" /> <!-- hovered -->
    <item android:drawable="@color/blue" /> <!-- default -->
</selector>

Is there a way (there should be) where I can keep my background (first image) and change the color of the selected item (second image)?

Hans Vn
  • 787
  • 1
  • 16
  • 32
  • Check this http://stackoverflow.com/questions/15253987/inflate-listview-row-from-onclicklistener-in-android – Pragnani Apr 18 '13 at 18:14
  • It looks like your default color is blue what if you changed it to transparent: ` `? – Sam Apr 18 '13 at 18:17
  • @Sam the transparent doesn't solve it either... only when I remove the background color of the LinearLayout there is a change... – Hans Vn Apr 18 '13 at 18:24
  • @Pragnani: I tried to add but that also didn't work... – Hans Vn Apr 18 '13 at 18:24
  • 1
    I'm guessing your item background is fully opaque? In that case it's likely the selector simply isn't visible, because the background obscures it: by default the selector gets drawn 'under' the child. Try setting `android:drawSelectorOnTop="true"` and see if that makes the selector show up. It won't be pretty, but at least you'll know what's going on. – MH. Apr 18 '13 at 19:15
  • @MH the background was indead fully opaque, I changed the to alpha 33% and now the selector changes the colors... I think that might also be a possible solution: using a transparent background to get about the same result :) – Hans Vn Apr 18 '13 at 19:37

1 Answers1

0

you can try to set normal background in ListView xml

    <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/list_selector_normal"
        />
Steven
  • 1