Home » Android » Android Core » Android ListView context menu: ActionMode.CallBack

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

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

and many more ....

 

2 comments

  1. Thanks man, helped me alot

Leave a Reply

Your email address will not be published. Required fields are marked *

*


6 + = fifteen

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Do you want to know how to develop your skillset and become a ...

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!
Get ready to Rock!
To download the books, please verify your email address by following the instructions found on the email we just sent you.

THANK YOU!

Close