The official Android documentation suggests not using options menus:
http://developer.android.com/guide/topics/ui/menus.html
"Beginning with Android 3.0 (API level 11), Android-powered devices are no longer required to provide a dedicated Menu button. With this change, Android apps should migrate away from a dependence on the traditional 6-item menu panel and instead provide an action bar to present common user actions."
How to support options menus on earlier Android versions and action bars on newer devices:
http://android-developers.blogspot.co.il/2012/01/say-goodbye-to-menu-button.html
"Set targetSdkVersion to 14 and, if you use the options menu, surface a few actions in the action bar with showAsAction="ifRoom."
I like action bar items and I think they look cool. However, from a usability perspective, options menus are often easier and quicker to use than action bar items especially in mobility situations.
Samsung Galaxy S3 uses lots of options menus in the device and does not provide an action bar on its home screen. That's Samsung weighing in on the options menu or action bar issue. I think some features should use options menus and others are better with action bar items.
Code example.
1. In your strings.xml file, add title strings for each menu item.
Example:
<string name="new_game">New Game</string>
<string name="info">Info</string>
<string name="advanced">Advanced</string>
<string name="connect">Connect</string>
2. Create an XML menu in a menu folder under res.
Example game_menu.xml:
http://developer.android.com/guide/topics/ui/menus.html
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/new_game"
android:title="@string/new_game"
android:showAsAction="never"/>
<item
android:id="@+id/advanced"
android:title="@string/advanced"
android:showAsAction="never"/>
<item
android:id="@+id/connect"
android:title="@string/connect"
android:showAsAction="never"/>
<item
android:id="@+id/info"
android:title="@string/info"
android:showAsAction="never"/>
</menu>
Note: The menu item id and title attributes are required.
android:showAsAction="never" causes the options menu to display in any Android API level.
3. Override the activity's default onCreateOptionsMenu method.
Instantiate a MenuInflater object and pass to it the resource id of your menu.
MenuInflater class: "This class is used to convert XML menu files into Menu objects."
http://developer.android.com/reference/android/view/MenuInflater.html
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.game_menu, menu);
return true;
}
4. Handle menu item click events
http://developer.android.com/guide/topics/ui/menus.html
When the user selects an item from the options menu (including action items in the action bar), the system calls the activity's onOptionsItemSelected() method.
Identify the item by calling getItemId(), which returns the unique ID for the menu item. Use a switch statement to match this ID against known IDs of menu items.
Example:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.new_game:
Toast.makeText(this, "You selected New Game.", Toast.LENGTH_LONG).show();
return true;
case R.id.advanced:
Toast.makeText(this, "You selected Advanced.", Toast.LENGTH_LONG).show();
return true;
case R.id.connect:
Toast.makeText(this, "You selected Connect.", Toast.LENGTH_LONG).show();
return true;
case R.id.info:
Toast.makeText(this, "You selected Info.", Toast.LENGTH_LONG).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
When you successfully handle a menu item, return true.
If you don't handle the menu item (ie default case), you call the superclass implementation of onOptionsItemSelected() which returns false. The default case implies you have inflated a superclass menu using super.createOnOptionsMenu(menu); See discussion below on merging menus with derived classes.
Also see, Action Bar:
https://sites.google.com/site/myrononmobileapps/android-code-snippets/action-bar
Notes:
A) How to change a menu dynamically.
public boolean onCreateOptionsMenu (Menu menu)
"Called once, the first time the options menu is displayed. To update the menu every time it is displayed, see onPrepareOptionsMenu(Menu)."
B) Merging menu items with menu items in a superclass.
In the official Android documentation, "Add the Actions to the Action Bar" [http://developer.android.com/training/basics/actionbar/adding-buttons.html#AddActions] the example shows only #3 below, which is to call return super.onCreateOptionsMenu(menu); as the last statement in the public boolean onCreateOptionsMenu(Menu menu) method.
Here are three possibilities mainly from StackOverflow.
http://stackoverflow.com/questions/10303898/oncreateoptionsmenu-calling-super
1) How to merge menus with a superclass menu.
"Deriving classes should always call through to the base implementation."
// #1 As shown in the above example, shows only this menu. No call to a superclass.
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.mymenu, menu);
return true;
}
// #2 First inserts superclass menu then this subclass menu.
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.createOnOptionsMenu(menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.mymenu, menu);
return true;
}
// #3 First inserts this subclass menu then the superclass menu.
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.mymenu, menu);
return super.createOnOptionsMenu(menu);
}