Menu Item Options

Android supports most of the traditional Menu Item options you're probably familiar with, including icons, shortcuts, checkboxes, and radio buttons, as listed here:

> Checkboxes and radio buttons Checkboxes and radio buttons on Menu Items are visible in expanded menus and submenus, as shown in Figure 4-9. To set a Menu Item as a checkbox, use the setCheckable method. The state of that checkbox is controlled via setChecked.

A radio button group is a group of items displaying circular buttons, in which only one item can be selected at any given time. Checking one of these items will automatically uncheck any checked item in the same group.

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.

Checkboxes are not visible in the icon menu, so Menu Items that feature checkboxes should be reserved for submenus and items that appear only in the expanded menu. The following code snippet shows how to add a checkbox and a group of three radio buttons.

// Create a new check box item.

menu.add(0, CHECKBOX_ITEM, Menu.NONE, "CheckBox").setCheckable(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 You can specify a keyboard shortcut for a Menu Item using the setshortcut method. Each call to setshortcut requires two shortcut keys, one for use with the numeric keypad and a second to support a full keyboard. Neither key is case-sensitive.

// 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 icon menu does not display shortcuts or checkboxes, so it's often necessary to modify its display text to indicate its state. The setTitleCondensed method lets you specify text to be displayed only in the icon menu.

menuItem.setTitleCondensed("Short Title");

> Icons The icon property is a Drawable resource identifier for an icon to be used in the Menu Item. Icons are displayed only in the icon menu; they are not visible in the extended menu or submenus. You can specify any Drawable resource as a menu icon, though by convention menu icons are generally grayscale and use an embossed style.

menuItem.setIcon(R.drawable.menu_item_icon);

> Menu item click listener An event handler that will execute when the Menu Item is selected.

For efficiency, the use of such an event handler is discouraged; instead, Menu Item selections should be handled by the onOptionsItemSelected handler, as shown later in this section.

Prepared for ASHLEE KABAT, email: [email protected] Order number: 56760408 This PDF is for the purchaser's personal use in accordance with the Wrox Terms of Service and under US copyright as stated on this book's copyright page. If you did not purchase this copy, please visit www.wrox.com to purchase your own copy.

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

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


Post a comment