Android Options Menu Example


In this example, we will be creating a simple options menu for an activity . On clicking the menu items a toast message will be shown.

So lets start:

1.Create a new project File ->New -> Project ->Android ->Android Application Project. While creating the new project, name the activity as MenuActivity(MenuActivity.java) and layout as main.xml


2.If you have the latest SDK and ADT version say (20.0.2 ). There will be a folder named menu under /res in the folder structure, you can type your menu items code there. If not create a new menu.xml file in res/layout folder , here we should use two different names for the layout files

res/layout/menu.xml or res/menu/main.xml


<menu xmlns:android="http://schemas.android.com/apk/res/android" >

    <item
        android:id="@+id/menu_one"
        android:orderInCategory="100"
        android:title="one"/>

    <item
        android:id="@+id/menu_two"
        android:icon="@drawable/ic_launcher"
        android:orderInCategory="100"
        android:title="two"/>

    <item
        android:id="@+id/menu_three"
        android:icon="@android:drawable/ic_menu_camera"
        android:orderInCategory="100"/>

    <item
        android:id="@+id/menu_four"
        android:orderInCategory="100"
        android:title="four"/>

    <item
        android:id="@+id/menu_five"
        android:orderInCategory="101"
        android:title="five"/>

    <item
        android:id="@+id/menu_six"
        android:orderInCategory="100"
        android:title="six"/>

</menu>

res/menu/main.xml


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/hello_world"
tools:context=".MenuActivity" />

</RelativeLayout>

3.Now let us bind our UI layout files with the activity. To do so we override onCreateOptionsMenu(Menu menu) and onOptionsItemSelected(MenuItem item) methods in MenuActivity.java file.Press Ctrl+Shift+O for missing imports after typing the code

MenuActivity.java

public class MenuActivity extends Activity {

 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
 }

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  getMenuInflater().inflate(R.menu.main, menu); // or use->
  // getMenuInflater().inflate(R.layout.menu,
  // menu);
  return true;
 }

 @Override
 public boolean onOptionsItemSelected(MenuItem item) {
  // TODO Auto-generated method stub
  switch (item.getItemId()) {
  case R.id.menu_one:
   Toast.makeText(MenuActivity.this, "One is Selected",
     Toast.LENGTH_SHORT).show();
   return true;

  case R.id.menu_two:
   Toast.makeText(MenuActivity.this, "Two with icon is Selected",
     Toast.LENGTH_SHORT).show();
   return true;

  case R.id.menu_three:
   Toast.makeText(MenuActivity.this, "Icon is Selected",
     Toast.LENGTH_SHORT).show();
   return true;

  case R.id.menu_four:
   Toast.makeText(MenuActivity.this, "Four is Selected",
     Toast.LENGTH_SHORT).show();
   return true;

  case R.id.menu_five:
   Toast.makeText(MenuActivity.this, "Five is Selected",
     Toast.LENGTH_SHORT).show();
   return true;

  case R.id.menu_six:
   Toast.makeText(MenuActivity.this, "Six is Selected",
     Toast.LENGTH_SHORT).show();
   return true;

  default:
   return super.onOptionsItemSelected(item);
  }

 }

}

4.Run the project by rightclicking project Run as → android project. If observed carefully the menu item six would be in front of five that is because we have the orderInCategory level of five greater than that of menu item six. We have also tried menus with icon,text and icon with text.

Output:

The output of this example would be as follows