What is option menu

Android option menu is primary menu item for activity.It display the option when user touches the menu.

In my program i am going to create a simple option menu just like whatsapp option menu you look the figure for clear your doubts.


Option Menu:.

In this section i am going to show the option menu with practical example.

First open the android studio and create a project and give the name of your project like OptionMenuDemo.

The screenshots of the program is as follows:

Smiley face Smiley face

First of all right click on res folder and a pop up menu will display click on new and then select the Android resource file a dialog box will display where you need to write your file name(main_menu) and Resource type as Menu.Figure display all the info do according the figure.

Smiley face

Open the main_menu.xml file and code look like this.

<menu xmlns:android="http://schemas.android.com/apk/res/android"
       xmlns:app="http://schemas.android.com/apk/res-auto">
<item
    android:id="@+id/item1"
    android:title="Privacy"
    app:showAsAction="never">

</item>
    <item
        android:id="@+id/item2"
        android:title="Restart"
        app:showAsAction="never">

    </item>
    <item
        android:id="@+id/item3"
        android:title="Custom Chats"
        app:showAsAction="never">

    </item>
    <item
        android:id="@+id/item4"
        android:title="Message Schdeular"
        app:showAsAction="never">

    </item>
</menu>

       

Open the MainActivity.java file and code look like this.

package com.example.niitpc.optionmenudemo;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

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

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        MenuInflater menuInflater=getMenuInflater();
        menuInflater.inflate(R.menu.main_menu,menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId())
        {
            case R.id.item1:
                Toast.makeText(getApplicationContext(),"Privacy selected",Toast.LENGTH_LONG).show();

            case R.id.item2:
                Toast.makeText(getApplicationContext(),"Restart selected",Toast.LENGTH_LONG).show();

            case R.id.item3:
                Toast.makeText(getApplicationContext(),"Custom Chats selected",Toast.LENGTH_LONG).show();

            case R.id.item4:
                Toast.makeText(getApplicationContext(),"Message Schedular selected",Toast.LENGTH_LONG).show();


        }
        return super.onOptionsItemSelected(item);
    }
}

       


Explanation:

onCreateOptionsMenu(Menu menu):This method is responsible for creating option menu in android.

MenuInflater menuInflater=getMenuInflater();:This statement initialized the object menuInflater of MenuInflate by using getMenuInflater().

menuInflater.inflate(R.menu.main_menu,menu):With the help of menuInflater object we call the inflate method that take one argument that inflat the menu.

onOptionsItemSelected(MenuItem item):This method is responsible for selection of menu items it has a argument that returns the selected item id.

switch (item.getItemId()):With in onOptionsItemSelected(MenuItem item) i am using the switch case for matching particular item id and display a little message with the help of Toast.


Option Menu with Icon

For this you need to go the following link click here for icon and save the icon and then copy the icon and open the res folder and right click on drawable folder and paste it.

Open the main_menu.xml file and only place the icon like this.You also need to change the property app:showAsAction="always". In my program i am added four icons in drawable folder abc,abc1,abc2,abc3.

<menu xmlns:android="http://schemas.android.com/apk/res/android"
       xmlns:app="http://schemas.android.com/apk/res-auto">
<item
    android:id="@+id/item1"
    android:title="Privacy"
    android:icon="@drawable/abc"
    app:showAsAction="always">

</item>
    <item
        android:id="@+id/item2"
        android:icon="@drawable/abc1"
        app:showAsAction="always">

    </item>
    <item
        android:id="@+id/item3"
        android:title="Custom Chats"
        android:icon="@drawable/abc2"
        app:showAsAction="always">


    </item>
    <item
        android:id="@+id/item4"
        android:title="Message Schdeular"
        android:icon="@drawable/abc3"
        app:showAsAction="always">


    </item>
</menu>

       

Option Menu

What is option menu for video Click here

Smiley face                     Smiley face