Utilizing the Android Options Menu

You may have noticed the three button icon in the lower right-hand corner of the window, the options button, that Android provides to you within the context of an application. For many applications, this provides the options available for the current view. The options provided within the current view are customizable from within the activity. The Activity has two methods that work with the options menu, which are:

– OnCreateOptionsMenu
– OnOptionsItemSelected

The first method creates the instance of the menu, while the second method actually controls what happens when a menu item is selected. To begin, let’s create a menu:

public override bool OnCreateOptionsMenu (IMenu menu)
{
	menu.Add (0, 0, 0, "Fragment Demo");
	menu.Add (0, 1, 1, "Maps Demo");
	menu.Add (0, 2, 2, "Webs Demo");

	return true;
}

Here we have some menu items being added to the menu. Each of the items is a textual item that points to a specific demo in a demo app I’ve created. When you select the menu item, the actual text of these items appears in the bottom menu (note that images can also be used within the menu item). This method has many overrides, but the override I used has two methods, described as the following:

– groupID – The ID for the grouping of menu items.
– itemID – The ID of an item for the item in the list; this is a generic ID you assign to your item, used next.
– sortOrder – The order to display within the list.
– text – The text of the item.

On selecting an item, the ItemID is passed to the caller and available for the following type of logic:

public override bool OnOptionsItemSelected (IMenuItem item)
{
	if (item.ItemId == 0)
		StartActivity (typeof(FragmentDemoActivity));
	else if (item.ItemId == 1)
		StartActivity (typeof(MapsActivity));
	else if (item.ItemId == 2)
		StartActivity (typeof(WebsActivity));
	else
		return base.OnOptionsItemSelected (item);

	return true;
}

Each option starts an activity using the StartActivity method on the given activity. The item ID is used to differentiate between the menu item selected; otherwise, if none are selected, the base behavior is used.

See how simple it is to add items to the options menu? And in case you are wondering, this translates pretty well to the Android equivalent, sort of. From the Android documentation, you can see we have the same two methods; the only differential is that Android uses resource ID’s in place of the item ID’s, and that a menu inflater is used to load the menu items. All the menu inflater actually does is take a given XML file, and transform them into menu items, as we have done above. The process between the two options is pretty similar.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s