Menu In Android

Creating Menus in Android
This could be done in 2 ways: XML and Java. But the preferred way to do is through XML .Simply, to keep the designing and the logic of the system differently.

Create -> new XML (mymenu.xml) -> Select the Menu (radio button). Through that you make that xml file a menu .On finishing it .We could also see a menu folder has been created in the res folder .

Now to create an item for the menu that is mymenu.xml

<?xml version="1.0" encoding="utf-8"?>

<item android:id="@+id/item1" android:icon="@android:drawable/ic_menu_compass">
<item android:id="@+id/item2"  android:icon="@android:drawable/ic_menu_call">


I am thinking, what the hell is ic_menu_compass right?

Well first of all since we want to put an icon with the menu out there we have customized our item tagby adding an attribute to it “android:icon”. And ic_menu_compass is the System icon that is present in the android.jar package.


Keep going unless you get it.

Then, you see a lot of .png files and in that you will see a group of “ic” prefixed image files. There you could find ic_menu_compass. And, we need a reference to that beautiful thing.
Come to your Java file:

package com.menuinflator;

import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

public class MenuInflatorTestActivity extends Activity {
    /** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState) {

	public boolean onPrepareOptionsMenu(Menu menu) { //Called everytime menu is being clicked.
		// TODO Auto-generated method stub
		Toast.makeText(getBaseContext(),"Prepared menu so called",4000).show();
		return super.onPrepareOptionsMenu(menu);

	public boolean onCreateOptionsMenu(Menu menu) { //used for the initialization purposes. Called once
		// TODO Auto-generated method stub
		Toast.makeText(getBaseContext(),"Menu Creating!",4000).show(); 
		return true;

	public boolean onMenuItemSelected(int featureId, MenuItem item) {
		// TODO Auto-generated method stub
			Toast.makeText(getBaseContext(),"Slected" + featureId, 4000).show();
			Toast.makeText(getBaseContext(),"Item2 Selected", 4000).show();
		return true;

Click on the #menu on the Emulator. The toast gets printed. And now even if you click on menu again and again it never gets printed.

onCreateOptionsMenu() : This method has been overridden. And, it gets called just for the first time when the options of the menus are going to be created. Then, never in life will they show their faces. Remember it!
And run the program.

public boolean onOptionsItemSelected (MenuItem item)

Added in API level 1
This hook is called whenever an item in your options menu is selected. The default implementation simply returns false to have the normal processing happen (calling the item’s Runnable or sending a message to its Handler as appropriate). You can use this method for any items for which you would like to do processing without those other facilities.

Derived classes should call through to the base class for it to perform the default menu handling.

item The menu item that was selected.
boolean Return false to allow normal menu processing to proceed, true to consume it here.

public boolean onMenuItemSelected (int featureId, MenuItem item)

Added in API level 1
Default implementation of onMenuItemSelected(int, MenuItem) for activities. This calls through to the new onOptionsItemSelected(MenuItem) method for the FEATURE_OPTIONS_PANEL panel, so that subclasses of Activity don’t need to deal with feature codes.

featureId The panel that the menu is in.
item The menu item that was selected.
Returns boolean

Return true to finish processing of selection, or false to perform the normal menu handling (calling its Runnable or sending a Message to its target Handler).


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s