Learn Menu Item in Action Bar

Introduction

 
This article explains the Action Bar menu on Android. The sample application is developed using Android Studio.
 
The Action Bar is present at the top of an Activity in Android. It can display icon actions that can trigger additional views. It is also used in navigation within your application.
 
The Action Bar was introduced in version 3.0. You can use the Action Bar using the Sherlock Library on Android devices version 1.6. The activity populates the action bar in the onCreateoptionmenu() method. We define the action for the action bar inside the XML file. The MenuInflator class allows inflation of actions defined in an XML file and adds them to the ActionBar.
  • onCreateOptionMenu(): the method that is called only before the optionMenu is displayed.
  • getMenuInflator(): returns the MenuInflator
  • onoptionsItemSelected(): called when you click on a menu item and display what you want in your Activity.
In this, you will create an XML file that consists of an item that you want to show on the ActionBar.
  1. <menu xmlns:android="http://schemas.android.com/apk/res/android" >  
  2.    
  3.     <item  
  4.             android:id="@+id/menuitem3"  
  5.             android:orderInCategory="10"  
  6.             android:showAsAction="ifRoom"  
  7.             android:icon="@drawable/images"  
  8.             android:title="Refresh"  
  9.             />  
  10.     <item  
  11.             android:id="@+id/action_settings"  
  12.             android:title="Settings">  
  13.     </item>  
  14. </menu>  
When you click on the menu item it will display a message on the Activity.
 
Step 1
 
Create the project like this:
 
 
Step 2
 
Create an XML file and write this:
  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.     android:paddingLeft="@dimen/activity_horizontal_margin"  
  6.     android:paddingRight="@dimen/activity_horizontal_margin"  
  7.     android:paddingTop="@dimen/activity_vertical_margin"  
  8.     android:paddingBottom="@dimen/activity_vertical_margin"  
  9.     tools:context=".MainActivity">  
  10.    
  11.     <TextView  
  12.         android:layout_width="wrap_content"  
  13.         android:layout_height="wrap_content"  
  14.         android:text="@string/hello_world"  
  15.         android:textSize="50dp"  
  16.         android:textStyle="bold"  
  17.         android:layout_centerHorizontal="true"  
  18.  />  
  19.    
  20. </RelativeLayout>  
Step 3
 
Create another XML file for the menu item as in the following:
  1. <menu xmlns:android="http://schemas.android.com/apk/res/android" >  
  2.    
  3.     <item  
  4.             android:id="@+id/menuitem3"  
  5.             android:orderInCategory="10"  
  6.             android:showAsAction="ifRoom"  
  7.             android:icon="@drawable/images"  
  8.             android:title="Refresh"  
  9.             />  
  10.     <item  
  11.             android:id="@+id/action_settings"  
  12.             android:title="Settings">  
  13.     </item>  
  14. </menu>  
Step 4
 
Create a Java class file and write this:
  1. package com.actionbar;   
  2. import android.app.ActionBar;  
  3. import android.os.Bundle;  
  4. import android.app.Activity;  
  5. import android.view.Menu;  
  6. import android.view.MenuItem;  
  7. import android.widget.Toast;  
  8.    
  9. public class MainActivity extends Activity {  
  10.    
  11.     @Override  
  12.     protected void onCreate(Bundle savedInstanceState) {  
  13.         super.onCreate(savedInstanceState);  
  14.         setContentView(R.layout.activity_main);  
  15.     }  
  16.    
  17.    
  18.     @Override  
  19.     public boolean onCreateOptionsMenu(Menu menu) {  
  20.         // Inflate the menu; this adds items to the action bar if it is present.  
  21.         getMenuInflater().inflate(R.menu.main, menu);  
  22.         return true;  
  23.     }  
  24.     @Override  
  25.     public boolean onOptionsItemSelected(MenuItem item) {  
  26.         switch (item.getItemId()) {  
  27.             case R.id.menuitem3:  
  28.                 Toast.makeText(this"Menu Item 1 selected", Toast.LENGTH_SHORT).show();  
  29.                 break;  
  30.          /*   case R.id.menuitem2: 
  31.                 Toast.makeText(this, "Menu item 2 selected", Toast.LENGTH_SHORT) 
  32.                         .show(); 
  33.                 break;*/  
  34.    
  35.             default:  
  36.                 break;  
  37.         }  
  38.    
  39.         return true;  
  40.     }  
  41.    
Step 5
 
Clipboard04.jpg
 
Step 6
 
When you click on the menu item:
 
Clipboard06.jpg