Hello Friends, Today i am going to post different example of Android menu.
Android provides a standard XML format to define menu items
create an XML file inside your project's res/menu/ directory and build the menu.
1. OptionMenu Example:
Res/menu/option_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/Opt1"
android:icon="@drawable/ic_launcher"
android:title="Opt1"/>
<item
android:id="@+id/Opt2"
android:icon="@drawable/ic_launcher"
android:title="Opt2"/>
<item
android:id="@+id/Opt3"
android:icon="@drawable/ic_launcher"
android:title="Opt3"/>
</menu>
OptionnsMenuActivity.class
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 OptionsMenuActivity extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.option_menu, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.Opt1:
Toast.makeText(this, "You have chosen the Opt1 menu option",
Toast.LENGTH_SHORT).show();
return true;
case R.id.Opt2:
Toast.makeText(this, "You have chosen the Opt2 menu option",
Toast.LENGTH_SHORT).show();
return true;
case R.id.Opt3:
Toast.makeText(this, "You have chosen the Opt3 menu option",
Toast.LENGTH_SHORT).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
2. Custom OptionMenu Example:
we can also create custom menu for Android Application.here is the example of custom menu.
CustomOptionsActivity.class
3. Context Menu Example:
here is the example of context menu in android using long press of button event.
context_menu.xml
we can also create custom menu for Android Application.here is the example of custom menu.
custom_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="bottom"
android:orientation="horizontal" >
<Button
android:id="@+id/Btn1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/ic_launcher"
/>
<Button
android:id="@+id/Btn2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/ic_launcher"
/>
<Button
android:id="@+id/Btn3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/ic_launcher"
/>
<Button
android:id="@+id/Btn4"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/ic_launcher"
/>
</LinearLayout>
CustomOptionsActivity.class
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
public class CustomOptionsActivity extends Activity
{
MenuDialog customMenuDialog;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
private class MenuDialog extends AlertDialog
{
public MenuDialog(Context context)
{
super(context);
View cus_menu = getLayoutInflater().inflate(R.layout.custom_menu,null);
setView(cus_menu);
}
@Override
public boolean onKeyUp(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_MENU)
{
dismiss();
return true;
}
return super.onKeyUp(keyCode, event);
}
}
@Override
public boolean onKeyUp(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_MENU)
{
if (customMenuDialog == null)
{
customMenuDialog = new MenuDialog(this);
}
customMenuDialog.show();
return true;
}
return super.onKeyUp(keyCode, event);
}
}
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/edit"
android:title="Edit"/>
<item
android:id="@+id/save"
android:title="Save"/>
<item
android:id="@+id/delete"
android:title="Delete"/>
<item
android:id="@+id/view"
android:title="View"/>
</menu>
ContextMenuActivity.class
import android.app.Activity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.Button;
import android.widget.Toast;
public class ContextMenuActivity extends Activity
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button=(Button)findViewById(R.id.button1);
registerForContextMenu(button);
}
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo)
{
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.context_menu, menu);
}
public boolean onContextItemSelected(MenuItem item)
{
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
switch(item.getItemId())
{
case R.id.edit:
Toast.makeText(this, "You have chosen the Edit",Toast.LENGTH_SHORT).show();
return true;
case R.id.save:
Toast.makeText(this, "You have chosen the Save",
Toast.LENGTH_SHORT).show();
return true;
case R.id.delete:
Toast.makeText(this, "You have chosen the Delete",
Toast.LENGTH_SHORT).show();
return true;
case R.id.view:
Toast.makeText(this, "You have chosen the View",
Toast.LENGTH_SHORT).show();
return true;
default:
return super.onContextItemSelected(item);
}
}
}
}import android.app.Activity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class ContextMenuActivity extends Activity
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button=(Button)findViewById(R.id.button1);
registerForContextMenu(button);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo)
{
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("Context Menu");
menu.add(0, v.getId(), 0, "Item 1");
menu.add(0, v.getId(), 0, "Item 2");
}
@Override
public boolean onContextItemSelected(MenuItem item) {
if(item.getTitle()=="Action 1")
{
function1(item.getItemId());
}
else if(item.getTitle()=="Action 2")
{
function2(item.getItemId());
}
else
{
return false;
}
return true;
}
public void function1(int id)
{
Toast.makeText(this, "You click Item 1", Toast.LENGTH_SHORT).show();
}
public void function2(int id)
{
Toast.makeText(this, "you click Item 2", Toast.LENGTH_SHORT).show();
}
}
i will be happy if you will provide your feedback or follow this blog. Any suggestion and help will be appreciated.
Thank you :)
No comments:
Post a Comment