Android user interface-menu (menus 1)

Source: Internet
Author: User

In many types of applications, menus are commonly used user interface components. To provide a friendly and consistent user experience, you should use menu APIs to display the activity action and other options to the user.

Since android3.0 (API level 11), Android devices no longer need to provide a dedicated menu button, android applications will be freed from the dependency on the traditional six menu panels. Instead, they provide an operation bar for users to display common user actions.

Although the design and user experience for some menu items have changed, the definition of a group of actions and options is still based on menu APIs. This guide shows how to create three basic types of menus and actions that can be demonstrated in all android versions.

Option menu and operation bar

The options menu is the main menu set for the activity. It is the place where you place actions that have a global impact on your application, such as searching, writing emails, and setting.

If you develop applications for android2.3 or a lower version, you need to press the menu button to display the option menu panel.

In android3.0 or later versions, items originating from the option menu are displayed through the action bar, which is composed of the action items on the screen and the remaining options. Since android3.0, the menu button is discarded (some devices do not have this button at all). Therefore, you should use the operation bar to provide access to actions and other options.

Context Mode

A context menu is a floating menu that is displayed only when you execute the long-Click Event on an element. It provides actions that affect the selected content or context framework.

When developing applications for android3.0 and later versions, you should use the context action mode to ensure the actions of the selected content. This mode displays the action items that affect the selected content in a horizontal bar at the top of the screen, and allows you to select multiple projects.

Pop-up menu

A pop-up menu displays the project list in a vertical list, which is placed beside the view object that calls this menu. It is advantageous to provide action display for the specified content or to provide options for the second part of a command. The action in the pop-up menu should not directly affect the corresponding content. On the contrary, the pop-up menu is designed to expand the action in the relevant content area of the activity.

Define a menu in XML

For all menu types, Android provides a standard XML format to define menu items. You should define a menu and all its projects in an XML menu resource, instead of creating a menu in your activity code. Then you can load the menu resource as a menu object to the activity or fragment object.

Using menu resources is a good choice for the following reasons:

1. It is easier to see the menu structure in the XML file;

2. It separates the menu content and application behavior code;

3. It allows you to create optional menu configurations for different platform versions, screen sizes, and other configurations used by the Application resource framework.

To define a menu, you must create an XML file in the Res/menu/directory of your project and use the following elements to build the menu:

<Menu>

Define a menu that contains a menu item. The <menu> element must be the root node of the file and can have multiple <item> and <group> elements.

<Item>

Create a menuitem object, which represents a separate project in a menu. To create a sub-menu, this element can contain a nested <menu> element.

<Group>

An optional non-visible container for <item> elements. It allows you to classify menu items so that they share attributes such as activity status and visibility.

The following is a menu named game_menu.xml:

<? XML version = "1.0" encoding = "UTF-8"?>
<Menu xmlns: Android = "http://schemas.android.com/apk/res/android">
<Item Android: Id = "@ + ID/new_game"
Android: icon = "@ drawable/ic_new_game"
Android: Title = "@ string/new_game"
Android: showasaction = "ifroom"/>
<Item Android: Id = "@ + ID/help"
Android: icon = "@ drawable/ic_help"
Android: Title = "@ string/help"/>
</Menu>

<Item> the element supports the following attributes used to define the appearance and behavior of a menu item:

Android: ID

The unique resource ID of a menu item. When you select this menu item, the application can use this ID to identify it.

Android: icon

Point to a resource that can be traced and used as an icon for this menu item.

Android: Title

A string that is used as the menu title.

Android: showasaction

Specifies the time and method when this menu item should be displayed as an action item in the action bar.

The above are only the most important attributes you should use, but there are still some valid attributes. For more information about all menu attributes, see "menu resources.

You can add a sub-menu to any item in the menu (except the sub-menu) and add the <menu> element as a sub-element of the <item> element. When there are many functions in an application that can be organized into a topic, sub-menus are helpful, just like projects (such as file, edit, and view) in the menu bar of a PC application, for example:

<? XML version = "1.0" encoding = "UTF-8"?>
<Menu xmlns: Android = "http://schemas.android.com/apk/res/android">
<Item Android: Id = "@ + ID/file"
Android: Title = "@ string/file">
<! -- "File" submenu -->
<Menu>
<Item Android: Id = "@ + ID/create_new"
Android: Title = "@ string/create_new"/>
<Item Android: Id = "@ + ID/Open"
Android: Title = "@ string/Open"/>
</Menu>
</Item>
</Menu>

To Use menus in an activity, you need to use the menuinflater. Inflate () method to load menu resources (convert XML resources into programmable objects ). In the following sections, you will see how to create a menu of each type.

Create an option menu

The option menu should contain actions and other options related to the context environment of the current activity, such as "Search", "email writing", and "setting.

The position of the Project displayed on the screen in the option menu depends on the Android platform version that your application depends on:

1. if your application depends on android2.3.x (API level 10) or a lower version, when you press the menu button, your options menu will be displayed at the bottom of the screen, as shown in 1: when a menu is opened, the icon menu is displayed first, which is held by six menu items. If your menu contains more than 6 menu items, the adroid will be placed in the sixth project and the remaining projects will be placed in the overflow menu, you can select more menu items to open other menu items.

