How to Create ActionBar Menu Items in Android

In this article we will learn to create an application which add action items and menu items to action bar.

When we use ActionBar in the application , few important points are to be remember as follows:

  1. Extend ActionBarActivity or AppCompatActivity instead of Activity,

    public class MainActivity extends ActionBarActivity{
    or
    public class MainActivity extends AppCompatActivity {

  2. Select the right title for showAsAction for instance : “always”, “ifRoom”
    1. <menu xmlns:android="http://schemas.android.com/apk/res/android" Xmlns:app="http://schemas.android.com/apk/res-auto">  
    2.     <item android:id="@+id/action_user" android:icon="@drawable/ic_action_user" android:title="@string/action_user" app:showAsAction="ifRoom" /> ...   
    3. </menu>  
  3. Define your Menu in Activity,
    1. @Override  
    2. public boolean onCreateOptionsMenu(Menu menu)  
    3. {  
    4.     // Inflate the menu; this adds items to the action bar if it is present.  
    5.     getMenuInflater().inflate(R.menu.menu_main, menu);  
    6.     return true;  
    7. }  
    8. @Override  
    9. public boolean onOptionsItemSelected(MenuItem item)  
    10. {  
    11.     // Handle action bar item clicks here. The action bar will  
    12.     // automatically handle clicks on the Home/Up button, so long  
    13.     // as you specify a parent activity in AndroidManifest.xml.  
    14.     switch (item.getItemId())  
    15.     {  
    16.         case R.id.action_user:  
    17.             Toast toast0 = Toast.makeText(getApplicationContext(), "User Profile", Toast.LENGTH_SHORT);  
    18.             toast0.show();  
    19.             return true;  
    20.         case R.id.search:  
    21.             Toast toast00 = Toast.makeText(getApplicationContext(), "Search", Toast.LENGTH_SHORT);  
    22.             toast00.show();  
    23.             return true;  
    24.         case R.id.action_settings:  
    25.             Toast toast = Toast.makeText(getApplicationContext(), "Action Settings", Toast.LENGTH_SHORT);  
    26.             toast.show();  
    27.             return true;  
    28.         case R.id.help_center:  
    29.             Toast toast1 = Toast.makeText(getApplicationContext(), "Help Center", Toast.LENGTH_SHORT);  
    30.             toast1.show();  
    31.             return true;  
    32.         case R.id.Rating:  
    33.             Toast toast3 = Toast.makeText(getApplicationContext(), "Rating", Toast.LENGTH_SHORT);  
    34.             toast3.show();  
    35.             return true;  
    36.         case R.id.More:  
    37.             Toast toast2 = Toast.makeText(getApplicationContext(), "More", Toast.LENGTH_SHORT);  
    38.             toast2.show();  
    39.             return true;  
    40.         default:  
    41.             return super.onOptionsItemSelected(item);  
    42.     }  
    43. }  

main_menu.xml

  1. <menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity">  
  2.     <item android:id="@+id/action_user" android:icon="@drawable/ic_user" android:title="Profile" app:showAsAction="ifRoom">  
  3.       </item>  
  4.     <item android:id="@+id/search" android:icon="@drawable/ic_search" android:title="Search" app:showAsAction="ifRoom">  
  5.       </item>  
  6.     <item android:id="@+id/action_settings" android:title="@string/action_settings" />  
  7.     <item android:id="@+id/help_center" android:title="Help Center">  
  8.       </item>  
  9.     <item android:id="@+id/Rating" android:title="Rate the App">  
  10.       </item>  
  11.     <item android:id="@+id/More" android:title="More">  
  12.       </item>  
  13. </menu>  
Note:

app:showAsAction="always"

It will force them to be there but take in consideration what is happening if it's still not enough room.

When contained within the action bar there is a finite maximum of action items based on the device's density-independent width. The action items can also not cover more than half the width of the action bar.

Source Code:

MainActivity.java
  1. package com.example.abhijeet.menuoptionsdemo;  
  2. import android.app.Dialog;  
  3. import android.content.Intent;  
  4. import android.os.Bundle;  
  5. import android.support.design.widget.FloatingActionButton;  
  6. import android.support.design.widget.Snackbar;  
  7. import android.support.v7.app.AppCompatActivity;  
  8. import android.support.v7.widget.Toolbar;  
  9. import android.view.Gravity;  
  10. import android.view.View;  
  11. import android.view.Menu;  
  12. import android.view.MenuItem;  
  13. import android.widget.Button;  
  14. import android.widget.EditText;  
  15. import android.widget.Toast;  
  16. public class MainActivity extends AppCompatActivity  
  17. {  
  18.     @Override  
  19.     protected void onCreate(Bundle savedInstanceState)  
  20.     {  
  21.         super.onCreate(savedInstanceState);  
  22.         setContentView(R.layout.activity_main);  
  23.         Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);  
  24.         setSupportActionBar(toolbar);  
  25.         FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);  
  26.         fab.setOnClickListener(new View.OnClickListener()  
  27.         {  
  28.             @Override  
  29.             public void onClick(View view)  
  30.             {  
  31.                 Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG).setAction("Action"null).show();  
  32.             }  
  33.         });  
  34.     }  
  35.     @Override  
  36.     public boolean onCreateOptionsMenu(Menu menu)  
  37.     {  
  38.         // Inflate the menu; this adds items to the action bar if it is present.  
  39.         getMenuInflater().inflate(R.menu.menu_main, menu);  
  40.         return true;  
  41.     }  
  42.     @Override  
  43.     public boolean onOptionsItemSelected(MenuItem item)  
  44.     {  
  45.         // Handle action bar item clicks here. The action bar will  
  46.         // automatically handle clicks on the Home/Up button, so long  
  47.         // as you specify a parent activity in AndroidManifest.xml.  
  48.         switch (item.getItemId())  
  49.         {  
  50.             case R.id.action_user:  
  51.                 Toast toast0 = Toast.makeText(getApplicationContext(), "User Profile", Toast.LENGTH_SHORT);  
  52.                 toast0.show();  
  53.                 return true;  
  54.             case R.id.search:  
  55.                 Toast toast00 = Toast.makeText(getApplicationContext(), "Search", Toast.LENGTH_SHORT);  
  56.                 toast00.show();  
  57.                 return true;  
  58.             case R.id.action_settings:  
  59.                 Toast toast = Toast.makeText(getApplicationContext(), "Action Settings", Toast.LENGTH_SHORT);  
  60.                 toast.show();  
  61.                 return true;  
  62.             case R.id.help_center:  
  63.                 Toast toast1 = Toast.makeText(getApplicationContext(), "Help Center", Toast.LENGTH_SHORT);  
  64.                 toast1.show();  
  65.                 return true;  
  66.             case R.id.Rating:  
  67.                 Toast toast3 = Toast.makeText(getApplicationContext(), "Rating", Toast.LENGTH_SHORT);  
  68.                 toast3.show();  
  69.                 return true;  
  70.             case R.id.More:  
  71.                 Toast toast2 = Toast.makeText(getApplicationContext(), "More", Toast.LENGTH_SHORT);  
  72.                 toast2.show();  
  73.                 return true;  
  74.             default:  
  75.                 return super.onOptionsItemSelected(item);  
  76.         }  
  77.     }  
  78. }  
menu_main.xml
  1. <menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity">  
  2.     <item android:id="@+id/action_user" android:icon="@drawable/ic_user" android:title="Profile" app:showAsAction="ifRoom">  
  3.       </item>  
  4.     <item android:id="@+id/search" android:icon="@drawable/ic_search" android:title="Search" app:showAsAction="ifRoom">  
  5.       </item>  
  6.     <item android:id="@+id/action_settings" android:title="@string/action_settings" />  
  7.     <item android:id="@+id/help_center" android:title="Help Center">  
  8.       </item>  
  9.     <item android:id="@+id/Rating" android:title="Rate the App">  
  10.       </item>  
  11.     <item android:id="@+id/More" android:title="More">  
  12.       </item>  
  13. </menu>  
Output:

output

output  
Ebook Download
View all
Learn
View all