Android Option Menu Example

  • 0

Android Option Menu Example

Category : Uncategorized

Android Option Menus are the primary menus of android. They can be used for settings, search, delete item etc.

Here, we are going to see two examples of option menus. First, the simple option menus and second, options menus with images.

Here, we are inflating the menu by calling the inflate() method of MenuInflater class. To perform event handling on menu items, you need to override onOptionsItemSelected() method of Activity class.

Android Option Menu Example

Let’s see how to create menu in android. Let’s see the simple option menu example that contains three menu items.

activity_main.xml

We have only one textview in this file.

File: activity_main.xml
  1. <RelativeLayout xmlns:androclass=“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:paddingBottom=“@dimen/activity_vertical_margin”
  6.     android:paddingLeft=“@dimen/activity_horizontal_margin”
  7.     android:paddingRight=“@dimen/activity_horizontal_margin”
  8.     android:paddingTop=“@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. </RelativeLayout>

menu_main.xml

It contains three items as show below. It is created automatically inside the res/menu directory.

File: menu_main.xml
  1. <menu xmlns:androclass=“http://schemas.android.com/apk/res/android” >
  2.     <item  android:id=“@+id/item1”
  3.         android:title=“Item 1”/>
  4.     <item  android:id=“@+id/item2”
  5.         android:title=“Item 2”/>
  6.     <item  android:id=“@+id/item3”
  7.         android:title=“Item 3”/>
  8. </menu>

Activity class

This class displays the content of menu.xml file and performs event handling on clicking the menu items.

File: MainActivity.java
  1. package com.javatpoint.optionmenu;
  2. import android.os.Bundle;
  3. import android.app.Activity;
  4. import android.view.Menu;
  5. import android.view.MenuItem;
  6. import android.widget.Toast;
  7. public class MainActivity extends Activity {
  8.     @Override
  9.     protected void onCreate(Bundle savedInstanceState) {
  10.         super.onCreate(savedInstanceState);
  11.         setContentView(R.layout.activity_main);
  12.     }
  13.     @Override
  14.     public boolean onCreateOptionsMenu(Menu menu) {
  15.         // Inflate the menu; this adds items to the action bar if it is present.
  16.         getMenuInflater().inflate(R.menu.main, menu);//Menu Resource, Menu
  17.         return true;
  18.     }
  19.     @Override
  20.     public boolean onOptionsItemSelected(MenuItem item) {
  21.         switch (item.getItemId()) {
  22.             case R.id.item1:
  23.               Toast.makeText(getApplicationContext(),“Item 1 Selected”,Toast.LENGTH_LONG).show();
  24.             return true;
  25.            case R.id.item2:
  26.                 Toast.makeText(getApplicationContext(),“Item 2 Selected”,Toast.LENGTH_LONG).show();
  27.               return true;
  28.             case R.id.item3:
  29.                 Toast.makeText(getApplicationContext(),“Item 3 Selected”,Toast.LENGTH_LONG).show();
  30.               return true;
  31.               default:
  32.                 return super.onOptionsItemSelected(item);
  33.         }
  34.     }
  35. }


Output:

Output without clicking on the menu button.

android option menu example output 1

Output after clicking on the menu button.


android option menu example output 2

Output after clicking on the second menu item .


android option menu example output 3

Option Menu with Icon

You need to have icon images inside the res/drawable directory. The android:icon element is used to display the icon on the option menu. You can write the string information in the strings.xml file. But we have written it inside the menu_main.xml file.

File: menu_main.xml
  1. <menu xmlns:androclass=“http://schemas.android.com/apk/res/android” >
  2.     <item  android:id=“@+id/item1”
  3.         android:icon=“@drawable/add”
  4.         android:title=“Item 1”/>
  5.     <item  android:id=“@+id/item2”
  6.         android:icon=“@drawable/minus”
  7.         android:title=“Item 2”/>
  8.     <item  android:id=“@+id/item3”
  9.         android:icon=“@drawable/delete”
  10.         android:title=“Item 3”/>
  11. </menu>

Leave a Reply