Android Tutorial

(avery) #1

By : Ketan Bhimani


198 

to provide additional controls, or to configure options. The
OptionsMenu control can contain icons, submenus, and keyboard
shortcuts.

An options menu.

For an options menu to show when a user
presses the Menu button on their device,
you need to override the implementation
of onCreateOptionsMenu() in your Activity.
Here is a sample implementation that gives
the user three menu items to choose from:

public boolean onCreateOptionsMenu(
android.view.Menu menu)
{
super.onCreateOptionsMenu(menu);
menu.add(“Forms”)
.setIcon(android.R.drawable.ic_menu_edit)
.setIntent(new Intent(this,
FormsActivity.class));
menu.add(“Indicators”)
.setIntent(new Intent(this,
IndicatorsActivity.class))
.setIcon(android.R.drawable.ic_menu_info_details);
menu.add(“Containers”)
.setIcon(android.R.drawable.ic_menu_view)
.setIntent(new Intent(this, ContainersActivity.class));
return true;
}


For each of the items that are added, we also set a built-in icon
resource and assign an Intent to each item. We give the item title
with a regular text string, for clarity. You can use a resource
identifier, as well. For this example, there is no other handling or
code needed. When one of these menu items is selected, the
Activity described by the Intent starts.
Free download pdf