Android Menus - GeeksforGeeks

21 Feb, 2022
In android, Menu is an important part of UI component which is used to provide some common functionality around the application. With the help of menu user can experience smooth and consistent experience throughout the application.
In order to use menu, we should define it in 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.
Android Studio provides a standard XML format for 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 following elements.
Below is the example of defining a menu in XML file (menu_example.xml).
XML
<?xml version="1.0" encoding="utf-8"?><menu xmlns:android="http:// schemas.android.com/apk/res/android">    <item android:id="@+id/mail"        android:icon="@drawable/ic_mail"        android:title="@string/mail" />    <item android:id="@+id/upload"        android:icon="@drawable/ic_upload"        android:title="@string/upload"        android:showAsAction="ifRoom" />    <item android:id="@+id/share"        android:icon="@drawable/ic_share"        android:title="@string/share" /></menu>
 
<menu> It is the root element which helps in defining Menu in XML file and it also holds multiple elements.
<item> It is used to create a single item in menu. It also contains nested <menu> element in order to create a submenu.
<group> It is an optional and invisible for <item> elements to categorize the menu items so they can share properties like active state, visibility.
If we want to add 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 menu item.
XML
<?xml version="1.0" encoding="utf-8"?><menu xmlns:android="http:// schemas.android.com/apk/res/android">    <item android:id="@+id/file"        android:title="@string/file" >        <!-- "file" submenu -->        <menu>            <item android:id="@+id/create_new"                android:title="@string/create_new" />            <item android:id="@+id/open"                android:title="@string/open" />        </menu>    </item></menu>
In android, we have a three types of Menus available to define a set of options and actions in our android applications.
The Menus in android applications are following –
Android Options Menu
Android Context Menu
Android Popup Menu
Android Options Menu – Android Options Menu is a primary collection of menu items in an android application and useful for actions that have a global impact on the searching application.
Android Context Menu – Android Context Menu is a floating menu only appears when user click for a long time on an element and useful for elements that effect the selected content or context frame.
Android Popup Menu – Android Popup Menu displays a list of items in a vertical list which presents to the view that invoked the menu and useful to provide an overflow of actions that related to specific content.
ayushpandey3july
android
Kotlin Android
Kotlin
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Broadcast Receiver in Android With Example
Android RecyclerView in Kotlin
Content Providers in Android with Example
Retrofit with Kotlin Coroutine in Android
Kotlin Setters and Getters
Kotlin Android Tutorial
How to Add and Customize Back Button of Action Bar in Android?
Kotlin when expression
How to Change the Color of Status Bar in an Android App?
Kotlin Higher-Order Functions