How to Implement Options Menu in Android?
Last Updated :
10 Feb, 2025
In Android, there are three types of Menus available to define a set of options and actions in our android applications. Android Option Menus are the primary menus of android. They can be used for settings, searching, deleting items, etc. When and how this item should appear as an action item in the app bar is decided by the Show Action attribute.
How can we implement Option Menu?
There are two main methods/options we can follow to implement option menu:
- In Action Bar
- In Overflow menu
- Using “ifRoom”
i). Action Bar
app:showAsAction="always"
always: This ensures that the menu will always show in the action bar.
Example:
XML
<item
android:id="@+id/send"
android:icon="@android:drawable/ic_menu_send"
android:title="send"
app:showAsAction="always" />

ii). Overflow menu
app:showAsAction="never"
never: This means that the menu will never show, and therefore will be available through the overflow menu.
Example:
XML
<item
android:id="@+id/exit"
app:showAsAction="never"
android:title="exit" />
iii). Using “ifRoom”
app:showAsAction="ifRoom"
never: The menu item will be displayed in the Action Bar if there is enough room. If there isn’t enough space, it will move to the overflow menu.
Example:

Implementation of Option Menu
Below is the complete code for implementing the Options Menu in Android is given below:
Step 1: Create a New Project in Android Studio
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio.
The code for that has been given in both Java and Kotlin Programming Language for Android.
Step 2: Create Menu Directory and Menu file
First, we will create a menu director which will contain the menu file. Go to app > res > right-click > New > Android Resource Directory and give the Directory name and Resource type as menu.

Step 3: Setup Menu file
Now, we will create a menu_example file inside that menu resource directory. Go to app > res > menu > right-click > New > Menu Resource File and create a menu resource file and name it popup_menu. In the popup_menu file, we will add menu items. Below is the code snippet for the menu_example.xml file.
menu_example.xml:
XML
<?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/send"
android:icon="@android:drawable/ic_menu_send"
android:title="send"
app:showAsAction="always" />
<item
android:id="@+id/gallery"
android:icon="@android:drawable/ic_menu_gallery"
android:title="gallery"
app:showAsAction="always" />
<item
android:id="@+id/call"
android:icon="@android:drawable/ic_menu_call"
android:title="call"
app:showAsAction="always" />
<item
android:id="@+id/calculator"
android:icon="@android:drawable/ic_menu_call"
android:title="calculator"
app:showAsAction="ifRoom" />
<item
android:id="@+id/exit"
android:icon="@android:drawable/ic_menu_call"
android:title="exit"
app:showAsAction="ifRoom" />
</menu>
Design UI:

