About Francesco Azzola

I'm a senior software engineer with more than 15 yrs old experience in JEE architecture. I'm SCEA certified (Sun Certified Enterprise Architect), SCWCD, SCJP. I'm an android enthusiast and i've worked for long time in the mobile development field.

Android ListView context menu: ActionMode.CallBack

In this post we want to analyze the context menu (contextual action bar). This is a menu that is related to a specific item. The contextual menu can be applied to almost all views but it is usually used with ListView. We talk a lot about List view, because it is one of the most important component. We can distinguish two different type of contextual menu:

  • Floating menu
  • Contextual action mode (ActionMode)

The floating menu is used with Android version lower than 3.0 (API level 11). It is essentially a menu that appears when an user long click on an ListView item. You can
 
find an example here. It looks like the image shown below:

The contextual action mode is introduced in Android 3.0 or higher and it is essentially a contextual bar that appears on the top when user long clicks an item. According to Android guides this kind of menu is better than the floating menu. In this post we want to analyze how we can create this menu.

Create contextual action Mode: Define ActionMode.CallBack interface

To create a contextual menu we have first to define a ActionMode.CallBack interface. This interface is called when an user long clicks on an ListView item. The code looks like:

private ActionMode.Callback modeCallBack = new ActionMode.Callback() {

   public boolean onPrepareActionMode(ActionMode mode, Menu menu)    
    return false;
   }

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

   public boolean onCreateActionMode(ActionMode mode, Menu menu) {
     return true;
   }

   public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
   }
};

We are interested on line 11 and line 15. The first one is where we will create our contextual action bar on the top of the screen and in line 15 is where we handle the logic when user chooses one of our menu item.

The first thing we have to do is creating our menu. For simplicity we can suppose we have just two menu items, then we define a file under res/menu called activity_main.xml:

<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/edit"
          android:icon="@android:drawable/ic_menu_edit"/>

    <item android:id="@+id/delete"
          android:icon="@android:drawable/ic_menu_delete"/>

</menu>

Now we have our menu and we simply have to “inject” it in the
onCreateActionMode method.

public boolean onCreateActionMode(ActionMode mode, Menu menu) {
 mode.setTitle("Options");
 mode.getMenuInflater().inflate(R.menu.activity_main, menu);
 return true;
}

Now we have to show this contextual action bar when user long clicks on an item.

ActionMode and Long Click: onItemLongClickListener

If we want to show this contextual bar when user long clicks we have simply set a listener on our ListView, that we call lv in the source code. So we have:

lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
   public boolean onItemLongClick (AdapterView parent, View view, int position, long id) {
     System.out.println("Long click");
     startActionMode(modeCallBack);
     view.setSelected(true);
     return true;
   }
});

In line 4 we simply start the contextual menu using startActionMode method. Now the result is:

As you can see in the top we have our contextual action bar.

Contextual menu item selection

Now let’s suppose we an user clicks on a menu item. How do we handle this event? Well if we come back at ActionMode.CallBack we have to implement another method onActionItemClicked. So we have:

public boolean onActionItemClicked(ActionMode mode, MenuItem item) {

 int id = item.getItemId();
 switch (id) {
   case R.id.delete: {
     aAdpt.remove( aAdpt.getItem(aAdpt.currentSelection) );
            mode.finish();
     break;
          }
          case R.id.edit: {
     System.out.println(" edit ");
            break;
         }
         default:
            return false;

}

In line 6 we simply remove from our adapter the selected item. To know the position of the selected item inside the ListView we store it in the OnItemLongClickListener method.

aAdpt.currentSelection = position;

When we finish handling user menu item selection we have to dismiss the contextual action bar callig mode.finish (line 7).
 

Related Whitepaper:

Rapid Android Development: Build Rich, Sensor-Based Applications with Processing

Create mobile apps for Android phones and tablets faster and more easily than you ever imagined

Use 'Processing', the free, award-winning, graphics-savvy language and development environment, to work with the touchscreens, hardware sensors, cameras, network transceivers, and other devices and software in the latest Android phones and tablets.

Get it Now!  

2 Responses to "Android ListView context menu: ActionMode.CallBack"

  1. Jakob says:

    Thanks man, helped me alot

  2. Thanks ..it helps me a lot..!

Leave a Reply


9 × three =



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.

Sign up for our Newsletter

20,709 insiders are already enjoying weekly updates and complimentary whitepapers! Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

As an extra bonus, by joining you will get our brand new e-books, published by Java Code Geeks and their JCG partners for your reading pleasure! Enter your info and stay on top of things,

  • Fresh trends
  • Cases and examples
  • Research and insights
  • Two complimentary e-books