Tuesday, June 21, 2011

Simple Example on How to Create Menus In Android

Following article will be helpful in creating custom Menus in Android.

Menus in Android Phones are those which Appears on clicking the leftmost button, which is at the left side of the home screen, Menus Appear at the bottom most area of the Screen, Example as illustrated in the following screen shot.



Following Example will help you to create your own custom menus required for your Application, (Check Screenshot below)



Start with creating the xml file named menus.xml in <application_name>\layout\ folder for defining the Menu List as follows:

<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/menu_new"          
          android:title="New" />
          
     <item android:id="@+id/menu_about"         
          android:title="About" />
               
    <item android:id="@+id/menu_help"         
          android:title="Help" />
</menu>

In the above xml, I have simply defined three menus, 'New', 'About' and 'Help', also defined their respective Title and Ids.

Next Step is to call this file in our main Activity Class, this is done as shown in the code for the main Activity class as follows:


package com.mayuri.menuexample;


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 MenuSample 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)
    {
        MenuInflater menuInflater = getMenuInflater();
        menuInflater.inflate(R.layout.menus, menu);
        return true;
    } 
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
       
        switch (item.getItemId())
        {
        case R.id.menu_about:
            Toast.makeText(MenuSample.this, "You Clicked About", 3000).show();
            return true;
        case R.id.menu_help:
            Toast.makeText(MenuSample.this, "You Clicked Help", 3000).show();
            return true;
        case R.id.menu_new:
            Toast.makeText(MenuSample.this, "You Clicked New", 3000).show();
            return true;
        default:
            return super.onOptionsItemSelected(item);
        }
    }
}

The method onCreateOptionsMenu is required to call the defined menus.xml file with the help of the class 'MenuInflater'.

Then an 'onOptionsItemSelected' is defined on each menu items to get the selection events for the each of the menus and the corresponding Toast Popup is displayed for all the three menu selected events.

Following screenshot shows the Diplayed Toast Popup on the selection of the menu 'Above'


This was the basic example of creating custom menus in Android, Hope it was useful, So thats all from my end for now!

Happy Coding..  :-))

~Mayuri