diff --git a/docs/html/guide/topics/resources/menu-resource.jd b/docs/html/guide/topics/resources/menu-resource.jd index 33c782b1c0530..d09790bc14e86 100644 --- a/docs/html/guide/topics/resources/menu-resource.jd +++ b/docs/html/guide/topics/resources/menu-resource.jd @@ -12,9 +12,12 @@ parent.link=available-resources.html -

A menu resource defines an application menu (Options Menu, Context Menu, or Sub Menu) that +

A menu resource defines an application menu (Options Menu, Context Menu, or submenu) that can be inflated with {@link android.view.MenuInflater}.

+

For a guide to using menus, see the Creating +Menus document.

+
file location:
@@ -110,12 +113,12 @@ example at the bottom. href="{@docRoot}guide/developing/tools/proguard.html">ProGuard (or a similar tool), be sure to exclude the method you specify in this attribute from renaming, because it can break the functionality.

-

Introduced in API Level HONEYCOMB.

+

Introduced in API Level 11.

android:showAsAction
Keyword. When and how this item should appear as an action item in the Action Bar. A menu item can appear as an action item only when the activity includes an {@link -android.app.ActionBar} (introduced in API Level HONEYCOMB). Valid values: +android.app.ActionBar} (introduced in API Level 11). Valid values:
ValueDescription
ifRoomOnly place this item in the Action Bar if @@ -131,14 +134,14 @@ with other UI in the action bar.

See Using the Action Bar for more information.

-

Introduced in API Level HONEYCOMB.

+

Introduced in API Level 11.

android:actionViewLayout
Layout resource. A layout to use as the action view.

See Using the Action Bar for more information.

-

Introduced in API Level HONEYCOMB.

+

Introduced in API Level 11.

android:actionViewClassName
Class name. A fully-qualified class name for the {@link android.view.View} @@ -149,7 +152,7 @@ more information.

href="{@docRoot}guide/developing/tools/proguard.html">ProGuard (or a similar tool), be sure to exclude the class you specify in this attribute from renaming, because it can break the functionality.

-

Introduced in API Level HONEYCOMB.

+

Introduced in API Level 11.

android:alphabeticShortcut
@@ -277,7 +280,7 @@ public void onGroupItemClick(MenuItem item) { }

Note: The {@code android:showAsAction} attribute is -available only on Android X.X (API Level HONEYCOMB) and greater.

+available only on Android 3.0 (API Level 11) and greater.

diff --git a/docs/html/guide/topics/ui/menus.jd b/docs/html/guide/topics/ui/menus.jd index d1c0ff8b481d8..984bf8fcd202a 100644 --- a/docs/html/guide/topics/ui/menus.jd +++ b/docs/html/guide/topics/ui/menus.jd @@ -7,11 +7,11 @@ parent.link=index.html

In this document

    -
  1. Defining Menus
  2. +
  3. Creating a Menu Resource
  4. Inflating a Menu Resource
  5. Creating an Options Menu
      -
    1. Changing the menu when it opens
    2. +
    3. Changing menu items at runtime
  6. Creating a Context Menu
  7. @@ -21,7 +21,7 @@ parent.link=index.html
  8. Menu groups
  9. Checkable menu items
  10. Shortcut keys
  11. -
  12. Intents for menu items
  13. +
  14. Dynamically adding menu intents
@@ -42,52 +42,60 @@ parent.link=index.html
-

Menus are an important part of an application that provide a familiar interface for the user -to access application functions and settings. Android offers an easy programming interface -for you to provide application menus in your application.

+

Menus are an important part of an activity's user interface, which provide users a familiar +way to perform actions. Android offers a simple framework for you to add standard +menus to your application.

-

Android provides three types of application menus:

+

There are three types of application menus:

