Today we are discussion about Option menu
What is Option Menu in android ?
Option Menus are the primary menus of android. They can be used for settings, search, delete item etc.
Here, we are going to see two examples of option menus. First, the simple option menus and second, options menus with images.
Here, we are inflating the menu by calling the inflate() method of MenuInflater class. To perform event handling on menu items, you need to override onOptionsItemSelected() method of Activity class.
How to Create Option menu in Xml file with code ?
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/action_settings"
android:orderInCategory="100"
android:showAsAction="never"
android:title="@string/action_settings"/>
<item android:id="@+id/item1" android:title="Call"></item>
<item android:id="@+id/item2" android:title="MMS"></item>
<item android:id="@+id/item3" android:title="Delete"></item>
</menu>
Java Code of Option menu
package com.rajnish.opetionmenu;
import com.example.opetionmenu.R;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
public class MainActivity extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.item1:
Intent in =new Intent(Intent.ACTION_CALL);
in.setData(Uri.parse("tel:121"));
startActivity(in);
Toast.makeText(getApplicationContext(),
"Calling started", Toast.LENGTH_LONG).show();
return true;
case R.id.item2:
Toast.makeText(getApplicationContext(),
"MMS Sending", Toast.LENGTH_LONG).show();
return true;
case R.id.item3:
Toast.makeText(getApplicationContext(),
"Item Delete", Toast.LENGTH_LONG).show();
return true;
default:
// TODO Auto-generated method stub
return super.onOptionsItemSelected(item);
}
}
}



