I stumbled about a strange problem with ListViews.
In the emulator the ListView shows no feedback, when an item is pressed for selection. I think I have seen that in the real thing, but I am not sure. I would expect that the list item gets selected (darker background) when it is pressed, so that the user can see, which item he pressed.
Do I have to code that?
RG
After seeing the comments, I tried the following:
I created a file res/color/backgroundstate.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true"
android:color="#888"/>
<item android:state_pressed="true"
android:color="#888"/>
<item android:color="#eee"/>
</selector>
Then I use this in listitem.xml as a background color
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textColor="@color/helptextcolor"
android:background="@color/backgroundstate"
android:padding="10dp"
android:textSize="16sp" >
</TextView>
When I display the ListView with
ArrayAdapter adapter=new ArrayAdapter<String>(this,R.layout.listitem,names);
LV.setAdapter(adapter);
LV.invalidate();
the program crashes.
There are three exceptions connected with this. But the problem seems to be the last one XMLPullParserException:
<item> tag needs a drawable.
Now?
<item>indeed needs a drawable, not a color. You need a color drawable for that, like this:…and then put that into your item list.
Furthermore, this is a drawable and as such, it has to be in a
/res/drawable-*directory, not in the/res/color/directory.To sum it up: