As Android programming goes, creating an options menu item/button in the Android ActionBar
is fairly straightforward. In this brief tutorial I’ll demonstrate all of the steps needed to add a new menu item to the action bar. Note that in this tutorial I assume that you’re using a Fragment
with your Activity
. If you’re not, the steps are a little different.
1) Define the menu view/layout
The first step is to declare what the menu “view” is going to look like. This is very similar to defining other Android views: you do it with an XML layout file.
In my case I want an “add” button (menu item) that looks like a plus sign (+), and I want it to always be visible. I don’t care about the button/icon showing text; to me, showing text on an action bar menu item is usually pretty ugly, and if a mobile app user can’t figure out that the plus sign means “add,” well, yikes. Here’s the XML layout for the menu item:
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:myapp="http://schemas.android.com/apk/res-auto"> <item android:id="@+id/menu_item_new_quote" android:icon="@android:drawable/ic_menu_add" android:title="@string/new_quote" myapp:showAsAction="ifRoom" /> </menu>
I name this file fragment_quotes_list.xml, and I put it in the res/menu directory of my project.
2) Write the Java/fragment code to show the menu
The first thing you have to do in your fragment is to state that it has an “options menu.” You do that with the setHasOptionsMenu
method, like this:
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setHasOptionsMenu(true); }
Next, you need to “inflate” your XML menu in your Java code. (That’s the Android way.) In my case I inflate it in an onCreateOptionsMenu
method in my fragment (a ListFragment
, to be specific), like this:
/** * the menu layout has the 'add/new' menu item */ @Override public void onCreateOptionsMenu(Menu menu, MenuInflater menuInflater) { menuInflater.inflate(R.menu.fragment_quotes_list, menu); super.onCreateOptionsMenu(menu, menuInflater); }
That’s all you need to do to show your menu. If you run your code now you’ll see the new menu item (button), but when you tap it, it won’t do anything. We need to wire it up to respond to being tapped (clicked), which we’ll do next.
3) Write the Java code to respond to taps/clicks
To get a menu item button/icon to respond to the user tapping/clicking it, you implement the onOptionsItemSelected
method in your fragment.
Because this one method lets you respond to any/all menu items being tapped, you need to identify the menu item you’re handling. You can do this in a case statement, as shown here:
/** * react to the user tapping/selecting an options menu item */ @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.menu_item_new_quote: // TODO put your code here to respond to the button tap Toast.makeText(getActivity(), "ADD!", Toast.LENGTH_SHORT).show(); return true; default: return super.onOptionsItemSelected(item); } }
As shown by the TODO
comment, you need to put your custom Java code in that case
statement clause to do whatever it is you want your application to do when the user taps the menu item. In my current project, the code is very simple, I just start a new Activity
to let the user add a new “Quote,” where a quote is any piece of text they want to add:
Intent i = new Intent(getActivity(), NewQuoteActivity.class); startActivity(i); return true;
Full source code
Here’s a link to the source code for the Android ActionBar ListFragment I used in this example.
Summary: How to implement an Android ActionBar menu item
In summary, if you needed to see the basics of how to implement an Android ActionBar
menu item, I hope this example was helpful.