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.

Example

Declare Menu Items in XML

<menu xmlns:android=”http://schemas.android.com/apk/res/android”

xmlns:app=”http://schemas.android.com/apk/res-auto”

xmlns:tools=”http://schemas.android.com/tools”

tools:context=”.MainActivity”>

<item

android:id=”@+id/mapMenu”

android:icon=”@drawable/menu_map”

android:title=”Map” />

<item

android:id=”@+id/favMenu”

android:icon=”@drawable/menu_favourite”

android:title=”Favorite” />

<item

android:id=”@+id/listMenu”

android:icon=”@drawable/menu_list”

android:title=”List” />

<item

android:id=”@+id/settingsMenu”

android:icon=”@drawable/menu_settings”

android:title=”Settings” />

</menu>

Using Menu from Android Activity :

import android.app.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 {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(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));

}

@Override

public boolean onCreateOptionsMenu(Menu menu) {

MenuInflater inflater = getMenuInflater();

inflater.inflate(R.menu.menu_myactivity, menu);

return true;

}

@Override

public boolean onOptionsItemSelected(MenuItem item) {

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

switch (item.getItemId()) {

case R.id.mapMenu:

// do something

break;

case R.id.favMenu:

// do something

break;

case R.id.listMenu:

// do something

break;

case R.id.settingsMenu:

// do something

break;

}

return true;

}

}

Output

android_menu_sample

Book your FREE DEMO session with our Android Developer – Contact Us

For more updates on  Free Android Tutorials.