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. <item
  3. android:id="@+id/menuitem3"
  4. android:orderInCategory="10"
  5. android:showAsAction="ifRoom"
  6. android:icon="@drawable/images"
  7. android:title="Refresh"
  8. />
  9. <item
  10. android:id="@+id/action_settings"
  11. android:title="Settings">
  12. </item>
  13. </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. <TextView
  11. android:layout_width="wrap_content"
  12. android:layout_height="wrap_content"
  13. android:text="@string/hello_world"
  14. android:textSize="50dp"
  15. android:textStyle="bold"
  16. android:layout_centerHorizontal="true"
  17. />
  18. </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. <item
  3. android:id="@+id/menuitem3"
  4. android:orderInCategory="10"
  5. android:showAsAction="ifRoom"
  6. android:icon="@drawable/images"
  7. android:title="Refresh"
  8. />
  9. <item
  10. android:id="@+id/action_settings"
  11. android:title="Settings">
  12. </item>
  13. </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. public class MainActivity extends Activity {
  9. @Override
  10. protected void onCreate(Bundle savedInstanceState) {
  11. super.onCreate(savedInstanceState);
  12. setContentView(R.layout.activity_main);
  13. }
  14. @Override
  15. public boolean onCreateOptionsMenu(Menu menu) {
  16. // Inflate the menu; this adds items to the action bar if it is present.
  17. getMenuInflater().inflate(R.menu.main, menu);
  18. return true;
  19. }
  20. @Override
  21. public boolean onOptionsItemSelected(MenuItem item) {
  22. switch (item.getItemId()) {
  23. case R.id.menuitem3:
  24. Toast.makeText(this, "Menu Item 1 selected", Toast.LENGTH_SHORT).show();
  25. break;
  26. /* case R.id.menuitem2:
  27. Toast.makeText(this, "Menu item 2 selected", Toast.LENGTH_SHORT)
  28. .show();
  29. break;*/
  30. default:
  31. break;
  32. }
  33. return true;
  34. }
  35. }
Step 5
Clipboard04.jpg
Step 6
When you click on the menu item:
Clipboard06.jpg