5

I'm trying to get my app working properly in 2.3 (it works fine in 4.0+) and one issue I'm having is that on my listview I can't get the background on the selected item to change. I'm not sure what I need to change - does anyone know?

Here's the listview itself:

<ListView
    android:id="@+id/score_list"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_above="@+id/keyboard"
    android:layout_below="@+id/sort_header"
    android:choiceMode="singleChoice"
    android:divider="#CCCCCC"
    android:dividerHeight="1dp"
    android:listSelector="@drawable/selector" />

Here's the selector that works in 4.0+ (in 2.3 there is no color change):

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_focused="true" android:drawable="@color/highlight"/>
    <item android:state_pressed="true" android:drawable="@color/highlight"/>
    <item android:state_activated="true" android:drawable="@color/highlight"/>
    <item android:state_selected="true" android:drawable="@color/highlight"/>
</selector>

I actually don't need all 4 of those, but I wanted to try everything.

GrilledCheese
  • 311
  • 3
  • 8
  • 13

4 Answers4

7

I had exactly the same problem. I haven't found a way how to do this in XML, but I have found a workaround in code. The following code is tested in application that supports API level 7+

First, you need to alter the adapter for the ListView a bit:

public class ListViewAdapter extends BaseAdapter {
  private int selectedItemPosition = -1;

  // your code

  public void selectItem(int i) {
    selectedItemPosition = i;
  }

  @Override
  public View getView(int i, View view, ViewGroup viewGroup) {

    // your code

    if (i == selectedItemPosition) {
      // set the desired background color
      textView.setBackgroundColor(context.getResources().getColor(R.color.highlight));
    }
    else {
      // set the default (not selected) background color to a transparent color (or any other)
      textView.setBackgroundColor(Color.TRANSPARENT);
    }
    return view;
  }
}

Next you have to inform the adapter that the selection changed in your OnItemClickListener's onItemClickMethod:

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
  // select the item
  ((ListViewAdapter) listview.getAdapter()).selectItem(position);
  // force redraw of the listview (invalidating just "view" is not enough)
  ((ListView) parent).invalidateViews();

  // your code
}

That should be it. Now whenever you wan't to change the selected item you can use the same code that is used in onItemClick(), ie. selectItem() followed by invalidateViews(). Instead of calling invalidateViews(), the adapter's notifyDataSetChanged() can also be used.

Also you should also add an appropriate listSelector to the list view, to avoid the brief flicker of the default selector when the item is clicked. However there's a bug with list selectors on API 7 and 8 when the background of the whole view is changed. You can find a workaround here

Community
  • 1
  • 1
stativ
  • 1,452
  • 12
  • 23
1

Try to set property in your listview android:cacheColorHint="@null. The listview background won't be hightlight by touch.

user3134124
  • 387
  • 4
  • 16
0

android:state_activated Introduced in API level 11.

See this link Drawable Resources

Update

I am use this in my app (API level 8)

    <?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Selector style for listrow -->
<item 
 android:state_selected="false"
    android:state_pressed="false" 
    android:state_focused="false"
    android:drawable="@color/normal"/> 

<item android:state_pressed="true"
        android:drawable="@color/highlight"/>

<item android:state_selected="true"
     android:state_pressed="false"
     android:drawable="@color/highlight"/>

<item android:state_focused="true"
     android:state_pressed="false"
     android:drawable="@color/highlight"/>
</selector>
AwadKab
  • 3,054
  • 2
  • 17
  • 17
  • So what I can do on API level 9? Nothing on that list works for highlighting the selected item. – GrilledCheese Apr 03 '13 at 22:29
  • When I click, this happens really quickly: 1. Whole list gets highlighted 2. Item I clicked unhighlights 3. Whole list unhighlights So it doesn't highlight the one I clicked. If I press and hold, the whole list stays highlighted until I release. – GrilledCheese Apr 03 '13 at 23:31
  • My experience is exactly the same as GrilledCheese – Charlie S Jun 01 '13 at 01:12
0

As is written in AwdKab response, the android:state_activated is introduced in API level 11. The solution is to implement Checkable interface for your top View of list item layout, see my my post here.

Community
  • 1
  • 1
Arcao
  • 1,391
  • 12
  • 16