Introduction

We have seen a "Menu" button on an Android screen, but how is it useful and how can we create a menu in android application? This article will help you to do the same.
Building Menus in an Android Application
Menu is generally used to give extra functionality to an application. One perfect example is, while we are making a phone call, we may want to see other contacts or messages. This is where the Option Menu comes in picture. This can be done using the same.
Menu has 3 types:
  1. Option Menu (Visible if touch "Menu" button)
  2. Context Menu (Visible if long press on any View. View is anything like Button, EditText)
  3. Sub Menu
The following tutorial will guide you towards creating this kind of application step by step.
Step 1: Add "optionmenu.xml"
Right Click on "project" -> New -> Andorid Xml File
index.jpeg
Step 2: Edit "optoinmenu.xml"
This file contains items to be displayed. It takes the "id" that uniquely identify that item and the "title" that displays on screen.
To edit that file
Your Project -> res -> menu -> optionmenu.xml
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <menu
  3. xmlns:android="http://schemas.android.com/apk/res/android">
  4. <item android:id="@+id/Color" android:title="Color">
  5. <menu>
  6. <item android:id="@+id/RedColor" android:title="Red"/>
  7. <item android:id="@+id/GreenColor" android:title="Green"/>
  8. </menu>
  9. </item>
  10. </menu>
Step 3: Edit "MenuOptionDemoActivity.java"
  1. package com.MenuOptionDemo;
  2. import android.app.Activity;
  3. import android.os.Bundle;
  4. import android.view.Menu;
  5. import android.view.MenuInflater;
  6. import android.view.MenuItem;
  7. import android.widget.Toast;
  8. public class MenuOptionDemoActivity extends Activity {
  9. /** Called when the activity is first created. */
  10. @Override
  11. public void onCreate(Bundle savedInstanceState) {
  12. super.onCreate(savedInstanceState);
  13. setContentView(R.layout.main);
  14. }
  15. @Override
  16. public boolean onCreateOptionsMenu(Menu menu) {
  17. // TODO Auto-generated method stub
  18. MenuInflater inflater=getMenuInflater();
  19. inflater.inflate(R.menu.optionmenu, menu);
  20. return super.onCreateOptionsMenu(menu);
  21. }
  22. @Override
  23. public boolean onOptionsItemSelected(MenuItem item) {
  24. // TODO Auto-generated method stub
  25. if(item.getItemId()==R.id.RedColor)
  26. {
  27. Toast.makeText(MenuOptionDemoActivity.this,"Red Color Selected" ,1000).show();
  28. }
  29. else if(item.getItemId()==R.id.GreenColor)
  30. {
  31. Toast.makeText(MenuOptionDemoActivity.this, "Green Color Selected", 1000).show();
  32. }
  33. return super.onOptionsItemSelected(item);
  34. }
  35. }
    In this java file, "onCreateOptionMenu" used to create option menu and "onOptionsItemSelected" used to listen which item is selected by user.
    Step 4: Start Emulator
    index1jpeg.jpeg
    index2.jpeg
    index3.jpeg

    Summary

    In this brief tutorial, we discussed how to create menus in an Android application.