This article aims to provide a clear Information on different Menu types and how to create Android Menu. Let us now talk about Menus.

What is a Menu in Android?

Android Menu is a user interface component used in many types of applications. To provide a familiar user experience, you should use the Menu APIs for user actions.

Starting from Android 3.0 (API 11), Android-based devices no longer have a dedicated Menu button. Instead, Android apps need to provide an app bar to present common user actions.

In Android applications, Menu is an important user interface entity which provides action options for Android applications.

What are the Types of Menus Used in Android?

Mainly 3 types of Menus are used in Android:

1. Options Menu

2. Context Menu

3. Popup Menu

Options Menu

The Options Menu is the collections of menu items required for an activity. This is where you place actions having the global impact like “Search”, “Compose Mail” and “Settings”.

Note: i) If you are developing for Android 2.3 or lower versions then users can reveal the options menu by pressing the Menu Button.

ii) For Android 3.0 and higher versions, the items are presented by the app bar as a combination of on-screen action items and other options.

Context Menu

A Context menu is a floating menu that appears when the user performs a long-click on an Element. It provides actions that affect the selected content or context frame.

The context menu uses contextual action mode (It is a system implementation of action mode class) to enable actions on selected content. This mode displays action items that affect the selected content in a bar.

Popup Menu

A Popup Menu displays a list of items in a vertical list that invoked the menu. It’s good for providing an overflow of actions that relate to specific options. Actions in a popup menu should not affect the corresponding content.

The Popup menu is used for extended actions that relate to regions of content in activity.

Define the Menu in XML File

For defining the Menus, Android provides the standard XML format to define menu items. Instead of creating the menu in individual’s activity code, we can define menu and its items in an XML menu resource.

For defining menu in XML file, <menu> is used, which is a container of menu items. It is a root element of any root node. For creating a MenuItem in menu, <item> is used, which represents the single item in menu.

Here is one example for MenuItem:

<?xml version="1.0" encoding="utf-8"?> //Root Menu <menu xmlns:android="http://schemas.android.com/apk/res/android"> //It defines the items <item android:id="@+id/item1” android:title="@string/item1" android:showAsAction="ifRoom"/> <item android:id="@+id/iten2” android:title="@string/item2" /> </menu>

Example – Using Popup Menu in Android

Android Popup Menu displays the menu generally below the text/button if space is available.

Otherwise, it is displayed above the text/button.

The android.widget.PopupMenu is the class used for Popup Menu in Android.

activity_main.xml

We are taking one Button.

<RelativeLayout xmlns:tools="http://schemas.android.com/tool” android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity”> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginLeft="62dp" android:layout_marginTop="50dp" android:text="Show Popup"/> </RelativeLayout>

popup_menu.xml

<menu xmlns:androclass="http://schemas.android.com/apk/res/android"> <item android:id="@+id/one" android:title="One"/> <item android:id="@+id/two" android:title="Two"/> <item android:id="@+id/three" android:title="Three"/> </menu>

MainActivity.java

//Initialize the Button. Button button1; cast the button1 in onCreate() method. button1=(Button)findViewById(R.id.button1); //Create the onClick() of button1 and add the code as below: button1.setOnClickListener(new @Override public voidonClick(View v) { //Creating the instance of PopupMenu PopupMenu popup= new PopupMenu(MainActivity.this, button1); popup.getMenuInflater().inflate(R.menu.popup_menu,popup.getMenu()); popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() { public boolean onMenuItemClick(MenuItem tem){ Toast.makeText(MainActivity.this,"You Clicked:"+item.getTitle(),Toast.LENGTH_SHORT).show(); return true; } }); popup.show();//showing popup menu } });//closing the setOnClickListener method } }

Output:

Click on “Show Popup”

Click on Popup Item and It will give you Toast as below:

Thus, we have successfully created Popup Menu with this Example.

Conclusion

Android Menu is slightly different from other UI components. Menus are a hierarchy of the options. App users love powerful options and features if the same are not well organized within the app’s interface then they might as well not even exist.

Depending on what you’ve built, these options and features could affect the way that the app behaves. They could offer extensive customization options or could even tie into APIs, other apps, hardware capabilities, or other external technologies. But, they’ll have to be intuitive and organized to be at their best.

I hope you found this blog helpful about the different Android Menu types. Keep visiting www.acadgild.com for more updates on the courses.

