<h3><strong><span style="color: #000080;">Android Options Menu</span></strong></h3>
<p>The <strong><span style="color: #0000ff;">options menu</span></strong> is the primary collection of menu items for an activity. It&#8217;s where you should place actions that have a global impact on the app, such as &#8220;Search&#8221; &#8220;Compose email&#8221; and &#8220;Settings.&#8221;</p>
<p>You can declare items for the options menu from either your <strong><span style="color: #0000ff;"><code><a style="color: #0000ff;" href="https://developer.android.com/reference/android/app/Activity.html">Activity</a></code></span></strong> subclass or a <strong><span style="color: #0000ff;"><code><a style="color: #0000ff;" href="https://developer.android.com/reference/android/app/Fragment.html">Fragment</a></code></span></strong> subclass.</p>
<p>If both your activity and fragment(s) declare items for the options menu, they are combined in the UI. The activity&#8217;s items appear first, followed by those of each fragment in the order in which each fragment is added to the activity.</p>
<p>In this article, we will talk about how to add options menu in our application.</p>
<p>In this article, we are creating a simple menu with 4 menu items. On clicking on a single menu item a simple Toast message will be shown.</p>
<h3><span style="color: #000080;"><strong>Creating Menu File</strong></span></h3>
<p class="p1">In android, to define <span style="color: #008000;"><b>options menu</b></span>, we need to create a new folder <span style="color: #008000;"><b>menu</b></span> inside of our project resource directory (<span style="color: #008000;"><b>res/menu/</b></span>) and add a new XML (<span style="color: #0000ff;"><b>menu_example.xml</b></span>) file to build the menu.</p>
<p class="p1">Following is the example of defining a menu in XML file (<span style="color: #008000;"><strong>menu_example.xml</strong></span>).</p>
<p><span style="color: #0000ff;"><b><strong>menu_example</strong>.xml</b></span></p>
<pre><;?xml version="1.0" encoding="utf-8"?>;
<;menu xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:app="http://schemas.android.com/apk/res-auto">;

 <;item
 android:id="@+id/item1"
 android:title="Item 1" />;
 <;item
 android:id="@+id/item2"
 android:title="Item 2" />;
 <;item
 android:id="@+id/item3"
 android:title="Item 3"
 app:showAsAction="withText" />;

