Open In App
Related Articles

Android Menus

Like Article
Save Article
Report issue

In android, Menu is an important part of the UI component which is used to provide some common functionality around the application. With the help of menu, users can experience a smooth and consistent experience throughout the application. In order to use menu, we should define it in a separate XML file and use that file in our application based on our requirements. Also, we can use menu APIs to represent user actions and other options in our android application activities.

How to define Menu in XML File?

Android Studio provides a standard XML format for the type of menus to define menu items. We can simply define the menu and all its items in XML menu resource instead of building the menu in the code and also load menu resource as menu object in the activity or fragment used in our android application. Here, we should create a new folder menu inside of our project directory (res/menu) to define the menu and also add a new XML file to build the menu with the following elements. Below is the example of defining a menu in the XML file (menu_example.xml). 

Way to create menu directory and menu resource file:

To create the menu directory just right-click on res folder and navigate to res->New->Android Resource Directory. Give resource directory name as menu and resource type also menu. one directory will be created under res folder. 

To create xml resource file simply right-click on menu folder and navigate to New->Menu Resource File. Give name of file as menu_example. One menu_example.xml file will be created under menu folder.


<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://">
    <item android:id="@+id/mail"
        android:title="@string/mail" />
    <item android:id="@+id/upload"
        android:showAsAction="ifRoom" />
    <item android:id="@+id/share"
        android:title="@string/share" />

  • <menu> It is the root element that helps in defining Menu in XML file and it also holds multiple elements.
  • <item> It is used to create a single item in the menu. It also contains nested <menu> element in order to create a submenu.
  • <group> It is optional and invisible for <item> elements to categorize the menu items so they can share properties like active state, and visibility.


If we want to add a submenu in menu item, then we need to add a <menu> element as the child of an <item>. Below is the example of defining a submenu in a menu item. 


<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://">
    <item android:id="@+id/file"
        android:title="@string/file" >
        <!-- "file" submenu -->
            <item android:id="@+id/create_new"
                android:title="@string/create_new" />
            <item android:id="@+id/open"
                android:title="@string/open" />


Android Different Types of Menus

In android, we have 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 Options Menu is a primary collection of menu items in an android application and is useful for actions that have a global impact on the searching application. Android Context Menu is a floating menu that only appears when the user clicks for a long time on an element and is useful for elements that affect the selected content or context frame. Android Popup Menu displays a list of items in a vertical list which presents the view that invoked the menu and is useful to provide an overflow of actions related to specific content.

Last Updated : 21 Jun, 2022
Like Article
Save Article
Share your thoughts in the comments
Similar Reads