Press the Android menu button.
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
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.
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.
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
ic_menu_call.png
res/drawable-hdpi
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
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.
OptionsMenuActivity.java
:
onCreateOptionsMenu
and the
MenuInflater
;
onOptionsItemSelected
.R.java
:
the numbers returned by
getItemId
are static final fields in class
R.id
R.java
.main.xml
:
a
LinearLayout
containing a
TextView
.optionsmenu.xml
:
a
menu
element containing
item
elements.AndroidManifest.xml
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
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”.
MenuInflateFromXml.java
title_only.xml
title_icon.xml
submenu.xml
:
press Emotions.groups.xml
checkable
:
a checkable submenu.shotrtcuts.xml
:
keyboard shortcuts (e.g., Menu+f)strings.xml
optionsmenu.xml
,withText
didn’t do anything until I switched to landscape.
android:showAsAction="always|withText"
android:showAsAction="always"
android:showAsAction="always"
android:showAsAction="ifRoom"