Basic ActionBar action item tutorial

As requested by a reader, here’s a simple tutorial explaining the use of menu items in an ActionBar. Menu items shown in the ActionBar are called “action items”, and they’re just as easy to use as menu items. This demo app shows an appropriate Toast when action items are selected.

To start with, make sure to set the minimum SDK version as 11 (Android 3.0 Honeycomb) or greater. ActionBar was introduced in Honeycomb, and will not work on platform versions below that without the help of the Android support library and/or ActionBarSherlock (Setting up ActionBarSherlock is covered here).

Create a default Activity. This Activity overrides the OnCreateOptionsMenu method to inflate a menu XML file, and OnMenuItemSeleceted method to handle action item selection.


public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.menu_main, menu);
		return true;
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see android.app.Activity#onMenuItemSelected(int, android.view.MenuItem)
	 */
	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item) {
		switch (item.getItemId()) {
		case R.id.menu_call:
			Toast.makeText(getApplicationContext(), "You selected \"Call\"!",
					Toast.LENGTH_SHORT).show();
			// TODO Do something here - Make a call etc
			return true;
		case R.id.menu_save:
			Toast.makeText(getApplicationContext(), "You selected \"Save\"",
					Toast.LENGTH_SHORT).show();
			// TODO Do something here - Save data etc
			return true;
		}
		return false;
	}

}

Now create the menu_main.xml file as follows. Every action item can be optionally assigned an icon. The showAsAction attribute defines when the item must be shown in the ActionBar. Commonly used values are “always” [to mandatorily show the item] and “ifRoom” [to show the item when the ActionBar has enough room for it].

<menu xmlns:android="http://schemas.android.com/apk/res/android" >

    <item
        android:id="@+id/menu_call"
        android:icon="@android:drawable/ic_menu_call"
        android:orderInCategory="100"
        android:showAsAction="always|withText"
        android:title="@string/menu_call"/>
    <item
        android:id="@+id/menu_save"
        android:icon="@android:drawable/ic_menu_save"
        android:orderInCategory="10"
        android:showAsAction="always"
        android:title="@string/menu_save"/>

</menu>

And that’s it, really. When an action item is selected, a Toast message is shown. Take a look at the screenshots.
Basic ActionBar Demo When an action item is clicked, a Toast message is displayed

When an action item is clicked, a Toast message is displayed

The source of this app is available here

Advertisements

2 Comments

    1. There isn’t much to change – Use getSupportMenuInflater() [which returns the support library implementation of MenuInflater] instead of getMenuInflater() and change the theme of the activity in AndroidManifest.xml to something based on ActionBarSherlock [such as @style/Theme.Sherlock.Light]

      ETA: You would also need to use getSupportFragmentManager instead of getFragmentManager, of course.

      Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s