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)


September 9th, 2009 on 8:25 am
Hi!, I’m learning a lot with your blog.
You could make a post with a menu with animated buttons for a game.
Greetings
October 21st, 2009 on 7:53 pm
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.
October 21st, 2009 on 8:30 pm
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.
December 29th, 2009 on 1:21 pm
Hey dude superb guide.. Can u give more info about manifest.xml ??
January 12th, 2010 on 9:09 pm
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
January 25th, 2010 on 1:59 pm
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
January 25th, 2010 on 4:21 pm
Have you pushed the menu button? If so, what does LogCat show?
January 31st, 2010 on 10:21 am
when i changed this (see caps below), it started working:
January 31st, 2010 on 3:20 pm
Right, but then you didn’t used the correct folder “res/menu”.
February 10th, 2010 on 9:14 am
Tank you for your study blog!
I am glad to test it successfully at the first time!
March 9th, 2010 on 9:29 am
Hey martin thnx a lot for ur blog.. it has helped me in many of my applications……..