VOOZH about

URL: https://www.geeksforgeeks.org/android/how-to-implement-options-menu-in-android/

⇱ How to Implement Options Menu in Android? - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

How to Implement Options Menu in Android?

Last Updated : 12 Jul, 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:


👁 Image


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:


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:

👁 Image


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.

👁 Image


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:

Design UI:

👁 Layout


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:


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:

Output:


Comment

Explore