Creating Contextual Action Bar – CAB

Before starting all about the action bar and Contextual Action bar concept and coding, I will explain you 2 ways to show contextual actions:

1. Floating Context Menu
2. Contextual Action Mode

You can check my previous tutorial on Action Bar and Action bar Search View to read more on Action bar before starting CAB.

1. Floating Context Menu


In earlier versions of Android, we were used to see almost all the apps having context menu ready for showing options (menu items) whenever user performs a long press on any element. Now, since from Android 3.0, the purpose of Long press gesture has changed, its now used to handle multi-select and contextual actions.

2. Contextual Action Mode

The contextual action mode is a system implementation of ActionMode that focuses user interaction toward performing contextual actions. When a user enables this mode by selecting an item, a contextual action bar appears at the top of the screen to present actions the user can perform on the currently selected item(s).


How to use/implement Contextual Action bar (CAB):

There are 2 designs by which you can implement Contextual Action bar:

1.) Enable CAB when user selects a particular view
2.) Enable CAB whenever user performs a long press gesture on particular view

In this tutorial we will talk about the first method of implementing CAB. The second method I will explore in my next tutorial.


1.) Implement the ActionMode.Callback interface. In its callback methods, you can specify the actions for the contextual action bar, respond to click events on action items, and handle other lifecycle events for the action mode.




2.) Call startActionMode() when you want to show the bar (such as when the user long-clicks the view).




Let’s build an example to enable Contextual action mode on the CheckBox selection. Write below code in your




Create and run the above code to check the results.


Leave a Comment: