In android, Options Menu is a primary collection of menu items for an activity and it is useful to implement actions that have a global impact on the app, such as Settings, Search, etc.
Following is the pictorial representation of using Options Menu in our android applications.
By using Options Menu, we can combine multiple actions and other options that are relevant to our current activity. We can define items for the options menu from either our Activity or Fragment class.
In case, if we define items for the options menu in both activity or fragment, then those items will be combined and display in UI.
In android, to define options menu, we need to create a new folder menu inside of our project resource directory (res/menu/) and add a new XML (menu_example) file to build the menu.
Following is the example of defining a menu in an XML file (menu_example.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>
To specify the options menu for an activity, we need to override onCreateOptionsMenu() method and load the defined menu resource using MenuInflater.inflate() like as shown below.
@Override
public void onCreateOptionsMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_example, menu);
}
If you observe above code we are calling our menu using MenuInflater.inflate() method in the form of R.menu.menu_file_name. Here our xml file name is menu_example.xml so we used file name menu_example.
In android, we can handle options menu item click events using the onOptionsItemSelected() event method.
Following is the example of handling a options menu item click event using onOptionsItemSelected().
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.mail:
// do something
return true;
case R.id.share:
// do something
return true;
default:
return super.onContextItemSelected(item);
}
}
Following are the some of commonly used attributes related to options menu control in android applications.
Attribute | Description |
---|---|
android:id | It is used to uniquely identify an element in application. |
android:icon | It is used to set the item's icon from the drawable folder. |
android:title | It is used to set the item's title |
android:showAsAction | It is used to specify how the item should appear as an action item in the app bar. |
Note: If you are using Android 3.0 +, the Options Menu won’t support any item shortcuts and item icons in the menu.
Following is the example of implementing an Options Menu in the android application.
Create a new android application using android studio and give names as OptionsMenu. In case if you are not aware of creating an app in android studio check this article Android Hello World App.
In android, to define options menu, we need to create a new folder menu inside of our project resource directory (res/menu/) and add a new XML (options_menu.xml) file to build the menu.
Now open newly created xml (options_menu.xml) file and write the code like as shown below.
<?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>
Once we are done with creation of menu, we need to load this menu XML resource from our activity using onCreateOptionsMenu() callback method, for that open main activity file MainActivity.java from \java\com.tutlane.optionsmenu path and write the code like as shown below.
public class MainActivity extends AppCompatActivity {
If you observe above code we are overriding onCreateOptionsMenu() method in activity to create options menu and loaded defined menu resource using MenuInflater.inflate().
Generally, during the launch of our activity, onCreate() callback method will be called by the android framework to get the required layout for an activity.
When we run above example using an android virtual device (AVD) we will get a result like as shown below.
This is how we can create Options Menu in android applications to handle global functionalities in our application.