Hello friends,
Today's post is about Menu in android, throughout this post you will be able to create menu .
let's start with some basic theory about menu. Android supports two types of menu system.
1.Option Menu
2.Context Menu
Option menu is the menu when user press hardware menu button of his device. Whereas Context menu appears when user long press on a View(it may be Button, EditText etc) .
So For creating option menu first thing you have to do is to create one folder named menu under res folder. Create menu.xml under menu.
res/menu/menu.xml
In menu.xml add your menu item(in this demo Home ,End)
Now in the code you have to override onCreateOptionsMenu(Menu menu)
and onOptionsItemSelected()
For creating Context Menu no need to create menu.xml file
Just override onCreateContextMenu(Menu menu) and onContextItemSelected()
Here is the steps for Creating Option Menu
1. Create new project with OptionMenuDemo Activity
2.Create menu/menu.xml under res folder
3.Create your item under optionmenu.xml.
3. Add this code in your OptionMenuDemo.java file.
package com.bp.menudemo;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;
public class OptionMenuDemo extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater m=getMenuInflater();
m.inflate(R.menu.optionmenu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getTitle().equals("Home"))
{
Toast.makeText(getApplicationContext(), "home is clicked", Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(getApplicationContext(), "End is clicked", Toast.LENGTH_LONG).show();
}
return true;
}
}
4. Option Menu output in emulator
Now some steps for creating Context Menu
1.Create another project having ContextMenuDemo Activity
2.In ContextMenuDemo.java file add this code.
package com.bp.contextmenudemo;
import android.app.Activity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.Button;
import android.widget.Toast;
public class ContextMenuDemo extends Activity {
Button b;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
b=(Button) findViewById(R.id.contextmenu);
registerForContextMenu(b);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("Choose");
menu.setHeaderIcon(R.drawable.icon);
menu.add(0, v.getId(), 0, "Home");
menu.add(0, v.getId(), 0, "End");
}
@Override
public boolean onContextItemSelected(MenuItem item) {
if(item.getTitle().equals("Home"))
{
Toast.makeText(getApplicationContext(), "Home menu is clicked", Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(getApplicationContext(), "End menu is clicked", Toast.LENGTH_LONG).show();
}
return true;
}
}
3. Context Menu output in emulator.
That's it for this post.
for any query mail me : bpsingh216@gmail.com
No comments:
Post a Comment