Android Core

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).
 

Francesco Azzola

He's a senior software engineer with more than 15 yrs old experience in JEE architecture. He's SCEA certified (Sun Certified Enterprise Architect), SCWCD, SCJP. He is an android enthusiast and he has worked for long time in the mobile development field.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Jakob
Jakob
11 years ago

Thanks man, helped me alot

Nikhil Musale
11 years ago

Thanks ..it helps me a lot..!

Back to top button