Android Development

How to create an option menu

by Martin on Aug.28, 2009, under how to

Today we learn how you can create an option menu for your application.
Lets start with an empty android project. The package name will be com.droidnova.android.howto.optionmenu and the activity will have the name SimpleOptionMenu.

Our activity should now look very familiar to us:

1
2
3
4
5
6
7
8
9
10
11
12
13
package com.droidnova.android.howto.optionmenu;
 
import android.app.Activity;
import android.os.Bundle;
 
public class SimpleOptionMenu extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}


First we have to make a new folder in our res/ directory named menu. In this new directory we will create a new xml file named menu.xml.

1
2
3
4
5
6
7
8
9
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/icon"
        android:icon="@drawable/icon" />
    <item android:id="@+id/text"
        android:title="Text" />
    <item android:id="@+id/icontext"
        android:title="Icon and text"
        android:icon="@drawable/icon" />
</menu>

The content of the xml file should be very self explaining. We have an id for each item, so we have a reference for it. The title attribute, if defined, is nothing more than the text you see in the option menu. The same with the icon attribute which references to an icon, in our case the default icon.

Now we have to modify our SimpleOptionMenu activity class. First we have to override the method onCreateOptionsMenu().

1
2
3
4
5
6
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu, menu);
    return true;
}

On line 2 you see the parameter menu. In this menu we will inflate our own menu defined by our menu.xml.
On line 3 we get the MenuInflater from the Activity class. We need this MenuInflater object to inflate, or merge, our own menu in the menu given as a parameter on line 2.
Line 4 inflate the given menu with our own menu. Line 5 is just our return type.

Some explanation: I talked a lot of menus so far, but we just have two different menus. As far as I understand, the menu given as a parameter always exist inside an activity, we simply don’t see it if we press the menu button. The reason should be clear: there is no standard menu defined so the menu has nothing to display. We change that by overriding this method and inflating our own menu.

Right now we can start our application and we will see our 3 option items in our menu when we press the menu button. As long as nothing happens when we press one item, the menu is senseless.

To implement a reaction of our menu, we should override another method named onOptionsItemSelected().

1
2
3
4
5
6
7
8
9
10
11
12
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.icon:     Toast.makeText(this, "You pressed the icon!", Toast.LENGTH_LONG).show();
                            break;
        case R.id.text:     Toast.makeText(this, "You pressed the text!", Toast.LENGTH_LONG).show();
                            break;
        case R.id.icontext: Toast.makeText(this, "You pressed the icon and text!", Toast.LENGTH_LONG).show();
                            break;
    }
    return true;
}

On line 2 you see the parameter which works the same way we know from onTouchEvent(). The parameter item represents the item we pressed in our menu.
A simple switch/case construction with the item.getItemId() execute the code we want. In our case it is a simple Toast which will display a text that describes which item you touched.

Full sources: SimpleOptionMenu (full eclipse project)

simpleoptionmenusimpleoptionmenu1

  • Share/Bookmark
:, , ,

11 Comments for this entry

  • Ismael

    Hi!, I’m learning a lot with your blog.
    You could make a post with a menu with animated buttons for a game.

    Greetings :-)

  • Ash

    Hi,
    I tried downloading and running your project. It opens up the screen, but does not show the menu option. Just shows the text at the top.
    could you please check it.

    Thanks,
    ash.

  • Martin

    Are you sure you pushed the menu button?
    This was programmed for Android 1.5, but I used exactly the same code for a 1.6 application.

    Maybe you try it again and maybe debug it.

    Please, keep me informed, if you could fix the issue.

  • Ajay

    Hey dude superb guide.. Can u give more info about manifest.xml ??

  • nioupy

    Thanks a lot : i am learning from scratch and spend about two hours trying to figure out how to get the id for my menu R.id.itemname… so easy ohhhh my !

    thanks again :)

  • rajesh

    Hi,
    I tried your project. It opens up the screen, but does not show the menu option. Just shows the text at the top.will u plz help me to show the menu content

  • Martin

    Have you pushed the menu button? If so, what does LogCat show?

  • p

    when i changed this (see caps below), it started working:

    1
    2
    3
    4
    5
    6
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.LAYOUT.menu, menu);
        return true;
    }
  • Martin

    Right, but then you didn’t used the correct folder “res/menu”.

  • yuhu.wei

    Tank you for your study blog!
    I am glad to test it successfully at the first time!

  • Richa

    Hey martin thnx a lot for ur blog.. it has helped me in many of my applications……..

Leave a Reply