Android ListView context menu: ActionBar ActionMode.CallBack

In this post we want to analyze the Android ListView context menu (contextual actionbar). A contextual actionbar is a menu that is activated when user long press on a specific item. The contextual menu can be applied to almost all views but it is usually used with ListView.
You will learn:

Contextual action mode

How to use Long click: onItemLongClickListener

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.
android Listview context menu
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 Android ListView context 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 when creating Android ListView context menu iscreating 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="">
<item android:id="@+id/edit"

<item android:id="@+id/delete"

Now we have our menu and we simply have to “inject” it in the onCreateActionMode method.
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
mode.getMenuInflater().inflate(, menu);
return true;

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

Android ListView Context menu: ActionMode and Long Click

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");
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 {
aAdpt.remove( aAdpt.getItem(aAdpt.currentSelection) );
case {
System.out.println(" edit ");
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 calling mode.finish (line 7). At the end of this post you gained the knowledge how to activate Android ListView context menu.

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.