Monday, August 25, 2014

Menu Item Options

Menu Item Options

Checkboxes and radio buttons

To set a Menu Item as a checkbox,

  • use the setCheckable method
  • The state of that checkbox is controlled via setChecked

// Create a new check box item.
menu.add(0, CHECKBOX_ITEM, Menu.NONE, "CheckBox").setCheckable(true);

To create a radio button group, 
  • assign the same group identifier to each item and then call Menu.setGroupCheckable, passing in that group identifier and setting the exclusive parameter to true.

// Create a radio button group.
menu.add(RB_GROUP, RADIOBUTTON_1, Menu.NONE, "Radiobutton 1");
menu.add(RB_GROUP, RADIOBUTTON_2, Menu.NONE, "Radiobutton 2");
menu.add(RB_GROUP, RADIOBUTTON_3, Menu.NONE, "Radiobutton 3").setChecked(true);
menu.setGroupCheckable(RB_GROUP, true, true);

Shortcut keys

  • can specify a keyboard shortcut for a Menu Item using the setShortcut method


// Add a shortcut to this menu item, ‘0’ if using the numeric keypad or ‘b’ if using the full keyboard.
menuItem.setShortcut(’0’, ‘b’);

Condensed titles

  • The setTitleCondensed method lets you specify text to be displayed only in the icon menu.

menuItem.setTitleCondensed("Short Title");

Icons

  • Icons are displayed only in the icon menu
  • can specify any Drawable resource as a menu icon

menuItem.setIcon(R.drawable.menu_item_icon);

Menu item click listener

  • An event handler that will execute when the Menu Item is selected (this is discouraged)
  • Menu Item selections should be handled by the onOptionsItemSelected handler

menuItem.setOnMenuItemClickListener(new OnMenuItemClickListener() {
     public boolean onMenuItemClick(MenuItem _menuItem) 
     {
     [ ... execute click handling, return true if handled ... ]
       return true;
     }
});

Intents

  • An Intent assigned to a Menu Item is triggered when the clicking of a Menu Item isn’t handled by either a MenuItemClickListener or the Activity’s onOptionsItemSelected handler. 
  • When the Intent is triggered Android will execute startActivity, passing in the specified Intent.


menuItem.setIntent(new Intent(this, MyOtherActivity.class));

No comments:

Post a Comment