Create Menu In Android

Introduction

 
In this article I explain how to create a menu in Android. Menus are a very important user interface entity in Android that provides actions for a specific view such as shown in the following application.
 
In this application I will show how to code a menu in Android and how it works.
 
Step 1
 
First of all create a new Android application project as shown below:
 
menunew.jpg
 
Step 2
 
Now open the XML file as "res/layout/activity_main.xml" and update it as in the following code:
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     tools:context=".MainActivity" >  
  6.     <TextView  
  7.         android:id="@+id/textView1"  
  8.         android:layout_width="200dp"  
  9.         android:layout_height="50dp"  
  10.         android:layout_alignParentTop="true"  
  11.         android:layout_centerHorizontal="true"  
  12.         android:layout_marginTop="170dp"  
  13.         android:gravity="center"  
  14.         android:textColor="#778866"  
  15.         android:background="#020202"  
  16.         android:text="@string/menu"  
  17.         android:textAppearance="?android:attr/textAppearanceLarge" />  
  18. </RelativeLayout> 
Step 3
 
In this step create a new XML file named "menu.xml" using the following code:
  1. <menu xmlns:android="http://schemas.android.com/apk/res/android" >  
  2.     <item  
  3.         android:id="@+id/menu_settings"  
  4.         android:orderInCategory="100"  
  5.         android:showAsAction="never"  
  6.         android:title="@string/menu_settings"/>  
  7.       <item  
  8.           android:id="@+id/option"  
  9.           android:icon="@drawable/option_icon"  
  10.           android:title="Option"/>  
  11.       <item  
  12.           android:id="@+id/delete"  
  13.           android:icon="@drawable/delete_icon"  
  14.           android:title="Delete"/>  
  15.       <item  
  16.           android:id="@+id/back"  
  17.           android:icon="@drawable/back_icon"  
  18.           android:title="Back"/>  
  19. </menu> 
Step 4
 
Now open the "MainAcivity.java" file and update it with your logic.
  1. package com.example.menucreation;  
  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 MainActivity extends Activity {  
  9.     @Override  
  10.     public void onCreate(Bundle savedInstanceState) {  
  11.         super.onCreate(savedInstanceState);  
  12.         setContentView(R.layout.activity_main);       
  13.     }  
  14.    // Initiating Menu XML file (menu.xml)  
  15.     @Override  
  16.     public boolean onCreateOptionsMenu(Menu menu)  
  17.     {  
  18.         MenuInflater menuInflater = getMenuInflater();  
  19.         menuInflater.inflate(R.layout.menu, menu);  
  20.         return true;  
  21.     }  
  22.     @Override  
  23.     public boolean onOptionsItemSelected(MenuItem item)  
  24.     {  
  25.         switch (item.getItemId())  
  26.         {  
  27.         case R.id.menu_settings:  
  28.             Toast.makeText(this"menu settings", Toast.LENGTH_SHORT).show();  
  29.             return true;  
  30.         case R.id.option:  
  31.             Toast.makeText(this"Option is Selected", Toast.LENGTH_SHORT).show();  
  32.             return true;  
  33.         case R.id.delete:  
  34.             Toast.makeText(this" deleted ", Toast.LENGTH_SHORT).show();  
  35.             return true;  
  36.    
  37.         case R.id.back:  
  38.             Toast.makeText(this"you are exit", Toast.LENGTH_SHORT).show();  
  39.             return true;  
  40.         default:  
  41.             return super.onOptionsItemSelected(item);  
  42.         }  
  43.     }     
Step 5
 
Open the "strings.xml" file and update it as given below.
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.     <string name="app_name">MenuCreation</string>  
  4.     <string name="hello_world">Hello world!</string>  
  5.     <string name="menu_settings">Settings</string>  
  6.     <string name="option"></string>  
  7.     <string name="delete"></string>  
  8.     <string name="back"></string>  
  9.     <string name="menu">"Welcome in Menu "</string>  
  10. </resources> 
Step 6
 
Output
 
In the output you will see in the outer Linear Layout I defined a vertical text view, two edit texts, one button and one inner Linear Layout with two horizontal buttons inside it.
 
menu.jpg
 
Selected option:
 
deleted.jpg


Similar Articles