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.

Capture

You know the drill. Create a new project, switch over to MainActivity.java that inherits from android.app.activity, 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(R.menu.main, menu);
    [/java]
  • 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(R.menu.main, menu); and insert the below code.[java]
    menu.add(0, 1, 0, "MenuItem1");
    menu.add(0, 2, 0, "MenuItem2");
    [/java]
  • 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).
    [/java]

    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.

img3

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:
    [java]
    menu.add(0, 1, 0, "MenuItem1").setIcon(R.drawable.menuitemicon1);
    menu.add(0, 2, 0, "MenuItem2").setIcon(R.drawable.menuitemicon2);
    [/java]
  • Obviously, your drawable folders must have images that have menuitemicon1.png and menuitemicon2.png for their names.

img4

COMPLETE SOURCE CODE

[java]
package com.nero.myfirstapp;

import android.os.Bundle;
import android.app.Activity;
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 {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

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

Leave a Comment

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