Tuesday, 27 November 2012

Android Menu Tutorial

Android Menu Tutorial

In this tutorial we are going to learn how to create android menus.In android applications menu is the most important user interface provides some action for the particular views.

         Here we are going to create a 6 menu items.While clicking a single menu a toast message will show.

Step by step actions

1.Create a new android project.

File==>New==>Android Project==>named as AndroidMenusActivity

2.Now create a XML file under res/layout folder and named as menu.xml

3.Open menu.xml and put the bellow code in it.

<?xml version="1.0" encoding="utf-8"?>
    <item android:id="@+id/menu_bookmark"
          android:title="Bookmark" />
    <item android:id="@+id/menu_save"
          android:title="Save" />
    <item android:id="@+id/menu_search"
          android:title="Search" />
    <item android:id="@+id/menu_share"
          android:title="Share" />
    <item android:id="@+id/menu_delete"
          android:title="Delete" /> 
    <item android:id="@+id/menu_preferences"
          android:title="Preferences" />

4.Now open your main Activity class file,ie.AndroidMenuActivity.java 
and put the following code in it.

package com.androidhive.androidmenus;
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 AndroidMenusActivity extends Activity {
    public void onCreate(Bundle savedInstanceState) {
    // Initiating Menu XML file (menu.xml)
    public boolean onCreateOptionsMenu(Menu menu)
        MenuInflater menuInflater = getMenuInflater();
        menuInflater.inflate(R.layout.menu, menu);
        return true;
     * Event Handling for Individual menu item selected
     * Identify single menu item by it's id
     * */
    public boolean onOptionsItemSelected(MenuItem item)
        switch (item.getItemId())
        case R.id.menu_bookmark:
            // Single menu item is selected do something
            // Ex: launching new activity/screen or show alert message
            Toast.makeText(AndroidMenusActivity.this, "Bookmark is Selected",
            return true;
        case R.id.menu_save:
            Toast.makeText(AndroidMenusActivity.this, "Save is Selected"
            return true;
        case R.id.menu_search:
            Toast.makeText(AndroidMenusActivity.this, "Search is Selected",
            return true;
        case R.id.menu_share:
            Toast.makeText(AndroidMenusActivity.this, "Share is Selected"
            return true;
        case R.id.menu_delete:
            Toast.makeText(AndroidMenusActivity.this, "Delete is Selected",
            return true;
        case R.id.menu_preferences:
            Toast.makeText(AndroidMenusActivity.this, "Preferences is Selected",
            return true;
            return super.onOptionsItemSelected(item);

5.Run your project and in the emulator.Click the button named MENU.Then the
final output is shown bellow.

If you need to run this application in the android smartphone

Hope you understand how it works.

" Ask your doubts and comments please "