Options Menu
-
The primary collection of menu items for an Activity that is associated with the device MENU -key. To provide instant access to select menu items, you can place some items in the Action Bar, if available.
+
The primary collection of menu items for an activity, which appears when the user touches +the MENU button. When your application is running on Android 3.0 or later, you can provide +quick access to select menu items by placing them directly in the Action Bar, as "action items."
Context Menu
-
A floating list of menu items that appears when the user performs a long-press on a View. +
A floating list of menu items that appears when the user touches and holds a view +that's registered to provide a context menu.
Submenu
-
A floating list of menu items that the user opens by pressing a menu item in the Options -Menu or a context menu. A submenu item cannot support a nested submenu.
+
A floating list of menu items that appears when the user touches a menu item that contains +a nested menu.
+

This document shows you how to create each type of menu, using XML to define the content of +the menu and callback methods in your activity to respond when the user selects an item.

-

Defining Menus

+ +

Creating a Menu Resource

Instead of instantiating a {@link android.view.Menu} in your application code, you should define a menu and all its items in an XML menu resource, then inflate the menu -resource (load it as a programmable object) in your application code. Defining your menus in XML is -a good practice because it separates your interface design from your application code (the same as -when you define your Activity -layout).

+resource (load it as a programmable object) in your application code. Using a menu resource to +define your menu is a good practice because it separates the content for the menu from your +application code. It's also easier to visualize the structure and content of a menu in XML.

-

To define a menu, create an XML file inside your project's res/menu/ +

To create a menu resource, create an XML file inside your project's res/menu/ directory and build the menu with the following elements:

<menu>
-
Creates a {@link android.view.Menu}, which is a container for menu items. It must be -the root node and holds one or more of the following elements. You can also nest this element -in an {@code <item>} to create a submenu.
+
Defines a {@link android.view.Menu}, which is a container for menu items. A +<menu> element must be the root node for the file and can hold one or more +<item> and <group> elements.
+
<item>
-
Creates a {@link android.view.MenuItem}, which represents a single item in a menu.
+
Creates a {@link android.view.MenuItem}, which represents a single item in a menu. This +element may contain a nested <menu> element in order to create a submenu.
+
<group>
An optional, invisible container for {@code <item>} elements. It allows you to -categorize menu items so they share properties such as active state and visibility. See Menu groups.
+categorize menu items so they share properties such as active state and visibility. See the +section about Menu groups.
-

For example, here is a file in res/menu/ named game_menu.xml:

+ +

Here's an example menu named game_menu.xml:

 <?xml version="1.0" encoding="utf-8"?>
 <menu xmlns:android="http://schemas.android.com/apk/res/android">
@@ -100,28 +108,33 @@ href="#groups">Menu groups.
 </menu>
 
-

This example defines a menu with two menu items. Each item includes the attributes:

+

This example defines a menu with two items. Each item includes the attributes:

{@code android:id}
-
A resource ID that's unique to the item so that the application can recognize the item when -the user selects it.
+
A resource ID that's unique to the item, which allows the application can recognize the item +when the user selects it.
{@code android:icon}
-
A drawable resource that is the icon visible to the user.
+
A reference to a drawable to use as the item's icon.
{@code android:title}
-
A string resource that is the title visible to the user.
+
A reference to a string to use as the item's title.
-

For more about the XML syntax and attributes for a menu resource, see the There are many more attributes you can include in an {@code <item>}, including some that + specify how the item may appear in the Action Bar. For more information about the XML +syntax and attributes for a menu resource, see the Menu Resource reference.

+

Inflating a Menu Resource

-

You can inflate your menu resource (convert the XML resource into a programmable object) using +

