Android menu details 2: option menu

Source: Internet
Author: User

Create an option menu

The option menu should contain basic activity actions and required navigation entries (for example, a menu item set to open the program ). option menu items have two different options: one is the menu item button, and the other is the action bar (in Android 3.0 and later versions ).

Figure 1: option menu in the browser


Figure 2. Action bar in the email program, with two actions and an overflow menu

When running in Android 2.3 and earlier versions, the option menu appears at the bottom of the screen, as shown in figure 1. when you open the option menu, the icon menu is first displayed. It has six menu items. If you add more than six menu items, the system places the sixth menu item and the subsequent menu item in the overflow menu. You can open them through the "more" menu item.

In Android 3.0 and later versions, the option menu item is placed on the action bar. the action bar is located at the top of the activity, where the traditional title bar is located. by default, all menu items from the option menu are placed in the overflow menu. you can click the menu icon on the right of the action bar to open it. however, you can also put the menu item as "action items" directly on the action bar, as shown in figure 2.

When the system creates the option menu for the first time, it calls your activity method oncreateoptionsmenu (). override this method and create an instance for the input parameter menu. menu is created through an inflate menu resource, as follows:

@ Override <br/> Public Boolean oncreateoptionsmenu (menu) {<br/> menuinflater Inflater = getmenuinflater (); <br/> Inflater. inflate (R. menu. game_menu, menu); <br/> return true; <br/>}You can also generate a menu in the Code and add a menu item using Add.

Note: In Android 2.3 and earlier versions, when you open the option menu for the first time, the system calls oncreateoptionsmenu () to create the option menu. However, in Android 3.0 and later versions, when an activity is created, the system creates an option menu to create an action bar.

Respond to user actions

When you select a menu item (including the action item on the action bar), the system will call your activity method onoptionsitemselected (). this method will input the selected menu item in the parameter. you can call the getitemid () method to locate this menu item. This method returns the unique ID of the menu item (defined by the Android: Id attribute in the menu resource file or in the call Method Add () an integer ). you can use known menu items to match this ID and perform relevant actions, for example:

@ Override <br/> Public Boolean onoptionsitemselected (menuitem item) {<br/> // handle item selection <br/> switch (item. getitemid () {<br/> case R. id. new_game: <br/> newgame (); <br/> return true; <br/> case R. id. help: <br/> showHelp (); <br/> return true; <br/> default: <br/> return Super. onoptionsitemselected (item); <br/>}< br/>}In this example, getitemid () gets the ID of the selected menu item and compares it with all menu IDs in the resource file in the switch statement. If the menu item is successfully processed in the switch statement, true is returned to indicate that the selected menu item is processed. Otherwise, the default statement will pass the menu item to the parent class. Maybe the parent class will process this menu item (if you directly derive from activity, the parent class will return false, however, it is a good habit to pass unprocessed menu items to the parent class rather than directly returning false .)

In addition, Android 3.0 adds the ability to define the click behavior of menu items in the menu resource XML file, which is defined through the Android: onclick attribute. So you do not need to implement onoptionsitemselected (). using the Android: onclick attribute, you can specify a method to call when the menu item is selected. your activity must implement the method specified in the property Android: onclick. It accepts a menuitem parameter-The menu selected when the system calls this method is passed in from this parameter.

TIPS: If your program contains multiple activities and they provide the same option menu, consider creating an activity class that only implements oncreateoptionsmenu () and onoptionsitemselected, then, all the activities that provide the same menu options are derived from this class. in this way, you only need to manage a group of codes for the class's children and grandchildren.

If you want to add a menu item to the grandson activitie, you only need to override oncreateoptionsmenu (). call super. oncreateoptionsmenu (menu), so the original menu is created, and then you can use the menu method. add () to add a single dish. You can also override the parent class method to create other menu items.

Change menu items during running

Once the activity is created, the oncreateoptionsmenu () method will be called only once (as mentioned earlier ). the system will save and reuse this menu until your activity is destroyed. what if you want to change the menu after it is created? You must override onprepareoptionsmenu (). it will be passed to the instance of the menu you have created. this function is used when you want to delete, add, disable, or enable menu items based on the application status.

In Android 2.3 and earlier versions, the system calls onprepareoptionsmenu () each time the option menu is opened ().

In Android 3.0 and later versions, you must call the invalidateoptionsmenu () method before you want to update the menu, because the menu is always open. the system will call onprepareoptionsmenu (), so you can change the menu item.

Note: You should never change the option menu of the current view with focus. when you are in the touch mode (you have not used a trackball or direction key), views cannot get the focus, so you can never modify the menu items of the option menu based on the focus. if you want to provide context-sensitive Menu items for the view, use context menu.

If you are developing apps on Android 3.0 or later, read the action bar Development Guide.

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.