[Android Software Development] michael.the.don:
[Ultimate Guide][Part 5-III][2/09/2011]Android App Development - From Scratc

PART V-II

Dynamically Updating Menu Items

By overriding your activity’s onPrepareOptionsMenu method, you can modify your menu based on the application state each time it’s displayed. This lets you dynamically disable/enable each item, set visibility, and modify text at runtime.

To modify Menu Items dynamically, you can either keep a reference to them when they’re created in the onCreateOptionsMenu method, or you can use menu.findItem as shown in the following skeleton

code, where onPrepareOptionsMenu is overridden:

@Override

public boolean onPrepareOptionsMenu(Menu menu) {

super.onPrepareOptionsMenu(menu);

MenuItem menuItem = menu.findItem(MENU_ITEM);

[ ... modify menu items ... ]

return true;

}

Handling Menu Selections

Android handles all of an Activity’s Menu Item selections using a single event handler, the onOptions ItemSelected method. The Menu Item selected is passed in to this method as the Menu Item parameter. To react to the menu selection, compare the item.getItemId value to the Menu Item identifi ers you used when populating the menu, and react accordingly, as shown in the following code:

public boolean onOptionsItemSelected(MenuItem item) {

super.onOptionsItemSelected(item);

// Find which menu item has been selected

switch (item.getItemId()) {

// Check for each known menu item

case (MENU_ITEM):

[ ... Perform menu handler actions ... ]

return true;

}

// Return false if you have not handled the menu item.

return false;

}

Creating Submenus

Submenus are displayed as regular Menu Items that, when selected, reveal more items. Traditionally, submenus are displayed using a hierarchical tree layout. Android uses a different approach to simplify menu navigation for small-screen devices. Rather than a tree structure, selecting a submenu presents a single floating window that displays all of its Menu Items. You can add submenus using the addSubMenu method. It supports the same parameters as the add method used to add normal Menu Items, allowing you to specify a group, unique identifi er, and text string for each submenu. You can also use the setHeaderIcon and setIcon methods to specify an icon to display in the submenu’s header bar or the regular icon menu, respectively. The Menu Items within a submenu support the same options as those assigned to the icon or extended menus. However, unlike traditional systems, Android does not support nested submenus. The code snippet below shows an extract from an implementation of the onCreateMenuOptions code that adds a submenu to the main menu, sets the header icon, and then adds a submenu Menu Item:

SubMenu sub = menu.addSubMenu(0, 0, Menu.NONE, “Submenu”);

sub.setHeaderIcon(R.drawable.icon);

sub.setIcon(R.drawable.icon);

MenuItem submenuItem = sub.add(0, 0, Menu.NONE, “Submenu Item”);

Using Context Menus

Context Menus are contextualized by the currently focused View and are triggered by pressing the trackball, middle D-pad button, or the View for around 3 seconds. You define and populate Context Menus similarly to the Activity menu. There are two options available for creating Context Menus for a particular View.

Creating Context Menus

The first option is to create a generic Context Menu for a View class by overriding a View’s onCreate ContextMenu handler:

@Override

public void onCreateContextMenu(ContextMenu menu) {

super.onCreateContextMenu(menu);

menu.add(“ContextMenuItem1”);

}

The more common alternative is to create Activity-specific Context Menus by overriding the onCreate ContextMenu method and registering the Views that should use it. Register Views using register ForContextMenu and passing them in, as shown in the following code:

@Override

public void onCreate(Bundle icicle) {

super.onCreate(icicle);

EditText view = new EditText(this);

setContentView(view);

registerForContextMenu(view);

}

Once a View has been registered, the onCreateContextMenu handler will be triggered whenever a Context Menu should be displayed for that View. Override onCreateContextMenu and check which View has triggered the menu creation to populate the menu parameter with the appropriate contextual items:

@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 above, the ContextMenu class supports the same add method as the Menu class, so you can populate a Context Menu in the same way as Activity menus — including support for submenus — but icons will never be displayed. You can also specify the title and icon to display in the Context Menu’s header bar. Android supports late runtime population of Context Menus using 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 behavior is the cut/copy/paste Menu Items available on EditText controls.

Handling Context Menu Selections

Context Menu Item selections are handled similarly to the Activity menu. You can attach an Intent or Menu Item Click Listener directly to each Menu Item, or use the preferred technique of overriding the onContextItemSelected method on the Activity.

This event handler is triggered whenever a Context Menu Item is selected within the Activity. A skeleton implementation is shown below:

@Override

public boolean onContextItemSelected(MenuItem item) {

super.onContextItemSelected(item);

[ ... Handle menu item selection ... ]

return false;

}