The menu is a part of the User Interface (UI) component, used to handle some common functionality around the app. To utilize the menu, you should define it in a separate XML file and use that file in your app based on your requirements. You can also use menu APIs to represent user actions and other options in your app activities.
Android provides three types of menus. They are as follows:
This type of menu is a primary collection of menu items in an app and is useful for actions that have a global impact on the searching app. The Option Menu can be used for settings, searching, deleting items, sharing, etc.
This type of menu is a floating menu that only appears when a user presses for a long time on an element and is useful for elements that affect the selected content or context frame.
Using Popup Menu we can display a list of items in a vertical list which presents the view that invokes the menu. Popup Menu is useful since it can provide an overflow of actions which are related to any specific content.
In this step, we will write the menu’s code in an XML format to define the type of menu and its items. First, you should create a new menu folder inside of your project resource folder (res/menu) to define the menu. Add a new XML file (res/menu/file.xml) to build your menu. This XML (res/menu/file.xml) file can be given any name that you provide. There are the following important elements of a menu:
<menu>
: A <menu>
element defines a menu, which is a container for menu items that holds one or more elements. It must be the root of a file.
<items>
: This element is used to create items in the menu. An <items>
element can contain a nested <menu>
element to create a submenu.
<group>
: This element is an optional, invisible container for <item>
elements. <group>
allows categorizing menu items so they share properties such as active state and visibility.
Create a new Android project. We need to create a folder menu inside of your project resource directory and add a new XML file to build the menu.
options_menu.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android" > <item android:id="@+id/search_item" android:title="Search" /> <item android:id="@+id/upload_item" android:title="Upload" /> <item android:id="@+id/copy_item" android:title="Copy" /> <item android:id="@+id/print_item" android:title="Print" /> <item android:id="@+id/share_item" android:title="Share" /> <item android:id="@+id/bookmark_item" android:title="BookMark" /> </menu>
MainActivity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package com.example.optionsmenu;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater()
.inflate(R.menu.options_menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
Toast.makeText(this, "Selected Item: " + item.getTitle(), Toast.LENGTH_SHORT)
.show();
switch (item.getItemId()) {
case R.id.search_item:
return true;
case R.id.upload_item:
return true;
case R.id.copy_item:
return true;
case R.id.print_item:
return true;
case R.id.share_item:
return true;
case R.id.bookmark_item:
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
Output
Output after clicking on the copy menu item.