I looked through interwebs and found many posts on how to change the colors of a list using a list selector. However, this does not seem to work for me. So my question will be what am I doing wrong?
When I use the files below, I get a list in which all the background of the element is initially blue. (I was expecting white)
When I move the focus up and down, the text just changes to dark gray and the background background is still blue. (This is when I expect one line to be blue and the rest to white)
When I click on a line, the background of the line I clicked on turns black, and all the other lines turn green. (I was expecting the line I clicked to turn green and the rest to 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 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 color 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"> <item android:state_pressed="true" android:drawable="@color/green" /> <item android:state_focused="true" android:drawable="@color/blue" /> <item android:drawable="@color/white" /> </selector>
Hope this is something dumb and simple that I'm doing wrong.
Thanks in advance.
android listview
Ross hambrick
source share