Android development primer: Creating Options Menu in Android – Part I

We can have Options Menu in Android activities. Options Menu is the one that appears when the Menu button on the device is pressed. Many applications do nothing on the press of the button. These applications do not have an Options Menu configured. In this post I’ll show you how to have an Options Menu and I’ll do this usingĀ Java. Remember that this can also be done using XML, but programming in Java is far more intuitive and also because well, I know very little xml šŸ˜›
The image below shows the Menu Button on an Eclair AVD.


You know the drill. Create a new project, switch over to MainActivity.javaĀ that inherits from,Ā and start coding.Ā Complete Source Code is at the bottom.

  • If you are using the newer versions of SDK, you can already see the functionĀ onCreateOptionsMenu(Menu menu)
  • We will be overriding the original function in the Activity class to customize our menu.
  • If you run the skeleton application on the emulator and click the Menu button, a menu will pop up with only one optionĀ Settings. Depending on the SDK you are using this may or may not appear.
  • The menu has been already customized. You can see the following line in the method onCreateOptionsMenu(Menu menu).[java]
    getMenuInflater().inflate(, menu);
  • You can look at the predefined layout by navigating toĀ <yourprojectname> -> res -> menu -> main.xml in Eclipse. For the time being, we will not be editing this xml and concentrate on working with Java.
  • Comment outĀ getMenuInflater().inflate(, menu); and insert the below code.[java]
    menu.add(0, 1, 0, "MenuItem1");
    menu.add(0, 2, 0, "MenuItem2");
  • MenuItem1 and MenuItem2 are placeholders. Change them as you like. The syntax of the menu.add() function used here is[java]
    public abstract MenuItem add(int groupId, int itemId, int order, CharSequence title).

    Remember that this is an overloaded function, but we will be using the one specified above for the time being.

  • Save it and execute it on the emulator/device.


Want to make your menu fancier? Keep reading.

You can insert images/icons to you menu items. A point to note however, is that the resolution of the icon should be pretty small so that it can appear over the menu item text. I would suggest one about the dimensions of 48×48.

  • Edit the lines you have just added, to read the following:
    menu.add(0, 1, 0, "MenuItem1").setIcon(R.drawable.menuitemicon1);
    menu.add(0, 2, 0, "MenuItem2").setIcon(R.drawable.menuitemicon2);
  • Obviously, your drawableĀ folders must have images that haveĀ menuitemicon1.png andĀ menuitemicon2.png for their names.



package com.nero.myfirstapp;

import android.os.Bundle;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity {

protected void onCreate(Bundle savedInstanceState) {

public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
//getMenuInflater().inflate(, menu);
menu.add(0, 1, 0, "MenuItem1").setIcon(R.drawable.menuitemicon1);
menu.add(0, 2, 0, "MenuItem2").setIcon(R.drawable.menuitemicon2);
return true;

Leave a Comment

Your email address will not be published. Required fields are marked *