Shortcut keys

Quick access shortcut keys using letters and/or numbers can be added to menu items with setAlphabeticShortcut(char) (to set char shortcut), setNumericShortcut(int) (to set numeric shortcut), or setShortcut(char,int) (to set both). Case is not sensitive. For example:

menu.add(0, MENU_QUIT, 0, "Quit") .setAlphabeticShortcut('q');

Now, when the menu is open (or while holding the MENU key), pressing the "q" key will select this item.

This shortcut key will be displayed as a tip in the menu item, below the menu item name (except for items in the Icon Menu).

| Note: Shortcuts cannot be added to items in a Context Menu. Menu item intents

If you've read the Application Fundamentals, then you're at least a little familiar with Android Intents. These allow applications to bind with each other, share information, and perform user tasks cooperatively. Just like your application might fire an Intent to launch a web browser, an email client, or another Activity in your application, you can perform such actions from within a menu. There are two ways to do this: define an Intent and assign it to a single menu item, or define an Intent and allow Android to search the device for activities and dynamically add a menu item for each one that meets the Intent criteria.

For more information on creating Intents and providing your application's services to other applications, read the Intents and Intent Filters document.

Set an intent for a single menu item

If you want to offer a specific menu item that launches a new Activity, then you can specifically define an Intent for the menu item with the setIntent() method.

For example, inside the onCreateOptionsMenu() method, you can define a new menu item with an Intent like this:

MenuItem menuItem = menu.add(0, PHOTO_PICKER_ID, 0, "Select Photo"); menuItem.setIntent(new Intent(this, PhotoPicker.class));

Android will automatically launch the Activity when the item is selected.

Note: This will not return a result to your Activity. If you wish to be returned a result, then do not use setintent(). Instead, handle the selection as usual in the onOptionsMenuitemSelected() or onContextMenuItemSelected() callback and call startActivityForResult().

Dynamically add intents

If there are potentially multiple activities that are relevant to your current Activity or selected item, then the application can dynamically add menu items that execute other services.

During menu creation, define an Intent with the category Intent.ALTERNATIVE_CATEGORY and/or Intent.SELECTED_ALTERNATIVE, the MIME type currently selected (if any), and any other requirements, the same way as you would satisfy an intent filter to open a new Activity. Then call addintentOptions() to have Android search for any services meeting those requirements and add them to the menu for you. If there are no applications installed that satisfy the Intent, then no additional menu items are added.

Note: SELECTED_ALTERNATIVE is used to handle the currently selected element on the screen. So, it should only be used when creating a Menu in onCreateContextMenu() or onPrepareOptionsMenu(), which is called every time the Options Menu is opened.

Here's an example demonstrating how an application would search for additional services to display on its menu.

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

// Create an Intent that describes the requirements to fulfill, to be included // in our menu. The offering app must include a category value of Intent.CATEGORY_ALTERNATIVE.

Intent intent = new Intent(null, getIntent().getData()); intent.addCategory(Intent.CATEGORY_ALTERNATIVE);

// Search for, and populate the menu with, acceptable offering applications. menu.addIntentOptions(

thisClass.INTENT_OPTIONS, // Menu group 0, // Unique item ID (none)

this.getComponentName(), // The current Activity name null, // Specific items to place first (none)

intent, // Intent created above that describes our requirements 0, // Additional flags to control items (none)

null); // Array of MenuItems that corrolate to specific items (none) return true;

For each Activity found that provides an Intent Filter matching the Intent defined, a menu item will be added, using the android:label value of the intent filter as the text for the menu item. The addIntentOptions() method will also return the number of menu items added.

Also be aware that, when addIntentOptions() is called, it will override any and all menu items in the menu group specified in the first argument.

If you wish to offer the services of your Activity to other application menus, then you only need to define an intent filter as usual. Just be sure to include the ALTERNATIVE and/or SELECTED_ALTERNATIVE values in the name attribute of a <category> element in the intent filter. For example:

<intent-filter label="



<category android:




.category.ALTERNATIVE" />

<category android:






read more about writing intent filters in the Intents and Intent Filters document. For a sample application using this technique, see the Note Pad sample code.

" Back to User Interface ! Go to top

Except as noted, this content is licensed under Apache 2.0. For details and restrictions, see the Content License.

Site Terms of Service - Privacy Policy - Brand Guidelines

Was this article helpful?

0 0

Post a comment