Using Context Menus

Context Menus are contextualized by the currently focused View and are triggered by the user's pressing the trackball, middle D-pad button, or a View for around three seconds.

You define and populate Context Menus much as you define and populate Activity Menus. There are two options available for creating Context Menus for a particular View.

Creating Context Menus

One option is to create a generic ContextMenu object for a View class by overriding a View's onCreateContextMenu handler, as shown here:

©Override public void onCreateContextMenu(ContextMenu menu) { super.onCreateContextMenu(menu);


The Context Menu created here will be available within any Activity that includes this View class.

The more common alternative is to create Activity-specific Context Menus by overriding the Activity's onCreateContextMenu method, and registering the Views that should use it using the registerForContextMenu as shown in Listing 4-29.

LISTING 4-29: Assigning a Context Menu to a View

©Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);

EditText view = new EditText(this); setContentView(view);


Once a View has been registered, the onCreateContextMenu handler will be triggered the first time a Context Menu should be displayed for that View.

Override onCreateContextMenu and check which View has triggered the menu creation in order to populate the Context Menu parameter with the appropriate Menu Items, as shown in this extension to Listing 4-29.

©Override public void onCreateContextMenu(ContextMenu menu, View v,

ContextMenu.ContextMenuInfo menuInfo) { super.onCreateContextMenu(menu, v, menuInfo);

menu.setHeaderTitle("Context Menu"); menu.add(0, menu.FIRST, Menu.NONE,

"Item 1").setIcon(R.drawable.menu_item); menu.add(0, menu.FIRST+1, Menu.NONE, "Item 2").setCheckable(true); menu.add(0, menu.FIRST+2, Menu.NONE, "Item 3").setShortcut('3', '3'); SubMenu sub = menu.addSubMenu("Submenu"); sub.add("Submenu Item");

As shown in the preceding code, the ContextMenu class supports the same add method as the Menu class, so you can populate a Context Menu in the same way that you populate Activity menus — using the add method. This includes using the add method to add submenus to your Context Menus. Note that

Available for download on icons will never be displayed. You can, however, specify the title and icon to display in the Context Menu's header bar.

Android also supports late runtime population of Context Menus via Intent Filters. This mechanism lets you populate a Context Menu by specifying the kind of data presented by the current View, and asking other Android applications if they support any actions for it.

The most common example of this mechanism is the cut/copy/paste Menu Items available on Edit Text controls. Using Intent Filters to populate Context Menus is covered in detail in the next chapter.

Handling Context Menu Selections

Context Menu Item selections are handled much the same as Activity Menu selection. You can attach an Intent or Menu Item Click Listener directly to each Menu Item, or use the preferred technique of overriding the onContextltemSelected method on the Activity.

This event handler is triggered whenever a Context Menu Item is selected.

©Override public boolean onContextItemSelected(MenuItem item) { super.onContextltemSelected(item);

return false;

Was this article helpful?

0 0
Mobile Apps Made Easy

Mobile Apps Made Easy

Quick start guide to skyrocket your offline and online business success with mobile apps. If you know anything about mobile devices, you’ve probably heard that famous phrase coined by one of the mobile device’s most prolific creators proclaiming that there’s an app for pretty much everything.

Get My Free Training Guide


  • fesahaye
    How is oncreatecontextmenu triggered?
    8 years ago

Post a comment