Monday, August 25, 2014

Menus

Android has a 3 stage menu system.

Icon menu

  • Appears along the bottom of the screen when the menu button is pressed. 
  • It displays the icons and text for a limited number of Menu Items (typically six)
  • This icon menu does not display checkboxes, radio buttons, or the shortcut keys for Menu Items
  • Pressing the back button closes the icon menu



Expanded menu

  • The expanded menu is triggered when a user selects the More Menu Item from the icon menu
  • This menu displays full text, shortcut keys, and checkboxes/radio buttons.
  • It does not, however, display icons
  • Pressing back from the expanded menu returns you to the icon menu



Submenus

  • its items are displayed in a floating menu dialog box
  • you can’t add a submenu to a submenu
  • icons are not displayed in the submenu
  • Pressing the back button closes the floating window




Defining an Activity Menu
  • To define a menu for an Activity, override its onCreateOptionsMenu handler. This method is triggered the first time an Activity’s menu is displayed.
  • Always call through to the superclass implementation, as it automatically includes additional system menu options where appropriate
  • Use the add method on the Menu object to populate your menu
  • For each Menu item, you must specify
    • A group value to separate Menu Items for batch processing and ordering.
    • A unique identifier for each Menu Item, important for determining which Menu Item was pressed
    • An order value that defines the order in which the Menu Items are displayed.
    • The Menu Item display text, either as a character string or as a string resource
  • When you have finished populating the Menu return true.

static final private int MENU_ITEM = Menu.FIRST;

@Override
public boolean onCreateOptionsMenu(Menu menu) {
  super.onCreateOptionsMenu(menu);

  // Group ID
  int groupId = 0;
  // Unique menu item identifier. Used for event handling.
  int menuItemId = MENU_ITEM;
  // The order position of the item
  int menuItemOrder = Menu.NONE;
  // Text to be displayed for this menu item.
  int menuItemText = R.string.menu_item;

  // Create the menu item and keep a reference to it.
  MenuItem menuItem = menu.add(groupId, menuItemId, 
                               menuItemOrder, menuItemText);

  return true;
}

Dynamically Updating Menu Items

  • By overriding your Activity’s onPrepareOptionsMenu method you can modify a Menu
  • This lets you dynamically disable enable Menu Items, set visibility, and modify text.


@Override
public boolean onPrepareOptionsMenu(Menu menu) {
  super.onPrepareOptionsMenu(menu);

  MenuItem menuItem = menu.findItem(MENU_ITEM);

  [ ... modify menu items ... ]

  return true;
}

Handling Menu Selections

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;
}

No comments:

Post a Comment