Create Android Options Menu Programatically

In this post we are going to learn about how to show an android menu without using a separate menu layout xml file. You can create a menu in two ways.
1. Create the menu by inflate an xml file located in the menu folder using the MenuInflater object.
2. Add the menu items dynamically (by writing code) to menu using the add() method.

Here I demonstrate how to add the menu items dynamically to an options menu.

Step 1:  Override the onCreateOptionsMenu in the MainActivity.java file.
  @Override  
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
}
Ads by Google


Step 2: Add each menu items to the menu using the add() method and the Menu object.
            add(arg1, arg2, arg3, arg4);
            The add method takes three arguments.
            arg1  =  Indicates the group id. You can pass Menu.NONE if there is no group available.
            arg2  =   Indicates the item id. 
            arg 3 = Indicates the order number in witch the item appear in the menu. You can pass                                      Menu.NONE if you not consider the item order.
            arg 4 = Indicates the title of the menu item.
 MenuItem it1 = menu.add(Menu.NONE, send_email, Menu.NONE, "Send Email");  
MenuItem it2 = menu.add(Menu.NONE, make_call, Menu.NONE, "Make Call");
MenuItem it3 = menu.add(Menu.NONE, send_cloud, Menu.NONE, "Send Cloud");

Step 3: Add an icon for the menu using setIcon( )  method.
 it1.setIcon(R.drawable.ic_action_email);  
it2.setIcon(R.drawable.ic_action_call);
it3.setIcon(R.drawable.ic_action_cloud);

Step 4: You can make the menu items visible on the Action bar using the setShowAsAction() method.
 it1.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);  
it2.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
it3.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);

Handling the click events

For handling the click events of the options menu, you need to override the onOptionsItemSelected() method. You can get the selected item id using the MenuItem object.
  @Override  
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case send_email:
Toast.makeText(getBaseContext(), "You select the email option ", Toast.LENGTH_SHORT).show();
return true;
case make_call:
Toast.makeText(getBaseContext(), "You select the call option ", Toast.LENGTH_SHORT).show();
return true;
case send_cloud:
Toast.makeText(getBaseContext(), "You select the cloud option ", Toast.LENGTH_SHORT).show();
return true;
default:
return false;
}

Here is the full source code
 package com.menutest;  
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
public class MainActivity extends Activity {
private static final int send_email = 0;
private static final int make_call = 1;
private static final int send_cloud = 2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
MenuItem it1 = menu.add(Menu.NONE, send_email, Menu.NONE, "Send Email");
MenuItem it2 = menu.add(Menu.NONE, make_call, Menu.NONE, "Make Call");
MenuItem it3 = menu.add(Menu.NONE, send_cloud, Menu.NONE, "Send Cloud");
it1.setIcon(R.drawable.ic_action_email);
it2.setIcon(R.drawable.ic_action_call);
it3.setIcon(R.drawable.ic_action_cloud);
it1.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
it2.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
it3.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case send_email:
Toast.makeText(getBaseContext(), "You select the email option ", Toast.LENGTH_SHORT).show();
return true;
case make_call:
Toast.makeText(getBaseContext(), "You select the call option ", Toast.LENGTH_SHORT).show();
return true;
case send_cloud:
Toast.makeText(getBaseContext(), "You select the cloud option ", Toast.LENGTH_SHORT).show();
return true;
default:
return false;
}
}
}

create dynamic menu in android


                                  Click Here to view the video tutorial of this topic.