views:

331

answers:

2

I've scoured the interwebs and found many posts regarding how to change the colors of a list view using a list selector. However it does not seem to work for me. So my question would be what am I doing wrong?

When I use the below files I get a list where all item backgrounds are initially blue. (I expected white)

When I move the focus up and down the text just changes to a dark grey and the item backgrounds are still blue. (This is when I would expect a single row to be blue with the rest white)

When I click on a row the background of the row I clicked on turns black and all other rows turned green. (I expected to have the row I clicked turn green and the rest be white)

Here is my main layout file:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<ListView
    android:id="@android:id/list"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:listSelector="@drawable/item_selector"
    />
<TextView
    android:id="@android:id/empty"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/empty"
    />
</LinearLayout>

Here is my list item file:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"
    android:orientation="horizontal" 
    android:padding="10sp">
    <CheckBox
        android:id="@+id/checkbox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:focusable="false"
        />
    <TextView 
        android:id="@+id/title" 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:ellipsize="end"
        android:singleLine="true" 
        android:textStyle="bold"
        android:padding="7dip"
        android:textSize="18sp"
        android:layout_toRightOf="@id/checkbox"
        />
</RelativeLayout>

Here is my colors file:

<?xml version="1.0" encoding="utf-8"?>
<resources> 
    <color name="red">#ff00</color>
    <color name="green">#f0f0</color>
    <color name="blue">#f00f</color>
    <color name="white">#ffff</color>
</resources>

Here is my selector file:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"&gt;
    <item 
        android:state_pressed="true" 
        android:drawable="@color/green" />
    <item 
        android:state_focused="true" 
        android:drawable="@color/blue" />
    <item 
        android:drawable="@color/white" />
</selector>

Hopefully it's something dumb and simple that I'm doing wrong.

Thanks in advance.

+2  A: 

I think that android:listSelector="@drawable/item_selector" is applied only on the root of the view not at all his subchild.

Try to add @drawable/item_selector as a backgroud of your RelativeLayout with android:background="@drawable/item_selector" and put a transparent layout to the listSelector:

android:listSelector="@android:color/transparent"
Francesco
That was the winning combination. Thank you! Been struggling with this for a couple days now.
hambonious
A: 

Actually if you want to do it programatically without the use of resources, you can set your element color,....say for example to set text color #F123456 in a TextView;

objTextView.setTextColor(color.parseColor("#F12345")

This is handy if you want to dynamically set a color in a list view that is user selectable, perhaps stored in a database where you simply cant use a static resource file for color lists.

PaulieG