How to implement Options Menu in Android

In android, there are three types of Menus available to define a set of options and actions in our android applications.

The Menus in android applications are the following:

  • Android Options Menu
  • Android Context Menu
  • Android Popup Menu

Android Option Menus are the primary menus of android. They can be used for settings, search, delete item etc. When and how this item should appear as an action item in the app bar is decided by the Show Action attribute. The values that can be given for the showAsAction attribute:

  • always: This ensures that the menu will always show in the action bar.

    Syntax:

    app:showAsAction="always"
    

    Example:



    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <item
            android:id="@+id/message"
            android:icon="@android:drawable/ic_menu_send"
            app:showAsAction="always"   
            android:title="message"
    />

    chevron_right

    
    

  • never: This means that the menu will never show, and therefore will be available through the overflow menu

    Syntax:

    app:showAsAction="never"
    

    Example:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

Below is the complete code for implementing the Options Menu in Android is given below:

activity_main.xml

filter_none

edit
close

play_arrow

link
brightness_4
code

<menu 
    xmlns:tools="http://schemas.android.com/tools" 
    tools:context=".MainActivity">
  
    <item
        android:id="@+id/message" 
        android:icon="@android:drawable/ic_menu_send" 
        app:showAsAction="always"   
        android:title="message"/>
  
    <item
        android:id="@+id/picture" 
        android:icon="@android:drawable/ic_menu_gallery" 
        app:showAsAction="always|withText" 
        android:title="picture"/>
  
    <item
        android:id="@+id/mode" 
        android:icon="@android:drawable/ic_menu_call" 
        app:showAsAction="always" 
        android:title="mode"/>
  
    <item
        android:id="@+id/about"
        android:icon="@android:drawable/ic_dialog_info" 
        app:showAsAction="never|withText"
        android:title="calculator"/>
  
    <item
        android:id="@+id/exit"
        app:showAsAction="never"
        android:title="exit"/>
</menu>

chevron_right


MainActivity.java

filter_none

edit
close

play_arrow

link
brightness_4
code

package com.example.menu;
  
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
  
import static android.widget.Toast.LENGTH_LONG;
  
public class MainActivity extends AppCompatActivity {
  
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
  
    public boolean onCreateOptionsMenu(Menu menu)
    {
        getMenuInflater().inflate(R.menu.menu, menu);
        return true;
    }
  
    public boolean onOptionsItemSelected(MenuItem item)
    {
        switch (item.getItemId()) {
        case R.id.message:
            Toast
                .makeText(
                    getApplicationContext(),
                    "Shows share icon",
                    Toast.LENGTH_SHORT)
                .show();
            return true;
  
        case R.id.picture:
            Toast
                .makeText(
                    getApplicationContext(),
                    "Shows image icon",
                    Toast.LENGTH_SHORT)
                .show();
            startActivity(i2);
            return (true);
  
        case R.id.mode:
            Toast
                .makeText(
                    getApplicationContext(),
                    "Shows call icon",
                    Toast.LENGTH_SHORT)
                .show();
            return (true);
  
        case R.id.about:
            Toast
                .makeText(
                    getApplicationContext(),
                    "calculator menu",
                    Toast.LENGTH_SHORT)
                .show();
            return (true);
  
        case R.id.exit:
            finish();
            return (true);
        }
        return (super.onOptionsItemSelected(item));
    }
}

chevron_right


Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.