Android ListView color setting?

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.

+7
android listview
source share
2 answers

I think that android: listSelector = "@ drawable / item_selector" applies only to the root of the view, and not to its subordinate at all.

Try adding @ drawable / item_selector as the backgroud of your RelativeLayout with android:background="@drawable/item_selector" and put the transparent layout in the Selector list:

 android:listSelector="@android:color/transparent" 
+7
source share

In fact, if you want to do this programmatically without using resources, you can set the color of the element. For example, to set the text color # F123456 to a TextView :

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

This is convenient if you want to dynamically set the color in the form of a list that the user selects, possibly stored in a database where you simply cannot use the static resource file for color lists.

+1
source share

All Articles