<;/menu>;</pre>
<h3 class="p1"><span style="color: #000080;"><b>Options Menu Attributes</b></span></h3>
<p class="p2">Following are the some commonly used attributes related to options menu control in android applications.</p>
<p><strong><span style="color: #0000ff;">android:id </span></strong></p>
<p>It is used to uniquely identify element in application.</p>
<p><strong><span style="color: #0000ff;">android:icon</span></strong></p>
<p>It is used to set the item&#8217;s icon from drawable folder.</p>
<p><span style="color: #0000ff;"><strong>android:title</strong></span></p>
<p>It is used to set the item&#8217;s title</p>
<p><strong><span style="color: #0000ff;">android:showAsAction</span></strong></p>
<p>It is used to specify how the item should appear as an action item in the app bar.It contains the following values:</p>
<p><strong><span style="color: #0000ff;">ifRoom : </span></strong>Only place this item in the app bar if there is room for it.</p>
<p><span style="color: #0000ff;"><strong>always</strong></span> : Always place this item in the app bar. Avoid using this unless it&#8217;s critical that the item always appear in the action bar. Setting multiple items to always appear as action items can result in them overlapping with other UI in the app bar.</p>
<p><strong><span style="color: #0000ff;">never: </span></strong><span style="color: #000000;">Never place this item in the app bar. Instead, list the item in the app bar&#8217;s overflow menu.</span></p>
<p><span style="color: #0000ff;"><strong>withText : </strong><span style="color: #000000;">Also include the title text (defined by <code>android:title</code>) with the action item. </span></span></p>
<p>For information about all the supported attributes, see the <span style="color: #0000ff;"><strong><a style="color: #0000ff;" href="https://developer.android.com/guide/topics/resources/menu-resource.html">Menu Resource</a></strong></span> document.</p>
<h3 class="p1"><span style="color: #000080;"><b>Loading Options Menu from an Activity</b></span></h3>
<p class="p2">To specify the options menu for an activity, we need to override <span style="color: #008000;"><b>onCreateOptionsMenu()</b></span> method.</p>
<p>In this method, you can inflate your menu resource (<strong><span style="color: #0000ff;"><a style="color: #0000ff;" href="https://developer.android.com/guide/topics/ui/menus#xml">defined in XML</a></span></strong>) into the <strong><span style="color: #0000ff;"><code><a style="color: #0000ff;" href="https://developer.android.com/reference/android/view/Menu.html">Menu</a></code></span></strong> provided in the callback.</p>
<pre>@Override
public boolean onCreateOptionsMenu(Menu menu) {
 MenuInflater inflater = getMenuInflater();
 inflater.inflate(R.menu.menu_example,menu);
 return true;
}</pre>
<p class="p2">If you observe above code we are calling our menu using <span style="color: #008000;"><b>MenuInflater.inflate()</b> </span>method in the form of <span style="color: #008000;"><b>R.menu.menu_file_name</b></span>. Here our xml file name is <span style="color: #008000;"><b>menu_example.xml</b></span>.</p>
<h3 class="p1"><span style="color: #000080;"><b>Handling Click Events</b></span></h3>
<p>In android, we can handle options menu item click events using <span style="color: #008000;"><b>onOptionsItemSelected()</b></span> method.</p>
<p>When the user selects an item from the options menu (including action items in the app bar), the system calls your activity&#8217;s <code><a href="https://developer.android.com/reference/android/app/Activity.html#onOptionsItemSelected(android.view.MenuItem)">onOptionsItemSelected()</a></code> method.</p>
<p>This method passes the <code><a href="https://developer.android.com/reference/android/view/MenuItem.html">MenuItem</a></code> selected.</p>
<p>You can identify the item by calling <code><a href="https://developer.android.com/reference/android/view/MenuItem.html#getItemId()">getItemId()</a></code>, which returns the unique ID for the menu item (defined by the <code>android:id</code> attribute). You can match this ID against known menu items to perform the appropriate action.</p>
<p class="p2">Following is the example of handling options menu item click event using <span style="color: #008000;"><b>onOptionsItemSelected()</b></span>.</p>
<!-- WP QUADS Content Ad Plugin v. 2.0.98.1 -->
<div class="quads-location quads-ad2" id="quads-ad2" style="float:none;margin:0px;">

</div>

<pre>@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
 int id = item.getItemId();
 switch (id){
 case R.id.Item1:
 <span style="color: #008000;"><strong>//do something</strong></span>
 return true;
 case R.id.Item2:
<span style="color: #008000;"><strong> //do something</strong></span> 
 return true;
 default:
 return super.onOptionsItemSelected(item);
 }
}</pre>
<p>When you successfully handle a menu item, return <code>true</code>.</p>
<p>If you don&#8217;t handle the menu item, you should call the superclass implementation of <code><a href="https://developer.android.com/reference/android/app/Activity.html#onOptionsItemSelected(android.view.MenuItem)">onOptionsItemSelected()</a></code> (the default implementation returns false).</p>
<h3><strong><span style="color: #000080;">Creating New Project</span></strong></h3>
<p>1.Create a new project in <span style="color: #008000;"><strong>Android Studio</strong></span> from <span style="color: #008000;"><strong>File ⇒ New Project</strong></span> and fill the project details.</p>
<p class="p1">2.Now create a new folder <span style="color: #008000;"><b>menu</b></span> inside of our project resource directory (<span style="color: #008000;"><b>res/menu/</b></span>) and add a new XML (<span style="color: #008000;"><b>menu_main.xml</b></span>) file to build the menu.</p>
<p class="p1">3.Open newly created xml (<span style="color: #008000;"><b>menu_main.xml</b></span>) file and write the code as shown below.</p>
<p><span style="color: #0000ff;"><b>menu_main.xml</b></span></p>
<pre><;?xml version="1.0" encoding="utf-8"?>;
<;menu xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:app="http://schemas.android.com/apk/res-auto">;

 <;item
 android:id="@+id/Item1"
 android:title="Item1" />;

 <;item
 android:id="@+id/Item2"
 android:title="Item2" />;

 <;item
 android:id="@+id/Item3"
 android:title="Item3" />;

 <;item
 android:id="@+id/Item4"
 android:title="Item4" />;


