Android Menus are a common user interface component in many types of applications.
Option menu is the primary collection of menu items for an activity,
It's where you should place actions that have a global impact on the app,
The options menu is typically used for providing additional info about an app,
as well as linking to a help and settings sections.Options Menu Example in Android
If you've developed your application for Android 2.3.x (API level 10) or lower,
the contents of your options menu appear at the bottom of the screen when
the user presses the Menu button,
Screenshot
If you've developed your application for Android 3.0 (API level 11) and higher,
items from the options menu are available in the action bar.
Screenshot
Option Menu Will be Create 2 Ways
first way using XML(Declare all menu items in xml)
Second way Progrmatically in Android Activity
Now here we see Fist way of Option Menu creation in Android
Step 1
Create layout file for menu in (res/menu/optionmenulayout.xml) and copy the following
Here you can add what ever items you want to add it to the menu
here i want to add 2 items 1 for settings another one for information about application
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/settings_title"
android:icon="@android:drawable/ic_menu_manage"
android:title="Settings"/>
<item
android:id="@+id/info_title"
android:icon="@android:drawable/ic_menu_info_details"
android:title="Info about app"/>
</menu>
Step 2
Create Android Activity which will inflate optionmenulayout.xml to menu
and Handling click events
When the user selects an item from the options menu 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 or
with an integer given to the add() method). You can match this ID against known menu items
to perform the appropriate action.
package com.androidsurya.androidoptionsmenuexample;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;
public class AndroidOptionsMenu extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.android_options_menu);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu;
super.onCreateOptionsMenu(menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.optionmenulayout, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.settings_title:
// Here you can add your code like forward request to Other Activity
// using Intent.. now i am added only Toast Message for Demo purpose
Toast.makeText(getApplicationContext(),
"Settings Item in Menu clicked.", Toast.LENGTH_LONG).show();
return true;
case R.id.info_title:
// Here you can add your code like forward request to Other Activity
// using Intent.. now i am added only Toast Message for Demo purpose
Toast.makeText(getApplicationContext(),
"Information about Item in Menu clicked.",
Toast.LENGTH_LONG).show();
return true;
}
return false;
}
}
Step 3
Register Activity in AndroidManifest file
<activity
android:name="com.androidsurya.androidoptionsmenuexample.AndroidOptionsMenu"
android:label="@string/app_name" >
After Run your Application On Android Emulator click on Menu Button to launch menu.
Output Screenshots
Option menu is the primary collection of menu items for an activity,
It's where you should place actions that have a global impact on the app,
The options menu is typically used for providing additional info about an app,
as well as linking to a help and settings sections.Options Menu Example in Android
If you've developed your application for Android 2.3.x (API level 10) or lower,
the contents of your options menu appear at the bottom of the screen when
the user presses the Menu button,
Screenshot
If you've developed your application for Android 3.0 (API level 11) and higher,
items from the options menu are available in the action bar.
Screenshot
Option Menu Will be Create 2 Ways
first way using XML(Declare all menu items in xml)
Second way Progrmatically in Android Activity
Now here we see Fist way of Option Menu creation in Android
Step 1
Create layout file for menu in (res/menu/optionmenulayout.xml) and copy the following
Here you can add what ever items you want to add it to the menu
here i want to add 2 items 1 for settings another one for information about application
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/settings_title"
android:icon="@android:drawable/ic_menu_manage"
android:title="Settings"/>
<item
android:id="@+id/info_title"
android:icon="@android:drawable/ic_menu_info_details"
android:title="Info about app"/>
</menu>
Step 2
Create Android Activity which will inflate optionmenulayout.xml to menu
and Handling click events
When the user selects an item from the options menu 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 or
with an integer given to the add() method). You can match this ID against known menu items
to perform the appropriate action.
package com.androidsurya.androidoptionsmenuexample;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;
public class AndroidOptionsMenu extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.android_options_menu);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu;
super.onCreateOptionsMenu(menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.optionmenulayout, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.settings_title:
// Here you can add your code like forward request to Other Activity
// using Intent.. now i am added only Toast Message for Demo purpose
Toast.makeText(getApplicationContext(),
"Settings Item in Menu clicked.", Toast.LENGTH_LONG).show();
return true;
case R.id.info_title:
// Here you can add your code like forward request to Other Activity
// using Intent.. now i am added only Toast Message for Demo purpose
Toast.makeText(getApplicationContext(),
"Information about Item in Menu clicked.",
Toast.LENGTH_LONG).show();
return true;
}
return false;
}
}
Step 3
Register Activity in AndroidManifest file
<activity
android:name="com.androidsurya.androidoptionsmenuexample.AndroidOptionsMenu"
android:label="@string/app_name" >
After Run your Application On Android Emulator click on Menu Button to launch menu.
Output Screenshots
No comments:
Post a Comment