Figure 1. Optional menu of the browser on android2.3

2. If your application depends on android3.0 (API level 11) or a later version, the menu item can be effective in the action bar. By default, the system will place all items in the Action overflow. You can see the action overflow icon on the right of the action bar (or, if the menu button is valid, press the menu button of the device ). To allow quick access to important actions, you can add the Android: showasaction = "ifroom" attribute setting to the <item> element to display some menu items in the Operation bar.

Note: Even if your application does not depend on android3.0 or a later version, you can build your own action bar to simulate similar effects.

Figure 2. The action bar of the honeycomb gallery application displays the navigation label and camera action items (added to the action overflow button)

Honeycomb gallery application URL:

Http://developer.android.com/resources/samples/HoneycombGallery/index.html

You can declare an option menu item for both the activity subclass and the fragment subclass. If both activity and fragment declare option menu items, they are combined in the UI. The option menu of activity is displayed first, and then the option menu of fragment is displayed in the order that each fragmeng is added. If needed, you can use the Android: orderincategory attribute in each <item> element to re-specify the order of option menus.

To specify an option menu for an activity, you must override the oncreateoptionsmenu () method (fragment also provides their own oncreateoptionsmenu () method ). In this method, you can load the menu resources (defined in the XML file) to the menu object provided by this callback method. For example:

@ Override
Public Boolean oncreateoptionsmenu (menu ){
Menuinflater Inflater =
Getmenuinflater ();
Inflater. Inflate (R. Menu. game_menu, menu );
Return true;
}

You can also use the add () method to add menu items, and use the finditem () method to obtain the menu items for modifying properties using menuitem APIs.

If your application depends on android2.3.x or a lower version, the system will call the oncreateoptionsmenu () method to create the option menu when the user opens this menu for the first time. If your application depends on android3.0 or a later version, the system will call the oncreateoptionsmenu () method when starting the activity to display the option menu in the action bar.

Process click events

When you select a menu item in the option menu (including actions in the action bar), the system will call the onoptionsitemselected () method of the activity. This method transmits the selected menu item as a parameter. You can identify a menu item by calling the getitemid () method. This method returns the unique ID of the menu item of the object (this ID is defined in the Android: Id attribute of the menu resource, or an integer passed to the add method ). You can match this ID with a known menu item to perform the corresponding action, such:

@ Override
Public Boolean onoptionsitemselected (menuitem item ){
// Handle item selection
Switch (item. getitemid ()){
Case R. Id. new_game:
Newgame ();
Return true;
Case R. Id. Help:
ShowHelp ();
Return true;
Default:
Return super. onoptionsitemselected (item );
}
}

If a menu item is processed successfully, true is returned. If you have not processed the menu items, you should call the onoptionsitemselected () method of the parent class (the default implementation returns false ).

If your activity includes fragment, the system first calls the onoptionsitemselected () method of the activity, and then calls the onoptionsitemselected () method of each fragment (according to the order in which each fragment is added ), until one of them returns true, or all the fragment onoptionsitemselected () methods are called.

Tip:Android3.0 uses the Android: onclick attribute to add the ability to define on-click behaviors for menu items in an XML file. This attribute value must be a method name defined by activity to use the menu. This method must be public and receive a single menuitem object parameter. When the system calls this method, it will be used to pass the selected menu item. For more information and examples, see the "menu resources" document.

Tip:If your application contains multiple activities and some of them provide the same option menu, you can create an oncreateoptionsmenu () and onoptionitemselected () there is no other implemented activity class than the method, and each activity inherits the activity, so that the sharing of the same option menu is realized. In this way, you can manage only one set of codes that process menu actions, and each subclass inherits this menu action. If you want to add a menu item to the activity subclass, rewrite the oncreateoptionsmenu () method of the activity. when adding a new menu item using the add () method, you must call super. oncreateoptionsmenu (menu) method to create an initial menu item. You can also rewrite the behavior of the parent class for independent menu items.

Change menu items during running

After the system calls the oncreateoptionsmenu () method, it retains an instance of the menu you have assembled. The oncreateoptionsmenu () method is not called again unless the menu is invalid for some screens. Therefore, you should only use the oncreateoptionsmenu () method when creating the initial menu status, and do not change it during the activity lifecycle.

If you want to modify the option menu based on the events that occur during the activity lifecycle, you can do this in the onprepareoptionsmenu () method. This method passes the existing menu object to you so that you can modify it, such as adding, deleting, or disabling menu items. (Fragment also provides an onprepareoptionsmenu () callback method .)

In android2.3.x and earlier versions, the onprepareoptionsmenu () method is called every time you open the option menu (press the menu button.

In Android and later versions, when a menu item is displayed in the action bar, the option menu is always open. When an event occurs and you want to perform a Menu update action, you must call the invalidateoptionmenu () method to request the system to call the onprepareoptionsmenu () method.

Note:You will never change the option menu item based on the view object in the current focus. In Touch Screen mode, the view object does not require focus, so you will never use focus as the basis for modifying menu items in the option menu. If you want to provide a context-sensitive menu for a view object, use the context menu.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.