How to implement Options Menu in Android

14 Feb, 2020
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:<item        android:id="@+id/message"        android:icon="@android:drawable/ic_menu_send"        app:showAsAction="always"           android:title="message"/>
Syntax:
app:showAsAction="always"

Example:
<item        android:id="@+id/message"        android:icon="@android:drawable/ic_menu_send"        app:showAsAction="always"           android:title="message"/>
never: This means that the menu will never show, and therefore will be available through the overflow menuSyntax:app:showAsAction="never"
Example:<item        android:id="@+id/exit"          app:showAsAction="never"         android:title="exit"/>
Syntax:
app:showAsAction="never"

Example:
<item        android:id="@+id/exit"          app:showAsAction="never"         android:title="exit"/>
Below is the complete code for implementing the Options Menu in Android is given below:
activity_main.xml
MainActivity.java
<menu     xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    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>
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));    }}
android
Technical Scripter 2019
Java
Technical Scripter
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Object Oriented Programming (OOPs) Concept in Java
How to iterate any Map in Java
Interfaces in Java
HashMap in Java with Examples
ArrayList in Java
Collections in Java
Stream In Java
Multidimensional Arrays in Java
Singleton Class in Java
Set in Java