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.
How to define Menu in XML File?
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).
- <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.
Android Different Types of Menus
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.
- Android | How to Create/Start a New Project in Android Studio?
- Android | How to add Radio Buttons in an Android Application?
- Android | Android Application File Structure
- Android | AdMob Interstitial Ads for Android Studio
- Android | AdMob Banner Ads for Android Studio
- Android | How to Request permissions in Android Application?
- Android | Running your first Android app
- How to use WebView in Android
- How to use CheckBox in Android
- Android UI Layouts
- Android | 8.0 Oreo
- Android | RecyclerView
- Android SearchView with Example
- Android Architecture
- Android | App to Add Two Numbers
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.