Android Menu :

Menus are useful for displaying extra options that aren’t directly visible on the main UI of an application. you’ll be able to add two main types of menus in Android:

1.  options menu — Displays information related to the present activity. In Android, you activate the options menu by pressing the MENU key. The menu items displayed vary according to the present activity that’s running.
android option menu

2.  Context menu — Displays information related to a specific view on an activity. In Android, to activate a context menu you tap and hold it.

android context menu
This example shows the way to create the options and context menus for your Android application.

Note : This example (Project) is developed in Eclipse Version Kepler,tested on Android 4.4.2 (KitKat Version) and SDK build-tools 22.6.3

Solution Stuff :

1. To create either an android options menu or a android context menu, populate the activity_menu.xml file in the res/menu folder as follows:android menu

File : res/menu/activity_main

Note : if menu directory not exist in res folder you can create it manually then create xml file (activity_menu.xml).

Put this xml code it into your activity_menu.xml file:

The preceding code piece creates the following:

  •  Four main menu items, as indicated by the  part.
  •  The second menu item contains a submenu (as indicated by the  element) with 3 items.android menu
  •  The fourth menu item contains a submenu with 3 radio buttons (as indicated by the  element).

1. Open “res/layout/activity_main.xml” file and put this xml code it into file:

File : res/layout/activity_main.xml

 

3. Open “MainAcitvity.class” file and add following JAVA code.

File : src/package-name/MainActivity.java

The following java code have android options and context menus to be shown for the activity:

Figure shows Android Option Menu Output in Android emulator :

android option menu example

Figure shows Android Context Menu Output in Android emulator :

android Context  example

Download Android context and options menu example Source code :

Leave a Reply