<;/menu>;</pre>
<p>4.Now open <span style="color: #008000;"><strong>MainActivity.Java</strong></span> and type following code. In the following code each menu item is identified by its ID in switch case statement.</p>
<p><span style="color: #0000ff;"><b>MainActivity.java</b></span></p>
<pre>package com.example.optionmenudemo;

import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);

 }
<strong><span style="color: #008000;">// Initiating Menu XML file (menu_main.xml)</span></strong>
 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
 MenuInflater inflater = getMenuInflater();
 inflater.inflate(R.menu.menu_main, menu);
 return true;
 }
<strong><span style="color: #008000;">/**</span></strong>
<strong><span style="color: #008000;">*Event handling for individual menu item selected</span></strong>
<strong><span style="color: #008000;">*identify single menu item by its ID</span></strong>
<strong><span style="color: #008000;">*/</span></strong>
 @Override
 public boolean onOptionsItemSelected(@NonNull MenuItem item) {
 int id = item.getItemId();
 switch (id) {
 case R.id.Item1:
 Toast.makeText(getApplicationContext(), "Item1 Selected ", Toast.LENGTH_LONG).show();
 return true;
 case R.id.Item2:
 Toast.makeText(getApplicationContext(), "Item2 Selected ", Toast.LENGTH_LONG).show();
 return true;
 case R.id.Item3:
 Toast.makeText(getApplicationContext(), "Item3 Selected ", Toast.LENGTH_LONG).show();
 return true;
 case R.id.Item4:
 Toast.makeText(getApplicationContext(), "Item4 Selected ", Toast.LENGTH_LONG).show();
 return true;

 default:
 return super.onOptionsItemSelected(item);
 }
 }
}</pre>
<p class="p2">When we run above example using android virtual device (AVD) we will get a result like as shown below.</p>
<p><img class=" wp-image-1321 aligncenter" src="https://c1ctech.com/wp-content/uploads/2019/11/abcd.gif" alt="abcd" width="391" height="695" /></p>
<h3 class="p1"><span style="color: #000080;"><b>Options Menu with Icon</b></span></h3>
<p class="p2">You need to have icon images inside the <strong><span style="color: #008000;">res/drawable</span></strong> directory. The <strong><span style="color: #008000;">android:icon</span></strong> element is used to display the icon on the options menu.</p>
<p><strong><span style="color: #0000ff;">menu_main.xml</span></strong></p>
<pre><;?xml version="1.0" encoding="utf-8"?>;
<;menu xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:app="http://schemas.android.com/apk/res-auto">;

 <;item
 android:id="@+id/Item1"
 android:icon="@android:drawable/ic_menu_share"
 android:title="Item1"
 app:showAsAction="always" />;

 <;item
 android:id="@+id/Item2"
 android:icon="@android:drawable/ic_menu_camera"
 android:title="Item2"
 app:showAsAction="ifRoom" />;

 <;item
 android:id="@+id/Item3"
 android:icon="@android:drawable/ic_menu_search"
 android:title="Item3"
 app:showAsAction="never" />;

 <;item
 android:id="@+id/Item4"
 android:icon="@android:drawable/ic_menu_add"
 android:title="Item4"
 app:showAsAction="withText" />;


<;/menu>;</pre>
<p>When you run your application after adding the above <strong><span style="color: #008000;">menu_main.xml</span></strong> it will look like this:</p>
<p><img class="alignnone wp-image-1319" src="https://c1ctech.com/wp-content/uploads/2019/11/Screenshot_1574334747.png" alt="Screenshot_1574334747" width="311" height="553" /> <img class="alignnone wp-image-1320" src="https://c1ctech.com/wp-content/uploads/2019/11/Screenshot_1574334751.png" alt="Screenshot_1574334751" width="311" height="553" /></p>
<p class="p2">I hope this article will help you in understanding how to create <span style="color: #008000;"><b>Options Menu </b><span style="color: #000000;">to</span></span> handle global functionalities in our applications.</p>
<p> ;</p>


