Menus can be used to display additional options that do not have to appear in the main interface. In the Android framework, there are 2 main types of menus:
Options menu--Displays information about the current activity. Use the menu key to trigger the menus.
Context menu--Displays information related to one view in the activity. Long press a view to trigger this menu.
On the left is the option menu, and the right side is the context menu.
Before using both menus, let's start with two helper methods. A list of options to populate the menu, and another to handle the menu's selected events.
1. Create a project: Menus.
2. Code in the Menusactivity.java.
The public class Menusactivity extends activity {/** called the ' when ' is the ' The activity ' is the ' the '---' @Override
public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
} private void CreateMenu (Menu menu) {Menu.setqwertymode (true);
MenuItem mnu1 = menu.add (0, 0, 0, "Item 1");
{mnu1.setalphabeticshortcut (' a ');
Mnu1.seticon (R.drawable.ic_launcher);
MenuItem mnu2 = menu.add (0, 1, 1, "Item 2");
{mnu2.setalphabeticshortcut (' B ');
Mnu2.seticon (R.drawable.ic_launcher);
MenuItem mnu3 = menu.add (0, 2, 2, "Item 3");
{mnu3.setalphabeticshortcut (' C ');
Mnu3.seticon (R.drawable.ic_launcher);
MenuItem mnu4 = menu.add (0, 3, 3, "Item 4"); {mnu4.setalphabeticshortcut (' d ');
} menu.add (0, 4, 4, "Item 5");
Menu.add (0, 5, 5, "Item 6");
Menu.add (0, 6, 6, "Item 7");
Private Boolean Menuchoice (MenuItem item) {switch (Item.getitemid ()) {case 0:
Toast.maketext (This, "Your clicked on Item 1", Toast.length_long). Show ();
return true;
Case 1:toast.maketext (this, "Your clicked on Item 2", Toast.length_long). Show ();
return true;
Case 2:toast.maketext (this, "Your clicked on Item 3", Toast.length_long). Show ();
return true;
Case 3:toast.maketext (this, "Your clicked on Item 4", Toast.length_long). Show ();
return true; Case 4:toasT.maketext (This, "Your clicked on Item 5", Toast.length_long). Show ();
return true;
Case 5:toast.maketext (this, "Your clicked on Item 6", Toast.length_long). Show ();
return true;
Case 6:toast.maketext (this, "Your clicked on Item 7", Toast.length_long). Show ();
return true;
return false; }
}
In the above example, there are 2 main methods: CreateMenu () and Menuchoice ().
CreateMenu () method, receives a parameter of a menu type, and adds some menu items to the parameter.
To add a menu item, we will create an instance of the MenuItem class and then use the Add () method of the Menu object.
MenuItem mnu1 = menu.add (0, 0, 0,
"Item 1");
{
mnu1.setalphabeticshortcut (' a ');
Mnu1.seticon (R.drawable.ic_launcher);
}
The Add () method has 4 parameters, which I Yi Yilai explain:
groupid--If a menu item is in a group, this is the identifier for the group. If GroupID is 0, the menu item is not in any group.
Unique identification of the itemid--menu item.
order--the order in which menu items are displayed.
title--the display text for the menu item.
Alternatively, we can use the Setalphabeticshortcut () method to set a shortcut key. This allows you to use the keyboard to select a menu item.
The SetIcon () method lets you set a picture for a menu item.
Finally, the Menuchoice () method receives a MenuItem type parameter and then checks its ID to determine which menu item is selected, and pops up a toast notification to tell the user which menu item he has selected.