Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ListView items don't become visually "highlighted" when selected during contextual action mode

I followed the official Android site's tutorial on creating contextual action menus. Using the code below, when I long press on one of my ListView items, it does become selected, but it does not visually indicate that its been selected. I am using the Holo Light theme, and I expect the background color of every selected item in my ListView to change to a shade of blue.

Is this normal behavior?

I have tried testing listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE); and not even a single row will highlight.

Using listView.setSelector(android.R.color.holo_blue_light); does appear to highlight the row which was last selected, but it does not highlight the other rows which are selected.

Have I done something wrong, or do I need to make the background change manually? If so, how?

I have also tried listView.setSelector(android.R.drawable.list_selector_background); which is a real selector that contains items for different states. Unfortunately, it still only applies to the most recently selected ListView item.

public class MyActivity extends ListActivity {

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        // The list is generated here

        ListView listView = getListView();
        listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);

        listView.setMultiChoiceModeListener(new MultiChoiceModeListener() {
            // implements empty methods
        }
    }
}

Thanks!

like image 516
Gleasonator Avatar asked Apr 04 '13 21:04

Gleasonator


2 Answers

In my opinion it should be possible the way do describe, but unfortunately it does not.

What I have found so far is that you have to use a XML resource which describes the way the background should look like when the 'state' of the item is 'activated'.

Create a file called 'list_item_background_activated.xml' in the 'res/drawable/' directory.

In it you define a root element of type selector:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item 
    android:state_activated="true"
    android:drawable="@android:color/darker_gray" />

</selector>

Now you should modify the related resource (the resource which defines how you ListItem looks like) to reference this drawable:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/list_item_background_activated" >
    ...
</RelativeLayout>
like image 188
Brabbeldas Avatar answered Nov 10 '22 02:11

Brabbeldas


I just implemented Contextual Action Mode and had the same problem that you have/had. I too figured that the default behavior would be that each selected row would be highlighted in some way, but no.

The easiest way for me to get the desired effect was to change the list item resource I was using for my ListActivity.

I'm using an ArrayAdapter so the choice for me is made in the constructor of the adapter:

new ArrayAdapter<Exercise>(context, android.R.layout.simple_list_item_1);

The last parameter is the resource id for a layout file the adapter will use for instanciating new views (List items) in the listview.

I had chosen the one I figured to be the most basic one. By changing it to:

new ArrayAdapter<Exercise>(context, android.R.layout.simple_list_item_activated_1);

I got the desired effect, the rows selected in Contextual Action Mode are now staying highlighted.

There's also a couple of other resource id's defined in android.R.layout that you can chose from to get a similar but different result: http://developer.android.com/reference/android/R.layout.html , simple_list_item_activated_1 just did it for me.

Not sure if

// The list is generated here 

hides an adapter and if so an array adapter but if it does, this should help.

Otherwise I figure you could use the predefined resources in another place or take the slightly longer way and define selectors as Brabbeldas suggests

like image 2
Klepto Avatar answered Nov 10 '22 01:11

Klepto