Android Menu Tutorial

In android application menu is one of the most important user interface(UI) entity which provides some action options for a particular view.

Let’s look a the general idea first.

Menus are a common user interface component in many types of applications. To provide a consistent and familiar user experience, you must use the Menu APIs to present user actions and other options in your activities.

Three fundamental types of menus or action presentations on all versions of Android:

  1. Options menu and app bar
  2. Context menu and contextual action mode
  3. Popup menu

1.Option menu and app bar

The options menu is the primary collection of menu items for an activity. They can be used for settings, delete item ,search, etc.

2.Context menu and contextual action mode

A context menu is a floating menu that appears when the user performs a long-click on an element. It provides actions that affect the selected content or context frame.Context Menu is clean and animated library. We can easily add this animated context menu to our application.

A system implementation of contextual ActionMode that displays at the top of the screen when the user long-clicks on a UI element and it is  used to provide actions that are appropriate for the item that has been selected.

3.Popup menu

The popup menu can be displayed in response to almost any user action you care to use. It shows  next to the View object that causes it to be displayed.

Using a menu resource is a good practice for a few reasons:

  • It separates the content for the menu from your application’s behavioral code.
  • It’s easier to visualize the menus structure in XML.

Android menus make use of three objects and hence three XML tags:

<menu> This inflates to a Menu object which is a container for menu items and group elements.

<item> This element may contain a nested <menu> element in order to create a submenu.

<group>  It allows you to categorize menu items so they share properties such as active state and visibility

The four that you need to know about are:

android:id – A resource ID that’s unique to the item, which allows the application to recognize the item when the user selects it.

android:icon – a drawable image used when the menu item can be displayed as an icon.

android:title – A reference to a string to use as the item’s title.

android:showAsAction – Specifies when and how this item should appear as an action item in the app bar.


Declare Menu Items in XML

<menu xmlns:android=””







android:title=”Map” />




android:title=”Favorite” />




android:title=”List” />




android:title=”Settings” />


Using Menu from Android Activity :


import android.os.Bundle;

import android.view.Menu;

import android.view.MenuInflater;

import android.view.MenuItem;

import android.view.ViewGroup.LayoutParams;

import android.widget.TextView;

import android.widget.Toast;

public class MenuActivity extends Activity {


protected void onCreate(Bundle savedInstanceState) {


TextView text = new TextView(this);

text.setText(“Press the menu button to get list of menus.”);

addContentView(text, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));



public boolean onCreateOptionsMenu(Menu menu) {

MenuInflater inflater = getMenuInflater();

inflater.inflate(, menu);

return true;



public boolean onOptionsItemSelected(MenuItem item) {

Toast.makeText(getApplicationContext(),     item.getTitle() + ” selected”, Toast.LENGTH_SHORT).show();

switch (item.getItemId()) {


// do something



// do something



// do something



// do something



return true;