From your application code, you can inflate a menu resource (convert the XML resource into a +programmable object) using {@link android.view.MenuInflater#inflate(int,Menu) MenuInflater.inflate()}. For -example, the following code inflates the game_menu.xml file defined above during the -{@link android.app.Activity#onCreateOptionsMenu(Menu) onCreateOptionsMenu()} callback method, to be -used for the Options Menu:

+example, the following code inflates the game_menu.xml file defined above, during the +{@link android.app.Activity#onCreateOptionsMenu(Menu) onCreateOptionsMenu()} callback method, to +use the menu as the activity's Options Menu:

 @Override
@@ -133,59 +146,47 @@ public boolean onCreateOptionsMenu(Menu menu) {
 

The {@link android.app.Activity#getMenuInflater()} method returns a {@link -android.view.MenuInflater} for the Activity. With this object, you can call {@link +android.view.MenuInflater} for the activity. With this object, you can call {@link android.view.MenuInflater#inflate(int,Menu) inflate()}, which inflates a menu resource into a {@link android.view.Menu} object. In this example, the menu resource defined by game_menu.xml is inflated into the {@link android.view.Menu} that was passed into {@link android.app.Activity#onCreateOptionsMenu(Menu) onCreateOptionsMenu()}. (This callback method for -creating an option menu is discussed more in the next section.)

+the Options Menu is discussed more in the next section.)

Creating an Options Menu

- -

Figure 1. Screenshot of an Options Menu.

+ +

Figure 1. Screenshot of the Options Menu in the +Browser.

- -

The Options Menu is where you should include basic application functions and necessary navigation +

The Options Menu is where you should include basic activity actions and necessary navigation items (for example, a button to open the application settings). Items in the Options Menu are -accessible in two distinct ways: in the Action Bar and in the menu revealed by the MENU -key.

+accessible in two distinct ways: the MENU button or in the Action Bar (on devices running Android 3.0 +or higher).

-

The Action Bar is an optional widget that appears at the top of the activity in place of the -title bar. It can display several menu items that you choose from the Options Menu, but items in -the Action Bar display only an icon (no title text). Users can reveal the other menu items in the -Options Menu with the MENU key.

+

When running on a device with Android 2.3 and lower, the Options Menu appears at the bottom of +the screen, as shown in figure 1. When opened, the first visible portion of the Options Menu is +the icon menu. It holds the first six menu items. If you add more than six items to the +Options Menu, Android places the sixth item and those after it into the overflow menu, which the +user can open by touching the "More" menu item.

-

If you include the Action Bar in your activity, the menu items that are not placed in the Action -Bar can appear in two different styles:

-
-
Action Bar Menu
-
If the device has an extra-large screen ({@code xlarge}), then all items in the Options Menu -that are not placed in the Action Bar are placed into a drop-down list at the right side of the -Action Bar, with icons and title text. The user can reveal the drop-down list by pressing the -drop-down icon in the Action Bar or the MENU key.
-
Standard Options Menu
-
If the device does not have an extra-large screen, then all items in the Options -Menu that are not placed in the Action Bar are placed into the Standard Options Menu at the bottom -of the activity. The user can reveal the standard Options Menu by pressing the MENU key. -

The first visible portion of the Standard Options Menu is called the Icon Menu. -It holds the first six menu items (excluding any added to the Action Bar), with icons and title -text. If there are more than six items, Android adds a "More" item as the sixth menu item and places -the remaining items into the Expanded Menu, which the user can open by selecting "More". The -Expanded Menu displays menu items only by their title text (no icon)

-
-
+

On Android 3.0 and higher, items from the Options Menu is placed in the Action Bar, which appears +at the top of the activity in place of the traditional title bar. By default all items from the +Options Menu are placed in the overflow menu, which the user can open by touching the menu icon +on the right side of the Action Bar. However, you can place select menu items directly in the +Action Bar as "action items," for instant access, as shown in figure 2.

-

When the user opens the Options Menu for the first time, Android calls your Activity's -{@link android.app.Activity#onCreateOptionsMenu(Menu) -onCreateOptionsMenu()} method. Override this method in your Activity -and populate the {@link android.view.Menu} that is passed into the method. Populate the -{@link android.view.Menu} by inflating a menu resource as described in When the Android system creates the Options Menu for the first time, it calls your +activity's {@link android.app.Activity#onCreateOptionsMenu(Menu) +onCreateOptionsMenu()} method. Override this method in your activity +and populate the {@link android.view.Menu} that is passed into the method, +{@link android.view.Menu} by inflating a menu resource as described above in Inflating a Menu Resource. For example:

@@ -197,17 +198,31 @@ public boolean onCreateOptionsMenu(Menu menu) {
 }
 
-

(You can also populate the menu in code, using {@link android.view.Menu#add(int,int,int,int) -add()} to add items to the {@link android.view.Menu}.)

+
+ +

Figure 2. Screenshot of the Action Bar in the Email +application, with two action items from the Options Menu, plus the overflow menu.

+
-

When the user selects a menu item from the Options Menu (including items selected from the -Action Bar), the system calls your Activity's +

You can also populate the menu in code, using {@link android.view.Menu#add(int,int,int,int) +add()} to add items to the {@link android.view.Menu}.

+ +

Note: On Android 2.3 and lower, the system calls {@link +android.app.Activity#onCreateOptionsMenu(Menu) onCreateOptionsMenu()} to create the Options Menu +when the user opens it for the first time, but on Android 3.0 and greater, the system creates it as +soon as the activity is created, in order to populate the Action Bar.

+ + +

Responding to user action

+ +

When the user selects a menu item from the Options Menu (including action items in the +Action Bar), the system calls your activity's {@link android.app.Activity#onOptionsItemSelected(MenuItem) onOptionsItemSelected()} method. This method passes the {@link android.view.MenuItem} that the user selected. You can identify the menu item by calling {@link android.view.MenuItem#getItemId()}, which returns the unique ID for the menu -item (defined by the {@code android:id} attribute in the menu resource or with an integer passed -to the {@link android.view.Menu#add(int,int,int,int) add()} method). You can match this ID +item (defined by the {@code android:id} attribute in the menu resource or with an integer +given to the {@link android.view.Menu#add(int,int,int,int) add()} method). You can match this ID against known menu items and perform the appropriate action. For example:

@@ -229,45 +244,67 @@ public boolean onOptionsItemSelected(MenuItem item) {
 
 

In this example, {@link android.view.MenuItem#getItemId()} queries the ID for the selected menu item and the switch statement compares the ID against the resource IDs that were assigned to menu -items in the XML resource. When a switch case successfully handles the item, it -returns "true" to indicate that the item selection was handled. Otherwise, the default statement -passes the menu item to the super class in +items in the XML resource. When a switch case successfully handles the menu item, it +returns {@code true} to indicate that the item selection was handled. Otherwise, the default +statement passes the menu item to the super class, in case it can handle the item selected. (If you've directly extended the {@link android.app.Activity} -class, then the super class returns "false", but it's a good practice to -pass unhandled menu items to the super class instead of directly returning "false".)

+class, then the super class returns {@code false}, but it's a good practice to +pass unhandled menu items to the super class instead of directly returning {@code false}.)

+ +

Additionally, Android 3.0 adds the ability for you to define the on-click behavior for a menu +item in the menu resource XML, +using the {@code android:onClick} attribute. So you don't need to implement {@link +android.app.Activity#onOptionsItemSelected(MenuItem) onOptionsItemSelected()}. Using the {@code +android:onClick} attribute, you can specify a method to call when the user selects the menu item. +Your activity must then implement the method specified in the {@code android:onClick} attribute so +that it accepts a single {@link android.view.MenuItem} parameter—when the system calls this +method, it passes the menu item selected.

Tip: If your application contains multiple activities and some of them provide the same Options Menu, consider creating -an Activity that implements nothing except the {@link android.app.Activity#onCreateOptionsMenu(Menu) +an activity that implements nothing except the {@link android.app.Activity#onCreateOptionsMenu(Menu) onCreateOptionsMenu()} and {@link android.app.Activity#onOptionsItemSelected(MenuItem) -onOptionsItemSelected()} methods. Then extend this class for each Activity that should share the +onOptionsItemSelected()} methods. Then extend this class for each activity that should share the same Options Menu. This way, you have to manage only one set of code for handling menu actions and each descendant class inherits the menu behaviors.

If you want to add menu items to one of your descendant activities, override {@link android.app.Activity#onCreateOptionsMenu(Menu) -onCreateOptionsMenu()} in that Activity. Call {@code super.onCreateOptionsMenu(menu)} so the +onCreateOptionsMenu()} in that activity. Call {@code super.onCreateOptionsMenu(menu)} so the original menu items are created, then add new menu items with {@link android.view.Menu#add(int,int,int,int) menu.add()}. You can also override the super class's behavior for individual menu items.

-

Changing the menu when it opens

+

Changing menu items at runtime

-

The {@link android.app.Activity#onCreateOptionsMenu(Menu) onCreateOptionsMenu()} method is -called only the first time the Options Menu is opened. The system keeps and re-uses the {@link -android.view.Menu} you define in this method until your Activity is destroyed. If you want to change -the Options Menu each time it opens, you must override the +

Once the activity is created, the {@link android.app.Activity#onCreateOptionsMenu(Menu) +onCreateOptionsMenu()} method is +called only once, as described above. The system keeps and re-uses the {@link +android.view.Menu} you define in this method until your activity is destroyed. If you want to change +the Options Menu any time after it's first created, you must override the {@link android.app.Activity#onPrepareOptionsMenu(Menu) onPrepareOptionsMenu()} method. This passes you the {@link android.view.Menu} object as it currently exists. This is useful if you'd like to remove, add, disable, or enable menu items depending on the current state of your application.

+

On Android 2.3 and lower, the system calls {@link android.app.Activity#onPrepareOptionsMenu(Menu) +onPrepareOptionsMenu()} each time the user opens the Options Menu.

+ +

On Android 3.0 and higher, you must call {@link android.app.Activity#invalidateOptionsMenu +invalidateOptionsMenu()} when you want to update the menu, because the menu is always open. The +system will then call {@link android.app.Activity#onPrepareOptionsMenu(Menu) onPrepareOptionsMenu()} +so you can update the menu items.

+

Note: You should never change items in the Options Menu based on the {@link android.view.View} currently -in focus. When in touch mode (when the user is not using a trackball or d-pad), Views +in focus. When in touch mode (when the user is not using a trackball or d-pad), views cannot take focus, so you should never use focus as the basis for modifying items in the Options Menu. If you want to provide menu items that are context-sensitive to a {@link android.view.View}, use a Context Menu.

+

If you're developing for Android 3.0 or higher, be sure to also read Using the Action Bar.

+ +

Creating a Context Menu

@@ -287,7 +324,7 @@ feature.)

-

For each Activity found that provides an Intent filter matching the Intent defined, a menu -item is added, using the value in the Intent filter's android:label as the +

For each activity found that provides an intent filter matching the intent defined, a menu +item is added, using the value in the intent filter's android:label as the menu item title and the application icon as the menu item icon. The {@link android.view.Menu#addIntentOptions(int,int,int,ComponentName,Intent[],Intent,int,MenuItem[]) addIntentOptions()} method returns the number of menu items added.

@@ -642,14 +670,14 @@ addIntentOptions()}, it overrides any and all menu items by the menu group speci argument.

-

Allowing your Activity to be added to menus

+

Allowing your activity to be added to other menus

-

You can also offer the services of your Activity to other applications, so your +

You can also offer the services of your activity to other applications, so your application can be included in the menu of others (reverse the roles described above).

-

To be included in other application menus, you need to define an Intent +

To be included in other application menus, you need to define an intent filter as usual, but be sure to include the {@link android.content.Intent#CATEGORY_ALTERNATIVE} -and/or {@link android.content.Intent#CATEGORY_SELECTED_ALTERNATIVE} values for the Intent filter +and/or {@link android.content.Intent#CATEGORY_SELECTED_ALTERNATIVE} values for the intent filter category. For example:

 <intent-filter label="Resize Image">
@@ -660,7 +688,7 @@ category. For example:

</intent-filter>
-

Read more about writing Intent filters in the +

Read more about writing intent filters in the Intents and Intent Filters document.

For a sample application using this technique, see the diff --git a/docs/html/images/options_menu.png b/docs/html/images/options_menu.png index ecb9394e6a620..6c499069ed769 100755 Binary files a/docs/html/images/options_menu.png and b/docs/html/images/options_menu.png differ diff --git a/docs/html/images/radio_buttons.png b/docs/html/images/radio_buttons.png index b755e42f5765c..415ccca701a75 100755 Binary files a/docs/html/images/radio_buttons.png and b/docs/html/images/radio_buttons.png differ