Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android compatibility contextual action bar

Tags:

In trying to follow the Android Design Guidelines, I'm running into a small quandary.

I want to have a list of items that I can long-press several of (multi-select), and then perform bulk actions on them.

The Design Guidelines suggest using the Contextual Action Bar for this, and it sounds perfectly like what I had in mind. Problem is, I'm trying to maintain compatibility backwards to API 7 (due to my phone being 2.3.3 currently).

I'm using ActionBarSherlock to get other actionbar stuff, but I can't seem to figure out how to get it to either fire up a contextual action bar, nor have I figured out how to add buttons arbitrarily to the ActionBar in ABS. I see you can do tabs, so maybe that's the answer, but since I'm trying to allow multi-select, I don't want to have the normal modal context menu.

like image 919
Paul Avatar asked Mar 02 '12 18:03

Paul


People also ask

What is contextual action mode android?

↳ android.view.ActionMode. Represents a contextual mode of the user interface. Action modes can be used to provide alternative interaction modes and replace parts of the normal UI until finished. Examples of good action modes include text selection and contextual actions.

What is android Action Bar?

Android ActionBar is a menu bar that runs across the top of the activity screen in android. Android ActionBar can contain menu items which become visible when the user clicks the “menu” button.


2 Answers

This is a late answer, but I think would help people stuck.

Opening the contextual action bar is actually pretty simple, at any point in your activity you just have to call:

startActionMode(mActionModeCallback); 

If you are not in your main activity, like in fragments, you can get a reference with

getSherlockActivity().startActionMode(mActionModeCallback); 

and this is the callback

private ActionMode.Callback mActionModeCallback = new ActionMode.Callback(){      @Override      public boolean onCreateActionMode(ActionMode mode, Menu menu) {           MenuInflater inflater = mode.getMenuInflater();           inflater.inflate(R.menu.actionbar_context_menu, menu);           return true;         }      @Override     public void onDestroyActionMode(ActionMode mode) {      }      @Override     public boolean onActionItemClicked(ActionMode mode, MenuItem item) {         switch (item.getItemId()) {             case R.id.menu_item1:                 return true;             case R.id.menu_item2:                 //close the action mode                 //mode.finish();                 return true;             default:                 mode.finish();                 return false;        }     } }; 

The xml is a simple menu like the actionbar one:

<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android">  <item android:id="@+id/menu_item1"       android:icon="@drawable/ic_item1"       android:title="@string/ITEM1"       android:showAsAction="always|withText" />  <item android:id="@+id/menu_item2"       android:icon="@drawable/ic_item2"       android:title="@string/ITEM2"       android:showAsAction="always|withText" /> 

like image 84
sokie Avatar answered Sep 16 '22 21:09

sokie


Setting up contextual actionbar is the same to setting up the 'regular' ActionBar items as far as the XML is concerned. This example in the developer's guide explains it all.

In order to use ActionBarSherlock, replace the default Android-callbacks to the ActionBarSherlock-edited callbacks (e.g. instead of Android.View.ActionMode, use com.actionbarsherlock.view.ActionMode).

like image 22
Reinier Avatar answered Sep 16 '22 21:09

Reinier