Android Oncreateoptionsmenu () Create menu

Source: Internet
Author: User
Android has three different forms of menus:
1. Option menu (Optinosmenu)
2. Context menu (ContextMenu)
3. submenu (submenu)
One of the most common is the option menu (Optionsmenu), which is displayed at the bottom of the corresponding activity after clicking the menu key.

1.Activity menu Mechanism(similar to the dialog)
Activity has a set of mechanisms to implement menu management, as follows:
1.public boolean Oncreateoptionsmenu (Menu menu)
This method is used to initialize the menu, where the menu parameter is the menu instance that is about to be displayed.
Returns true to show that the menu,false is not displayed;
(invoked only the first time the menu is initialized)

2.public boolean Onprepareoptionsmenu (Menu menu)
After the Oncreateoptionsmenu is executed, the menu is called before it is displayed, and if the menu is already created, it is invoked before the menu is displayed.
Similarly, returning true shows that the menu,false is not displayed;
(This method can dynamically change the state of the menu, such as loading different menus, etc.)

3.public void onoptionsmenuclosed (Menu menu)
Called each time the menu is closed.
( There are three scenarios when the menu is turned off, the Menus button is clicked again, the Back button is clicked, or the user chooses a menu item.

4.public boolean onoptionsitemselected (MenuItem Item)
When a menu item is clicked, it is called, which is the method of listening for items.

With these methods, it can be learned that for an activity, only one menu object can be displayed and monitored at the same time.


2. Add Menu:

can be in Oncreateoptionsmenu or Onprepareoptionsmenu method to add a menu

2.1 Code Additions:
Menu.add (int groupId, int itemId, int order, charsequence title). SetIcon (drawable ID)

The Add () method has four parameters, followed by:

1, the group, if not grouped words to write menu.none,

2, ID, this is very important, Android based on this Id to determine the different menu

3, order, which menu item in front by the size of this parameter determines

4, Text, menu item display text the Add () method returns the MenuItem object, calls its SetIcon () method, sets the icon for the corresponding MenuItem

Example:
public boolean Oncreateoptionsmenu (Menu menu) {
Super.oncreateoptionsmenu (menu);
Menu.add (Menu.none,menu.first+1, 0, "settings"). SetIcon (r.drawable.setting);
return true;
}

2.2 Layout file Add:

Getmenuinflater (). Inflate (R.menu.options_menu, menu);

Invoke the Getmenuinflater () of the activity to get a menuinflater,
Use the Inflate method to load the defined menus in the layout file to the menu object corresponding to the second argument

Example:
@Override
public boolean Oncreateoptionsmenu (Menu menu) {
Super.oncreateoptionsmenu (menu);
Getmenuinflater (). Inflate (R.menu.options_menu, menu);
return true;
}

Layout file:
Establish a menu folder in the Res directory and create a layout file: Options_menu.xml
<?xml version= "1.0" encoding= "Utf-8"?>
<menu xmlns:android= "Http://schemas.android.com/apk/res/android" >
<item android:id= "@+id/menu_setting"android:title=" setting "android:icon=" @drawable/setting "></item>
</menu>


3. Menu item Monitor:
As soon as the menu item in the menu is clicked, onoptionsitemselected (MenuItem item) will be triggered.
Item parameter is a clicked menu item, then you need to determine which item is clicked in this method to achieve a different operation.
For two different ways to add a menu, the method of judgment is a little different, but the essence is the same.

3.1 Code to add a menu to determine the method
@Override
public boolean onoptionsitemselected (MenuItem item) {
super.onoptionsitemselected (item);
Switch (Item.getitemid ())//Itemid of the item being clicked
{
Casemenu.first+1://corresponding ID is the ID set in the Add method
Break
Case MENU.FIRST+2:
Break
}
return true;
}

3.2 Layout file to add a menu to determine the method:
@Override
public boolean onoptionsitemselected (MenuItem item) {
super.onoptionsitemselected (item);
Switch (Item.getitemid ())//Itemid of the item being clicked
{
Caser.id.menu_setting://The ID here is the ID defined in the layout file, acquired in the R.id.xxx method
Break
Case R.id.menu_info:
Break
}
return true;
}

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.