Cover by Marko Gargenta

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

O'Reilly logo

The Options Menu

The options menu is an Android user interface component that provides standardized menus to applications. The menus appear at the bottom of the screen when the user presses the Menu button on the device.

To add support for the options menu to an application, we need to do the following:

  1. Create the menu.xml resource where we specify what the menu consists of.

  2. Add onCreateOptionsMenu() to the activity that should have this menu. This is where we inflate the menu.xml resource.

  3. Provide handling of menu events in onOptionsItemSelected().

The Menu Resource

We start by defining the menus in an XML resource for the options menu. Just like with other Android XML files, we can use the little a+ icon in the Eclipse toolbar or choose FileNew…Android XML to launch the New Android XML File dialog. In this dialog, enter “menu.xml” in the file field, and for Type, select Menu. Click the Finish button, and Eclipse will create a new folder called /res/menu that contains the menu.xml file and will open this file in the developer-friendly view (see Figure 7-3).

Menu.xml in developer-friendly view

Figure 7-3. Menu.xml in developer-friendly view

In this view, you can click on AddItem, which will add a new menu item to your menu. In the Attributes section on the right, you can see over a dozen attributes that we can set for this menu item. Just like before, not all attributes are equally important:

Id

The unique identifier of this ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required