Android Menu-menu

Source: Internet
Author: User

The menu is a common user interface element, and there are 3 types of menus available in the Android system.

(1) Options Menu: The menu displayed on the Device by menu key.

(2) context menu: Long press a specific interface view display, with the specific view bound together, similar to the right mouse button on the PC menu.

(3) submenu (Sub menu): Both of the above menus can be added to a submenu, but a submenu cannot be nested in a sub-menu.

Let's use an example to illustrate the basic usage of 3 kinds of menus.

The XML resource file for the menu is placed under the Res/menu directory.


1. <?xml version= "1.0" encoding= "Utf-8"?>

2. <menu xmlns:android= "Http://" >

3. <item android:id= "@+id/optionitem1"

4. android:icon= "@drawable/icon"

5. android:title= "option 1"/>

6. <item android:id= "@+id/optionitem2"

7. android:icon= "@drawable/ic_launcher"

8. android:title= "Option 2"/>

9. <item android:id= "@+id/optionitem3"

android:icon= "@drawable/icon"

android:title= "submenu" >


<item android:id= "@+id/subitem1"

Android:title= "Sub-menu item 1"/>

<item android:id= "@+id/subitem2"

Android:title= "@string/Sub-menu item 2"/>




In this menu resource file, 3 menu items are defined, with IDs optionitem1, OPTIONITEM2, and Optionitem3 respectively. Each menu item item has its own ID, title, and icon.

The Optionitem3 menu item in line 9th to 18th adds the sub-menu structure shown in line 12th to 17th, in addition to the ID, title, and icon. The submenu item has only ID and title and cannot set icon.

The context menu resource file.


1. <?xml version= "1.0" encoding= "Utf-8"?>

2. <menu xmlns:android= "Http://" >

3. <item

4. android:id= "@+id/contextitem1"

5. Android:title= "context menu subkey 1" >

6. </item>

7. <item

8. android:id= "@+id/contextitem2"

9. Android:title= "context menu subkey 2" >

Ten. </item>


Android:id= "@+id/contextitem3"

Android:title= "context menu subkey 3" >



The context menu is formatted like a submenu, and the item item does not have an icon property.

The Java source code for the menu example is as follows.

1. @Override

2. public void OnCreate (Bundle savedinstancestate) {

3. Super.oncreate (savedinstancestate);

4. Setcontentview (R.layout.main);

5. GridView GridView;

6. string[] titles={"Zhao 1", "Qian 2", "Zhang San", "John Doe", "Harry"};

7. int buf=r.drawable.sample;

8. int[] Resids={buf,buf,buf,buf,buf};

9. gridview= (GridView) This.findviewbyid (;

Gridview.setadapter (New Myadapter (Titles,resids));

Registerforcontextmenu (GridView);

Gridview.setonitemclicklistener (New Adapterview.onitemclicklistener () {

13 ...});


. public void Oncreatecontextmenu (ContextMenu menu,view

V,contextmenu.contextmenuinfo menuinfo) {

Super.oncreatecontextmenu (Menu,v,menuinfo);

Getmenuinflater (). Inflate (,menu);



public boolean oncontextitemselected (MenuItem item) {

super.oncontextitemselected (item);

Switch (Item.getitemid ()) {


Toast.maketext (This, "context menu subkey 1", Toast.length_short). Show ();



Toast.maketext (This, "context menu subkey 2", Toast.length_short). Show ();



Toast.maketext (This, "context menu subkey 3", Toast.length_short). Show ();





return super.onoptionsitemselected (item);


Panax Notoginseng. @Override

. public boolean Oncreateoptionsmenu (Menu menu) {

Super.oncreateoptionsmenu (menu);

Getmenuinflater (). Inflate (, menu);

A. return true;



. public boolean onoptionsitemselected (MenuItem Item)

45. {

Switch (Item.getitemid ())

47. {


Toast.maketext (This, "click Option 1", Toast.length_short). Show ();

. return true;


Toast.maketext (This, "click Option 2", Toast.length_short). Show ();

. return true;


Toast.maketext (This, "click Option 3", Toast.length_short). Show ();

. return true;


Toast.maketext (This, click Sub-menu option 1, Toast.length_short). Show ();

. return true;


Toast.maketext (This, "Click submenu option 2", Toast.length_short). Show ();

. return true;

. Default:

. return super.onoptionsitemselected (item);


Myadapter public class extends Baseadapter {

67 ...}

In line 11th Registerforcontextmenu (GridView), we register a context menu for the GridView, and a context menu pops up when the Itemview in the GridView receives a long press event.

Line 14th to 18th we rewrite Oncreatecontextmenu, the callback method that creates the context menu. This method is called every time the context menu triggers the display.

Line 17th We use the Getmenuinflater (). Inflate method to load the menu resource XML file ContextMenu into the menu object, which is the context menu object passed in by the Oncreatecontextmenu callback function.

The Oncontextitemselected method in line 19th to 36th is the system callback method when the user selects an option in the context menu, and the system passes in the MenuItem object selected by the user. by Item.getitemid () We know the ID number of the menu item selected by the user. Here we use Toast to display the user's selection information.

Line 37th to 42nd Oncreateoptionsmenu is the callback method that creates the Options menu, which is called the first time the option menu is created. We have rewritten this method to load the menu resource Optionmenu into the Options menu.

Line 43rd to 65th is the item response callback function for the Options menu, and we also get the option ID number by Item.getitemid ().

Run 10-14, figure 10-15, figure 10-16, and figure 10-17.


▲ Figure 10-14 Options menu diagram ▲ Figure 10-15 Click on the icon after option 2


Figure 10-16 Sub-menu diagram ▲ Figure 10-17 context menu diagram

Android Menu-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: 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.