Option Menu:
Option Menu is a drop down menu containing options, and appears when a users clicks on Menu button.
For Ex:
We can create an Option Menu with following :
Create a new folder named "Menu" in res folder
inside this Menu folder create .xml file
here I have created option.xml xml for the option menu in above Image
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="@+id/ChangeColor"
android:icon="@drawable/setting"
android:title="Settings"
/>
<item android:id="@+id/phoneInformation"
android:icon="@drawable/phone"
android:title="My Phone Information" />
<item android:id="@+id/callInfo"
android:icon="@drawable/callinfo"
android:title="In and Out Call Info" />
<item android:id="@+id/email"
android:icon="@drawable/mail"
android:title="Mail to Developer" />
</menu>
Override onCreateOptionMenu method and inflate the .xml (here options.xml) inside method
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.options, menu);
return true;
}
When the user selects an item from the options menu (including action items in the action bar), the system calls your activity's
To Handle click events override onOptionsItemSelected method
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.ChangeColor:
// write code to execute when clicked on this option
return true;
case R.id.phoneInformation:
// write code to execute when clicked on this option
return true;
case R.id.callInfo:
// write code to execute when clicked on this option
return true;
case R.id.email:
// write code to execute when clicked on this option
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Option Menu is a drop down menu containing options, and appears when a users clicks on Menu button.
For Ex:
We can create an Option Menu with following :
- Create a menu xml
- Register the menu in Activity
- Write code to Handle the Clicks on menu items
1: Create xml for menu
Create a new folder named "Menu" in res folder
inside this Menu folder create .xml file
here I have created option.xml xml for the option menu in above Image
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="@+id/ChangeColor"
android:icon="@drawable/setting"
android:title="Settings"
/>
<item android:id="@+id/phoneInformation"
android:icon="@drawable/phone"
android:title="My Phone Information" />
<item android:id="@+id/callInfo"
android:icon="@drawable/callinfo"
android:title="In and Out Call Info" />
<item android:id="@+id/email"
android:icon="@drawable/mail"
android:title="Mail to Developer" />
</menu>
android:id
- A resource ID that's unique to the item, which allows the application can recognize the item when the user selects it.
android:icon
- An image to use as the item's icon.
android:title
- A tittle to show.
2: Register In Activity
Override onCreateOptionMenu method and inflate the .xml (here options.xml) inside method
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.options, menu);
return true;
}
3: Handle Click Events
When the user selects an item from the options menu (including action items in the action bar), the system calls your activity's
onOptionsItemSelected()
method. This method passes the MenuItem
selected. You can identify the item by calling getItemId()
, which returns the unique ID for the menu item (defined by the android:id
attribute in the menu resource To Handle click events override onOptionsItemSelected method
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.ChangeColor:
// write code to execute when clicked on this option
return true;
case R.id.phoneInformation:
// write code to execute when clicked on this option
return true;
case R.id.callInfo:
// write code to execute when clicked on this option
return true;
case R.id.email:
// write code to execute when clicked on this option
return true;
default:
return super.onOptionsItemSelected(item);
}
}
No comments:
Post a Comment