2

I was reading the new Android Design Standards here: http://developer.android.com/design/patterns/selection.html and was wondering first how to make my ListView selectable (because long presses are now supposed to select items, not show a context menu like before), then to figure out how to pass that information to the ActionBar so I can make a contextual ActionBar based on what is selected (and how many items are selected). I've checked out several sites, including http://www.vogella.de/articles/AndroidListView/article.html but it doesn't show how to do it for ICS with the ActionBar.

I apologize for not giving any code, but I haven't had to do anything with listviews aside from making custom ones.

DustinRiley
  • 555
  • 6
  • 15

2 Answers2

13

For the first part (select list item), you need to do a couple things:

-Depending on the context, set your ListView to either single or multiple choice (depending on your app), using the setChoiceMode method.

lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
// or
lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

-in order to mark the item as selected, just listen for a long press and call the ListView's setItemChecked method.

lv.setItemChecked(position, true);

Finally, in order for the listview item to show as selected, you need to add the following attribute to the custom XML layout for that listview item:
android:background="?android:attr/activatedBackgroundIndicator"

For creating contextual Action Bar, the process is a little more detailed, but it's outlined in the Android Developer guide in the Using Contextual Action Mode section.

Alexander Lucas
  • 22,171
  • 3
  • 46
  • 43
  • Thanks! I've been looking for an answer like this for a while – afollestad Jul 12 '12 at 23:44
  • I'm doing the same thing except I use a checkbox and when I call setItemChecked it unchecks the checkbox? Any hints? – Geeks On Hugs Aug 17 '12 at 18:58
  • The application crashes when trying to inflate a layout containing android:background="?android:attr/activatedBackgroundIndicator" on a pre-3.0 device (where you wouldn't need this anyway). My solution is to choose a different layout for pre-3.0 devices at runtime: if (Build.VERSION.SDK_INT < Build.VERSION_CODES.GINGERBREAD_MR1) { – Buffalo Sep 25 '12 at 09:21
3

My recommendation is to create a class implementing the ActionMode.Callback interfaces. You can supply it with the id or data associated with the view clicked when you initialise it, and I'm sure you could make methods to change this if the user changes the views selected etc. Here's the basic one I use:

public class UserABMode implements ActionMode.Callback {

    private long mId;
    private String mEquation;

    public UserABMode(long id, String equation) {
        super();
        mId = id;
        mEquation = equation;
    }

    @Override
    public boolean onCreateActionMode(ActionMode mode, Menu menu) {
        mode.getMenuInflater().inflate(R.menu.customcontext, menu);
        return true;
    }

    @Override
    public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
        // don't need to do anything
        return false;
    }

    @Override
    public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
        //do stuff with mId or mEquation
        return true;
    }

    @Override
    public void onDestroyActionMode(ActionMode mode) {
        mMode = null;
    }

}
Alex Curran
  • 8,818
  • 6
  • 49
  • 55