The code for the action bar items is very similar to the code for the options menu.
https://sites.google.com/site/myrononmobileapps/android-code-snippets/option-menu
Here is how the similarity is described by the official Android documentation:
http://developer.android.com/guide/topics/ui/menus.html
"Although the design and user experience for some menu items have changed, the semantics to define a set of actions and options is still based on the menu APIs."
Code example for text-only items:
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="always"/>
<item
android:id="@+id/advanced"
android:title="@string/advanced"
android:showAsAction="always"/>
<item
android:id="@+id/connect"
android:title="@string/connect"
android:showAsAction="always"/>
<item
android:id="@+id/info"
android:title="@string/info"
android:showAsAction="always"/>
</menu>
Note: The menu item id and title attributes are required.
android:showAsAction="always" causes the action bar items to display always and can potentially overwrite any action bar text.
3. You can customize the action bar as follows:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBar actionBar = getActionBar();
actionBar.setTitle("Custom Title");
}
By default, the app icon appears on the left side of the action bar.
You can change the icon/logo using these ActionBar methods:
ActionBar actionBar = getActionBar();
actionBar.setIcon();
actionBar.setILogo();
You can remove the icon.logo with this ActionBar method:
actionBar.setDisplayShowHomeEnabled(false);
You can change the text, which by default is the name of the application, using this ActionBar method. However, the text will display only if there is room for it.
In this example, we remove the action bar text as follows:
actionBar.setTitle("");
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 text-based 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 a user selects an item from the action items in the action bar, the system calls the activity's onOptionsItemSelected() method.
Identify the item by calling getItemId(), which returns a 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.
Code example for icon-only items is the same as above with these differences:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBar actionBar = getActionBar();
actionBar.setTitle("");
actionBar.setDisplayShowHomeEnabled(false);
}
The Action Bar default app name text and the icon are removed.
In addition, the game_menu.xml file now includes attributes to link to the drawable icons:
<?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:icon="@drawable/ic_new_game"
android:showAsAction="always"/>
<item
android:id="@+id/advanced"
android:title="@string/advanced"
android:icon="@drawable/ic_advanced"
android:showAsAction="always"/>
<item
android:id="@+id/connect"
android:title="@string/connect"
android:icon="@drawable/ic_connect"
android:showAsAction="always"/>
<item
android:id="@+id/info"
android:title="@string/info"
android:icon="@drawable/ic_info"
android:showAsAction="always"/>
</menu>
Notes:
A) How to change an action bar item 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 action bar items with action bar 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);
}
C) You may get a NPE if you run this statement:
ActionBar actionBar = getActionBar();
If your app theme does not have an action bar, or if your min SDK is less than 11.
You should correct the above errors and you can try to add this statement
getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
Before this statement:
setContentView
D) Set action bar background color this way:
Example:
actionBar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#0000ff")));