Options Menu

 

Press the Android menu button.

Different appearances

The first snapshot is Android < 3.0, SDK < 11. The menu items will line up correctly only if all of them have icons or if none of them have icons.

The second snapshot is Android ≥ 3.0, SDK ≥ 11. The icons will appear in the action bar at the top of the screen if there is room for them and if their showAsAction attribute has been set. See the showAsAction attribute of the item element in a menu.xml file, and the setShowAsAction method of class MenuItem. In this example we have not turned on showAsAction. The items that do not appear in the action bar are displayed in the overflow menu menu at the bottom of the screen. Apparently, the overflow menu does not show icons.

Different times

Android < 3.0, SDK < 11. onCreateOptionsMenu is called only once, the first time the Android menu button is pressed. Then onPrepareOptionsMenu is called each time the the Android menu button is pressed. If the menu is in two pieces because it has more than six items, onPrepareOptionsMenu is called for each piece.

Android ≥ 3.0, SDK ≥ 11. onCreateOptionsMenu and onPrepareOptionsMenu are called at the start of the app, even before the user presses the Android Menu button, because menu items may be displayed in the action bar. onCreateOptionsMenu and onPrepareOptionsMenu are also called when invalidateOptionsMenu is called. Finally, onPrepareOptionsMenu is called when the user presses the Android Menu button and when there are items that have to go in the overflow menu.

Two different ways to get the icons

The first icon (the plus sign) belongs to the operating system and is the file android-sdks/platforms/android-14/data/res/drawable-hdpi/ic_menu_add.png on your disk. The second icon (the telephone) belongs to the app and is the file ic_menu_call.png in the app’s res/drawable-hdpi directory. See Creating a Menu Resource, Menu Resource, Menu Icons.

Options Menu vs. ActionBar

In Android ≥ 3.0, menu items can be displayed in the action bar at the top of the screen. The action bar is visible as soon as the activity starts, so onCreateOptionsMenu is called right after the activity is created. In Android < 3.0, onCreateOptionsMenu is not called until the user presses the Android menu button.

To display a menu item in the action bar in Android ≥ 3.0, use the android:showAsAction attribute of the item element, or call the setShowAsAction method of a menu item object. Menu items that are not in the action bar will be shown in the overflow menu.

Source code in OptionsMenu.zip

  1. OptionsMenuActivity.java: onCreateOptionsMenu and the MenuInflater; onOptionsItemSelected.
  2. R.java: the numbers returned by getItemId are static final fields in class R.id in the file R.java.
  3. main.xml: a LinearLayout containing a TextView.
  4. optionsmenu.xml: a menu element containing item elements.
  5. AndroidManifest.xml

Create the project

In the Package Explorer pane of Eclipse, control-click on the res folder and say New → Folder.
Folder name: menu
Finish
.

Highlight the new menu folder and say
File → New → File
File name: optionsmenu.xml
Finish

ApiDemos/App/Menu/Inflate from XML

Broken in Android 14. onCreateOptionsMenu in MenuInflateFromXml.java is called as soon as the Activity is created in Android ≥ 3.0. This disables the Spinner, so we’re stuck with “Title only”.

  1. MenuInflateFromXml.java
  2. title_only.xml
  3. title_icon.xml
  4. submenu.xml: press Emotions.
  5. groups.xml
  6. checkable: a checkable submenu.
  7. shotrtcuts.xml: keyboard shortcuts (e.g., Menu+f)
  8. strings.xml

Things to try

  1. Add the four following attributes to the four items in optionsmenu.xml, one attribute per item. withText didn’t do anything until I switched to landscape.
    	android:showAsAction="always|withText"
    
    	android:showAsAction="always"
    
    	android:showAsAction="always"
    
    	android:showAsAction="ifRoom"