Android context menu

Download context menu and contextual action mode sample

We use Android context menu to show list of actions that can be taken against an item. We can show a context menu for any view, but they are most often used for items in a ListView, GridView, or other view collections in which the user can perform direct actions on each item.

Android context menu Android contextual action mode

Type of context menu?

There are two type to provide contextual actions:

  • Floating context menu : A menu appears as a floating list of menu items (similar to a dialog) when the user performs a long click (press and hold) on a view that declares support for a context menu. Users can perform a contextual action on one item at a time.
  • Contextual action mode : This mode is a system implementation of ActionMode that displays a contextual action bar at the top of the screen with action items that affect the selected item(s). When this mode is active, users can perform an action on multiple items at once (if your app allows it).

How to register a view for context menu?

To register a view for context menu, call registerForContextMenu method :

Once a view register for context menu, system call onCreateContextMenu method. This is the place where you will inflate a menu resource for context menu or add a menu item programatically.

How to handle context menu item selection?

When user select an item of context menu, system call onContextItemSelected method. Here, we can take appropriate action based on context item selection.

How to start contextual action bar?

To start contextual action bar, we should implements ActionMode.Callback Interface. Once, implementation finished, we can start contextual action bar on any event. In below given example, I am starting CAB on itemClick event of a listView.

Android context menu and Contextual action mode example

In this example, I have taken 2 listView. One listView to show list of days and second listView to show list of students.

When user long press an item of Day listView, showing a floating context menu with 3 actions. When user click an item of student listView, showing Contextual action mode with 3 actions.

Step 1: Create a new project in Android Studio and name it Context Menu.

Step 2: Create a folder menu inside res directory if it is not already present.

Step 3: Open res -> menu and add a new menu resource file with a name context_menu.xml. Now, replace the code of context_menu.xml with the below given code.


Step 4: Open res -> layout -> activity_main.xml and replace its code with below given code.


Step 5: Open and replace its code with below given code.

Step 6: Open res -> values -> styles.xml and write your custom styling of action code. I just changed, the background of default Contextual action bar.


Step 7:Clean and run your project to see the result.

Share & Like

Tutorial topics

About is for Java, J2ee, Html5 and android developers. All tutorials are well explained and are easy to understand. We have tested every tutorials on our developement environment before posting it in public domain.