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 combine and display in UI.
Create Android Options Menu in XML File
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 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>
<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>
Load Android Options Menu from an Activity
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.
@Overridepublic void onCreateOptionsMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_example, menu);
}
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.
Handle Android Options Menu Click Events
In android, we can handle a options menu item click events using onOptionsItemSelected() event method.
Following is the example of handling a options menu item click event using onOptionsItemSelected().
@Overridepublic 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);
}
}
switch (item.getItemId()) {
case R.id.mail:
// do something
return true;
case R.id.share:
// do something
return true;
default:
return super.onContextItemSelected(item);
}
}
Android Options Menu Attributes
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 element in application. |
android:icon | It is used to set the item's icon from 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.
Android Options Menu Example
Following is the example of implementing an Options Menu in 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.
options_menu.xml
<?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>
<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.
MainActivity.java
package com.tutlane.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:
// do your code
return true;
case R.id.upload_item:
// do your code
return true;
case R.id.copy_item:
// do your code
return true;
case R.id.print_item:
// do your code
return true;
case R.id.share_item:
// do your code
return true;
case R.id.bookmark_item:
// do your code
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
@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:
// do your code
return true;
case R.id.upload_item:
// do your code
return true;
case R.id.copy_item:
// do your code
return true;
case R.id.print_item:
// do your code
return true;
case R.id.share_item:
// do your code
return true;
case R.id.bookmark_item:
// do your code
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
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 android framework to get the required layout for an activity.
Output of Android Options Menu Example
When we run above example using 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.
No comments:
Post a Comment