0

I want to set the color of row in listview if selected to yellow and otherwise be white so I use the following selector

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:state_enabled="true" 
     android:state_pressed="true" android:drawable="@color/encounter_normal"  />
    <item android:state_enabled="true"
     android:state_focused="true" android:drawable="@color/encounter_normal" />
    <item android:state_enabled="true"
     android:state_selected="true" android:drawable="@color/encounterselector_color" />
    <item
     android:drawable="@color/encounter_normal"  />
</selector>

where

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="encounterselector_color">#fbeda5</color>
    <color name="encounter_normal">#ffffff</color>  
</resources>

and I use it like the following

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" 

    android:id="@+id/EncounterGrid"
    android:background="@drawable/encounterlistview"

    >
 <!-- remaining code -->

but the row is always white , any idea how to fix that

AMH
  • 6,363
  • 27
  • 84
  • 135

4 Answers4

0

Setting the background color with a selector is a bit tricky. Basically you have to create a drawable for each color and use them in your android:drawable attributes. You cannot directly use colors.

Check this related question for more details.

Community
  • 1
  • 1
Dalmas
  • 26,409
  • 9
  • 67
  • 80
0

I use the following:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

<!-- This is the switched off state -->
<item   android:state_enabled="false"
        android:drawable="@drawable/grey_bar" />

<!-- These are the partial states -->
<item   android:state_pressed="true"
        android:drawable="@drawable/button_pressed" />        

<item   android:state_focused="true" android:state_enabled="true"
        android:drawable="@drawable/button_focused" />

<!--  This is the switched on state -->
<item   android:state_enabled="true"
        android:drawable="@drawable/button_normal" />
</selector>

Where all of the drawables I point to are defined in xml, or are existing 9 patch images.

ScouseChris
  • 4,377
  • 32
  • 38
0

use android:color on your selector and not android:drawable because you are getting reference to the color resources, so your selector will be like this:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:state_enabled="true" 
     android:state_pressed="true" android:color="@color/encounter_normal"  />
    <item android:state_enabled="true"
     android:state_focused="true" android:color="@color/encounter_normal" />
    <item android:state_enabled="true"
     android:state_selected="true" android:color="@color/encounterselector_color" />
    <item
     android:color="@color/encounter_normal"  />
</selector>
K_Anas
  • 31,226
  • 9
  • 68
  • 81
0

Nothing was working for me until I set drawSelectorOnTop = "true".

Everything worked after that.

Edd
  • 3,724
  • 3
  • 26
  • 33
Tomislav3008
  • 121
  • 9