Android ActionBar example: How to create an options menu item

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.

1) Define the 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=""
    <item android:id="@+id/menu_item_new_quote"
          myapp:showAsAction="ifRoom" />

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:

public void onCreate(Bundle savedInstanceState) {

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
public void onCreateOptionsMenu(Menu menu, MenuInflater menuInflater) {
    menuInflater.inflate(, 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
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
            // TODO put your code here to respond to the button tap
            Toast.makeText(getActivity(), "ADD!", Toast.LENGTH_SHORT).show();
            return true;
            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);
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.

Share it!

There’s just one person behind this website; if this article was helpful (or interesting), I’d appreciate it if you’d share it. Thanks, Al.

I forgot that is now a private Github repo. I just copied the code to a different URL and updated the link. Thanks.

Add new comment

The content of this field is kept private and will not be shown publicly.

Anonymous format

  • Allowed HTML tags: <em> <strong> <cite> <code> <ul type> <ol start type> <li> <pre>
  • Lines and paragraphs break automatically.
By submitting this form, you accept the Mollom privacy policy.