Android phones have the menu button which displays a menu with several items that provide navigation or more functionality or settings to your applications

Android has three types of menus
1. Options Menu
2. Context Menu
3. Sub menus

Menu items can be grouped and each menu item can have submenu items.

Options Menu

Each activity has a single menu. It appears when you press the menu button Each activity creates this menu in the callback method onCreateOptionsMenu you can override this method and add items to the menu.
If you press the menu button you will see the menu like this:
public class MenusDemo extends Activity {
    /** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState) {
    public boolean onCreateOptionsMenu(Menu menu)
     return true;

Notice that the onCreateOptionsMenu method returns a Boolean. It should return true for the menu to appear, if returns false the menu will be invisible.

If you add many menu items, more than five items they will appear like this:
public boolean onCreateOptionsMenu(Menu menu)
     menu.add(1, 1, 0, "Item1");
     menu.add(1, 2, 1, "Item2");
     menu.add(1, 3, 2, "Item3");
     menu.add(1, 4, 3, "Item4");
     menu.add(1, 5, 4, "Item5");
     menu.add(2, 6, 0, "Item6");
     menu.add(2, 7, 1, "Item7");
     menu.add(2, 8, 2, "Item8");
     menu.add(2, 9, 3, "Item9");
     menu.add(2, 10, 4, "Item10");
     return true;

When you press on the “more” link you will see the rest of the menu items

The menu.add(int GroupID,int ItemID,int Order,String Title) method has the following parameters:
  1. GroupID: used to group related menu items to apply some configurations on all items of a certain group once at a time (discussed later) .
  2. ItemID: an integer to identify the menu item.
  3. Order: the sort order or the order ID, controls the order of menu items in a menu. Items with lower order id appear first. There are some constants defined for sort order for different kinds of menu items
    Like Menu.CATEGORY_SECONDRY for secondry menu items, Menu.CATEGORY_SYSTEM for system menu items and Menu.CATEGORY_ALTERNATIVE for altenative menu items..
  4. Title: the text of the menu item

We could have written the previous code using the predefined the sort order constants like this:
menu.add(1, 1, Menu.FIRST, "Item1");
     menu.add(1, 2, Menu.FIRST+1, "Item2");
     menu.add(1, 3, Menu.FIRST+2, "Item3");
     menu.add(1, 4, Menu.FIRST+3, "Item4");
     menu.add(1, 5, Menu.FIRST+4, "Item5");
     menu.add(2, 6, Menu.CATEGORY_SECONDARY, "Item1");
     menu.add(2, 7, Menu.CATEGORY_SECONDARY+1, "Item2");
     menu.add(2, 8, Menu.CATEGORY_SECONDARY+2, "Item3");
     menu.add(2, 9, Menu.CATEGORY_SECONDARY+3, "Item4");
     menu.add(2, 10, Menu.CATEGORY_SECONDARY+4, "Item5");

Adding Icons to menu items:
We saw that only the first five items can appear in the options menu and the other items appear in a dialog when you press “more” item.
For the first five items you can add an icon to appear next to the item's title:
menu.add(1, 1, 0, "Blu-Ray").setIcon(R.drawable.bluray);
     menu.add(1, 2, 1, "DVD").setIcon(R.drawable.dvd);
     menu.add(1, 3, 2, "Hard Disk").setIcon(R.drawable.hd);
     menu.add(1, 4, 3, "Sites").setIcon(R.drawable.sites);
     menu.add(1, 5, 4, "USB").setIcon(R.drawable.usb);
     menu.add(2, 6, 5, "Item1");
     menu.add(2, 7, 6, "Item2");
     menu.add(2, 8, 7, "Item3");
     menu.add(2, 9, 8, "Item4");
     menu.add(2, 10, 9, "Item5");

Grouping Menu Items:

We saw that the method we use to add menu items us Menu.Add(GroupID,ItemId,OrderID,Title)
The first parameter is GroupID, what is it ? the answer leads us to grouping Menu Items.

Grouping menu items makes it easy to apply some options on some related menu items by applying these options on all menu items that belong to a group once instead of applying them on each single item at a time.

The actions that can be applied on a group can be:
  • Menu.removeGroup(int GroupID): to remove all menu items belonging to the same group.
  • Menu.setGroupCheckable(int GroupID,Boolean Checakble,Boolean Exclusive): maked a group appear with a check mark beside each item. If the Boolean Exclusive is set to true then the items will appear as if they are in a radiobuttongroup and only one item can be selected at a time. If set to false then they will appear with check boxes beside each item and multiple items can be selected at a time.
  • Menu.setGroupEnabled(int GroupID,Boolean Enabled): sets all items in a group to be enabled/disabled.
  • Menu.setGroupVisible(int GroupID,Boolean Visible): sets all items in a group to be visible/invisible.
Note: the methods mentioned above can be applied only to menu items in the secondary menu (items that appear when you click the more button)

Adding Alpha-Numeric shortcuts to menu Items:
You can add alphabetic or numeric shortcuts to menu items like this:
menu.add(2, 8, 7, "Item3").setNumericShortcut('3');
menu.add(2, 9, 8, "Item4").setAlphabeticShortcut('c');
then setting the querty mode of the menu to true like this:

and that's it for Android's options menu.

In a next post we'll talk about handling options menu events