I'm trying to highlight (change the background color) of a row in a ListView when a user presses and releases it (after release, the highlight persists). I have read dozens of postings and for two days, I've tried everything, but can't get it to work. I've read postings that say it depends on if you're using a mouse or touchscreen. Some say it can't be done. My questions are: can this be done on this OS and with a touchscreen? And, if so, how (or what am I doing wrong)?
I'm running on an old phone (2.2.3 OS version). I read about this bug. I also am extending ArrayAdapter so each row can have an image (icon) along with text.
The following snippet is from the Activity xml for the ListView. Please note the choiceMode and listSelector:
<ListView
android:layout_width="match_parent"
android:layout_height="178dp"
android:id="@+id/listView"
android:layout_gravity="center_horizontal"
android:layout_weight="0.95"
android:choiceMode="singleChoice"
android:longClickable="false"
android:clickable="false"
android:listSelector="@drawable/list_view_selector"/>
Because of the bug in this old OS, I created several drawable xml files for rectangle shapes of various colors for test and debug of my attempts. Here is one for list_selector_green.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#00ff00" />
</shape>
My list_view_selector.xml file is:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_selected="false"
android:state_pressed="false"
android:drawable="@drawable/list_selector_red" />
<item
android:state_selected="false"
android:state_pressed="true"
android:drawable="@drawable/list_selector_purple" />
<item
android:state_selected="true"
android:state_pressed="false"
android:drawable="@drawable/list_selector_green" />
<item
android:state_selected="true"
android:state_pressed="true"
android:drawable="@drawable/list_selector_orange" />
</selector>
I created a listener for click events. I tried various things here (they're commented out).
myListView.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long arg3)
{
view.setSelected(true);
//myListView.setSelection(position);
//parent.requestFocusFromTouch(); // nothing gets selected
//parent.setSelected(true); // something (ListView?) gets selected
///parent.setBackgroundColor(Color.GREEN); // makes whole listview background green
//view.setBackgroundColor(Color.GREEN); // does nothing
//parent.setSelection(position); // does nothing
}
});
So, when I press an item, the background does change color, but when I release, it goes back to "normal" which I believe is transparent. I think it's acting like nothing is getting selected. The closest I came so far was calling parent.setSelected(true) which I think selected the entire ListView. When I did this, the first press was purple and the second was orange, so the system did appear to detect something was selected. It just didn't persist.
One more thing that may be relevant. Note in my list_view_selector.xml file that if state_selected and state_pressed are false, the color is red. Thus, I was expecting the initial background to be red, but it's not--it's the default (transparent?).