Step 4: Setup MainActivity file
In the MainActivity file, we will add functionalities to each menu options. Here is the code for the MainActivity in both Java and Kotlin.
MainActivity File:
Java
package org.geeksforgeeks.demo;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity
{
// Overriding onCreate Method
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
// Overriding onCreateOptionsMenu Method
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_example, menu);
return true;
}
// Overriding onOptionsItemSelected Method
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId())
{
case R.id.send:
Toast.makeText(getApplicationContext(),
"Shows share icon", Toast.LENGTH_SHORT).show();
return true;
case R.id.gallery:
Toast.makeText(getApplicationContext(),
"Shows image icon", Toast.LENGTH_SHORT).show();
return true;
case R.id.call:
Toast.makeText(getApplicationContext(),
"Shows call icon", Toast.LENGTH_SHORT).show();
return true;
case R.id.calculator:
Toast.makeText(getApplicationContext(),
"Calculator menu", Toast.LENGTH_SHORT).show();
return true;
case R.id.exit:
finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
Kotlin
package org.geeksforgeeks.demo
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
// Override onCreate Method
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
// Override onCreateOptionsMenu Method
override fun onCreateOptionsMenu(menu: Menu): Boolean {
menuInflater.inflate(R.menu.menu_example, menu)
return true
}
// Override onOptionsItemSelected Method
override fun onOptionsItemSelected(item: MenuItem): Boolean {
// Toast the Message according the click
when (item.itemId) {
R.id.send -> {
Toast.makeText(applicationContext,
"Shows share icon", Toast.LENGTH_SHORT).show()
return true
}
R.id.gallery -> {
Toast.makeText(applicationContext,
"Shows image icon", Toast.LENGTH_SHORT).show()
return true
}
R.id.call -> {
Toast.makeText(applicationContext,
"Shows call icon", Toast.LENGTH_SHORT).show()
return true
}
R.id.calculator -> {
Toast.makeText(applicationContext,
"calculator menu", Toast.LENGTH_SHORT).show()
return true
}
R.id.exit -> {
finish()
return true
}
}
return super.onOptionsItemSelected(item)
}
}
Step 4: Setup Themes file (Optional)
We can setup the themes.xml file to change the color of the Action Bar. Here is the code for themes.xml:
themes.xml:
XML
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Base.Theme.Demo" parent="Theme.Material3.DayNight">
<!-- Customize your light theme here. -->
<item name="android:statusBarColor">@android:color/holo_green_dark</item>
<item name="actionBarStyle">@style/AppTheme.ActionBar</item>
</style>
<style name="Theme.Demo" parent="Base.Theme.Demo" />
<style name="AppTheme.ActionBar" parent="Widget.AppCompat.Light.ActionBar">
<item name="background">@android:color/holo_green_light</item>
</style>
</resources>
Output:
Similar Reads
How to Implement Polling in Android?
Many times you may have seen on some apps like youtube, LinkedIn, etc. polling is done and users choose their options whatever they want to choose. Here we are going to implement polling in Android Studio. What we are going to build in this article? In this article, we will ask the user a question a
4 min read
How to Implement PDF Picker in Android?
In this article, we are going to see how we can implement a PDF picker in android studio and get the Uri and Path of the pdf. In this application, we will take permission from the user to Read External Storage and then show the Uri and path of the selected PDF to the user showing that we have succes
4 min read
How to Implement MultiSelect DropDown in Android?
In this article, we are going to see how we can make a MultiSelect DropDown in android studio and will select multiple items from a dropdown list. Advantages of MultiSelect DropDown. It is a good replacement for list boxes as it uses less space does the same work as a list box and gives a good look
4 min read
How to Implement OnSavedInstanceState in Android?
In android, Preserving and restoring an activity's UI state in a timely fashion across system-initiated activity or application destruction is a crucial part of the user experience. In these cases the user expects the UI state to remain the same, but the system destroys the activity and any state st
5 min read
How to Implement SuperBottomBar in Android App?
In this article, we are going to implement bottom navigation like there in Spotify. We all have come across apps that have a Bottom Navigation Bar. Some popular examples include Instagram, Snapchat, etc. In this article, letâs learn how to implement an easy stylish SuperBottomBar functional Bottom N
3 min read
How to Implement Preferences Settings Screen in Android?
In many apps, we have seen the Settings screen which is most common in most of the apps. This settings screen is used to manage the preferences of the users. For creating this settings screen android provides a feature to make a settings preferences screen. In this article, we will take a look at im
7 min read
How to implement Dark (Night) mode in Android app
Light-on-dark color scheme, also called dark mode, is a supplemental mode that uses a color scheme in which content of a webpage is displayed on a dark background. Such a color scheme reduces the light emitted by screens and enhances readability. Switching to dark mode allows website users to move t
5 min read
How to Implement Date Range Picker in Android?
Date Range Picker is a widely used feature in many popular Android apps and an essential component of Material Design. It allows users to select a range of dates such as a start and end date for various purposes including scheduling, filtering data, and setting time boundaries. Some Of The Real Life
4 min read
How to Create Option Menu in Android using Kotlin?
In this article, we will learn how to create an options menu in the Android app using Kotlin. To have an options menu in an Activity, we need to create a new menu XML file and inflate it using menuInflator.inflate( ) method. In menu.xml we will design the options menu as the requirement of the app.
2 min read
How to Implement Custom Searchable Spinner in Android?
Android Spinner is a view similar to the dropdown list which is used to select one option from the list of options. It provides an easy way to select one item from the list of items and it shows a dropdown list of all values when we click on it. The default value of the android spinner will be the c
5 min read