[Android General] Roach_:
Informations

In an Android Application, the menu is one of the most important user interface entities which provides some action options for a particular view.

In this Guide I'm discussing about Creating Android Menus.

Below is a screenshot of the final output:

Guide

In this Guide we are creating a simple menu with 6 menu items.

When clicking on single menu item a simple Toast message will be shown.

Create a new project File ⇒ New ⇒ Android Project and give activity name as AndroidMenusActivity.

Now create an XML file under res/layout folder and name it as menu.xml.

Open menu.xml file and type following code.

In the following code we are creating a single menu with 6 menu items.

Each menu item has an icon and title for display the label under menu icon. Also we have id for each menu item to identify uniquely.

[FONT="Courier New"][COLOR=#819f07]» menu.xml[/COLOR]
<?[B][COLOR="Teal"]xml[/COLOR][/B] [COLOR="gray"]version[/COLOR]=[COLOR="Blue"]"1.0"[/COLOR] [COLOR="gray"]encoding[/COLOR]=[COLOR="blue"]"utf-8"[/COLOR] ?>
<[B][COLOR="Teal"]menu[/COLOR][/B] [COLOR="Gray"]xmlns:android[/COLOR]=[COLOR="blue"]"http://schemas.android.com/apk/res/android"[/COLOR]>
[COLOR="DarkGreen"]<!-- Single menu item
Set id, icon and Title for each menu item
-->[/COLOR]
<[B][COLOR="Teal"]item[/COLOR][/B] [COLOR="gray"]android:id[/COLOR]=[COLOR="blue"]"@+id/menu_bookmark"[/COLOR]
[COLOR="gray"]android:icon[/COLOR]=[COLOR="blue"]"@drawable/icon_bookmark"[/COLOR]
[COLOR="gray"]android:title[/COLOR]=[COLOR="blue"]"Bookmark"[/COLOR] />

<[B][COLOR="Teal"]item[/COLOR][/B] [COLOR="gray"]android:id[/COLOR]=[COLOR="blue"]"@+id/menu_save"[/COLOR]
[COLOR="gray"]android:icon[/COLOR]=[COLOR="blue"]"@drawable/icon_save"[/COLOR]
[COLOR="gray"]android:title[/COLOR]=[COLOR="blue"]"Save"[/COLOR] />

<[B][COLOR="Teal"]item[/COLOR][/B] [COLOR="gray"]android:id[/COLOR]=[COLOR="blue"]"@+id/menu_search"[/COLOR]
[COLOR="gray"]android:icon[/COLOR]=[COLOR="blue"]"@drawable/icon_search"[/COLOR]
[COLOR="gray"]android:title[/COLOR]=[COLOR="blue"]"Search"[/COLOR] />

<[B][COLOR="Teal"]item[/COLOR][/B] [COLOR="gray"]android:id[/COLOR]=[COLOR="Blue"]"@+id/menu_share"[/COLOR]
[COLOR="gray"]android:icon[/COLOR]=[COLOR="blue"]"@drawable/icon_share"[/COLOR]
[COLOR="gray"]android:title[/COLOR]=[COLOR="blue"]"Share"[/COLOR] />

<[B][COLOR="Teal"]item[/COLOR][/B] [COLOR="gray"]android:id[/COLOR]=[COLOR="blue"]"@+id/menu_delete"[/COLOR]
[COLOR="gray"]android:icon[/COLOR]=[COLOR="blue"]"@drawable/icon_delete"[/COLOR]
[COLOR="gray"]android:title[/COLOR]=[COLOR="blue"]"Delete"[/COLOR] />

<[B][COLOR="Teal"]item[/COLOR][/B] [COLOR="gray"]android:id[/COLOR]=[COLOR="blue"]"@+id/menu_preferences"[/COLOR]
[COLOR="gray"]android:icon[/COLOR]=[COLOR="blue"]"@drawable/icon_preferences"[/COLOR]
[COLOR="gray"]android:title[/COLOR]=[COLOR="blue"]"Preferences"[/COLOR] />
</[B][COLOR="Teal"]menu[/COLOR][/B]>[/FONT]

Now open your main Activity class file (AndroidMenusActivity.java) and type following code.

In the following code each menu item is identified by its ID in switch case statement.

[FONT="Courier New"][COLOR=#819f07]» AndroidMenusActivity.java[/COLOR]
[B][COLOR="Teal"]package[/COLOR][/B] com.androidhive.androidmenus;

[B][COLOR="Teal"]import[/COLOR][/B] android.app.Activity;
[B][COLOR="Teal"]import[/COLOR][/B] android.os.Bundle;
[B][COLOR="Teal"]import[/COLOR][/B] android.view.Menu;
[B][COLOR="teal"]import[/COLOR][/B] android.view.MenuInflater;
[B][COLOR="teal"]import[/COLOR][/B] android.view.MenuItem;
[B][COLOR="teal"]import[/COLOR][/B] android.widget.Toast;

[B][COLOR="teal"]public class[/COLOR][/B] AndroidMenusActivity [B][COLOR="teal"]extends[/COLOR][/B] Activity {
[COLOR="Gray"]@Override[/COLOR]
[B][COLOR="teal"]public void[/COLOR][/B] onCreate(Bundle savedInstanceState) {
[B][COLOR="teal"]super[/COLOR][/B].onCreate(savedInstanceState);
setContentView(R.layout.main);
}

[COLOR="DarkGreen"]// Initiating Menu XML file (menu.xml)[/COLOR]
[COLOR="gray"]@Override[/COLOR]
[B][COLOR="teal"]public boolean[/COLOR][/B] onCreateOptionsMenu(Menu menu)
{
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.layout.menu, menu);
[B][COLOR="teal"]return true[/COLOR][/B];
}

[COLOR="DarkGreen"] /**
* Event Handling for Individual menu item selected
* Identify single menu item by it's id
* */[/COLOR]
[COLOR="Gray"]@Override[/COLOR]
[B][COLOR="teal"]public boolean[/COLOR][/B] onOptionsItemSelected(MenuItem item)
{

[B][COLOR="teal"]switch[/COLOR][/B] (item.getItemId())
{
[B][COLOR="teal"]case[/COLOR][/B] R.id.menu_bookmark:
[COLOR="DarkGreen"]// Single menu item is selected do something
// Ex: launching new activity/screen or show alert message[/COLOR]
Toast.makeText(AndroidMenusActivity.[B][COLOR="Teal"]this[/COLOR][/B], [COLOR="blue"]"Bookmark is Selected"[/COLOR], Toast.LENGTH_SHORT).show();
[B][COLOR="teal"]return true[/COLOR][/B];

[B][COLOR="teal"]case[/COLOR][/B] R.id.menu_save:
Toast.makeText(AndroidMenusActivity.[B][COLOR="Teal"]this[/COLOR][/B], [COLOR="blue"]"Save is Selected"[/COLOR], Toast.LENGTH_SHORT).show();
[B][COLOR="teal"]return true[/COLOR][/B];

[B][COLOR="teal"]case[/COLOR][/B] R.id.menu_search:
Toast.makeText(AndroidMenusActivity.[B][COLOR="Teal"]this[/COLOR][/B], [COLOR="blue"]"Search is Selected"[/COLOR], Toast.LENGTH_SHORT).show();
[B][COLOR="teal"]return true[/COLOR][/B];

[B][COLOR="teal"]case[/COLOR][/B] R.id.menu_share:
Toast.makeText(AndroidMenusActivity.[B][COLOR="Teal"]this[/COLOR][/B], [COLOR="blue"]"Share is Selected"[/COLOR], Toast.LENGTH_SHORT).show();
[B][COLOR="teal"]return true[/COLOR][/B];

[B][COLOR="teal"]case[/COLOR][/B] R.id.menu_delete:
Toast.makeText(AndroidMenusActivity.[B][COLOR="Teal"]this[/COLOR][/B], [COLOR="blue"]"Delete is Selected"[/COLOR], Toast.LENGTH_SHORT).show();
[B][COLOR="teal"]return true[/COLOR][/B];

[B][COLOR="teal"]case[/COLOR][/B] R.id.menu_preferences:
Toast.makeText(AndroidMenusActivity.[B][COLOR="Teal"]this[/COLOR][/B], [COLOR="Blue"]"Preferences is Selected"[/COLOR], Toast.LENGTH_SHORT).show();
[B][COLOR="teal"]return true[/COLOR][/B];

[B][COLOR="teal"]default[/COLOR][/B]:
[B][COLOR="teal"]return super[/COLOR][/B].onOptionsItemSelected(item);
}
}

}[/FONT]

Finally run your project by Right Clicking on your Project Folder ⇒ Run As ⇒ 1 Android Application to test your application.

Credits

Me - For the topic design.

And I must thank 4 other guys that helped me understand the Android and they're still teaching me:

ihavenick - The first one who helped me understand the Android!

Volk204 - He's the guy that helps me with codes and makes me understand what they do.

Tigrouzen - Lmao! Your kernels are great dude!

Rebellos - He motivated me and I thank him very much!