Android Oncreateoptionsmenu () Create Menus menu

Source: Internet
Author: User

Android has three different forms of menus:
1. Options menu (Optinosmenu)
2. Context menu (ContextMenu)
3. Sub-menu (submenu)
The most common of these is the Options menu (Optionsmenu), which is displayed at the bottom of the corresponding activity when you click the menu button.

1.Activity menu mechanism (similar to dialog)
activity has a mechanism for managing menus, as follows:
1.public boolean Oncreateoptionsmenu (Menu menu)
This method is used to initialize the menu, where the menus parameter is the menu instance that is about to be displayed.
Returns true to show that the menu,false is not displayed;
(only called when the menu is initialized for the first time)

2.public BooleanOnprepareoptionsmenu (Menu menu)
In theOncreateoptionsmenu is executed, the menu is called before it is displayed, and if the menu is already created, it is called before the menu is displayed.
the same,returns true to show that the menu,false is not displayed;
(This method can be used to 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.
(The menu is closed in three situations where menu button is clicked again, the Back button is clicked, or the user selects one of the menus.
              
4.public boolean onoptionsitemselected (MenuItem item)
The menu item is invoked when clicked, that is, the method of listening for the food item.
              
with these methods, it can be learned that for activity, only one menu object can be displayed and monitored at the same time.


2. Add a menu:
      can be in Oncreateoptionsmenu orAdd a menu to the Onprepareoptionsmenu method

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

The four parameters of the Add () method are, in turn:

1, groups, if not grouped, then 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 is determined

4, Text, menu item display text

the Add () method returns a 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 additions:
         
getmenuinflater (). Inflate (R.menu.options_menu, menu);

Call Activity's Getmenuinflater () to get a menuinflater,
use the Inflate method to load the defined menu in the Layout file to the menu object corresponding to the second parameter

Example:
@Override
Public Boolean Oncreateoptionsmenu (Menu menu) {
super.oncreateoptionsmenu (menu);
getmenuinflater (). Inflate (R.menu.options_menu, menu);
return true;
    }

Layout file:
set up 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 Monitoring:
onoptionsitemselected (MenuItem item) is triggered whenever a menu item in the menu is clicked
The item parameter is the menu item that is clicked, so it is necessary to determine which item is clicked in this method, thus implementing different actions.
for the two different ways to add a menu, the method of judging is a little different, but the essence is the same.

3.1 How to determine the code to add a menu
@Override
Public Boolean onoptionsitemselected (MenuItem item) {
super.onoptionsitemselected (item);
switch (Item.getitemid ())//Get Itemid of clicked item
        {
Casemenu.first+1://ID is the ID set in the Add method
Break ;
Casemenu.first+2:
Break ;
        }
return true;
    }

3.2 How to determine the layout file Add menu:
@Override
Public Boolean onoptionsitemselected (MenuItem item) {
super.onoptionsitemselected (item);
switch (Item.getitemid ())//Get Itemid of clicked item
        {
Caser.id.menu_setting://The ID here is the ID defined in the layout file, which is obtained by using the R.id.xxx method .
Break ;
Case R.id.menu_info:
Break ;
        }
return true;
    }

Android Oncreateoptionsmenu () Create menus menu details

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.