SIGN UP MEMBER LOGIN:    
ARTICLE

Option Menu in Android

Posted by Chintan Rathod Articles | Android Programming December 20, 2011
Option Menu is list of menu items for an activity, which appears when the user touches the MENU button on an Android device.
Reader Level:
Download Files:
 

Introduction

We have seen a "Menu" button in 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


-----------------
<?xml version="1.0" encoding="utf-8"?>
<menu
  xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/Color" android:title="Color">
           <menu>
            <item android:id="@+id/RedColor" android:title="Red"/>
            <item android:id="@+id/GreenColor" android:title="Green"/>
        </menu>
   </item>
  
</menu>
-----------------

Step 3: Edit "MenuOptionDemoActivity.java"

----------------

package com.MenuOptionDemo;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;

public class MenuOptionDemoActivity extends Activity {
   
/** Called when the activity is first created. */
   
@Override
   
public void onCreate(Bundle savedInstanceState) {
       
super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
   
  
  @Override
  
  public boolean onCreateOptionsMenu(Menu menu) {
       
// TODO Auto-generated method stub
       
MenuInflater inflater=getMenuInflater();
        inflater.inflate(R.menu.optionmenu, menu);
       
        return super.onCreateOptionsMenu(menu);
    }
   
   
@Override
   
public boolean onOptionsItemSelected(MenuItem item) {
      
  // TODO Auto-generated method stub
       
if(item.getItemId()==R.id.RedColor)
        {
            Toast.makeText(MenuOptionDemoActivity.this,"Red Color Selected" ,1000).show();
        }
       
else if(item.getItemId()==R.id.GreenColor)
        {
            Toast.makeText(MenuOptionDemoActivity.this, "Green Color Selected", 1000).show();
        }
       
return super.onOptionsItemSelected(item);
    }
}


--------------

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 create menus in an Android application.


 

Login to add your contents and source code to this article
share this article :
post comment
 

Thank you for your great reply. But if you tell me on which topic should I present an article, then it is very helpful for me to do so.

Posted by Chintan Rathod Dec 23, 2011

Thank You very much... :-)

Posted by Chintan Rathod Dec 23, 2011

Thanks for your good work! Keep it up!

Posted by Darwin Chaudhary Dec 22, 2011

Chintan you are great..really nice work

Posted by Akash Ahlawat Dec 21, 2011

Chintan your explanation is super, very easy to understand..please keep posting on android as I am also working on it...

Posted by Alec Stewart Dec 21, 2011
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Nevron Gauge for SharePoint
Become a Sponsor