SIMPLE MENU
SOURCE CODE is
package com.MenuExample;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
public class MenuExample extends Activity
{
private static final int MENU_ITEM_3 = Menu.FIRST + 3;
Toast msg;
@Override
public void onCreate(Bundle savedInstanceState)
setContentView(R.layout.main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
menu.add(Menu.NONE, MENU_ITEM_1, Menu.NONE,
"Menu Item 1").setIcon(R.drawable.icon);
menu.add(Menu.NONE, MENU_ITEM_2, Menu.NONE,
"Menu Item 2").setIcon(R.drawable.icon);
menu.add(Menu.NONE, MENU_ITEM_3, Menu.NONE,
"Close").setIcon(R.drawable.icon);
return (super.onCreateOptionsMenu(menu));
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
"Menu Item 1 Clicked", Toast.LENGTH_LONG);
msg.show();
break;
case MENU_ITEM_2:
msg = Toast.makeText(MenuExample.this,
"Menu Item 2 Clicked", Toast.LENGTH_LONG);
msg.show();
break;
case MENU_ITEM_3:
finish();
}
return (super.onOptionsItemSelected(item));
}
}
The OUTPUT will be
SOURCE CODE is
package com.MenuExample;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
public class MenuExample extends Activity
{
private static final int MENU_ITEM_1 = Menu.FIRST + 1;
private static final int MENU_ITEM_2 = Menu.FIRST + 2;private static final int MENU_ITEM_3 = Menu.FIRST + 3;
Toast msg;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);setContentView(R.layout.main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
menu.add(Menu.NONE, MENU_ITEM_1, Menu.NONE,
"Menu Item 1").setIcon(R.drawable.icon);
menu.add(Menu.NONE, MENU_ITEM_2, Menu.NONE,
"Menu Item 2").setIcon(R.drawable.icon);
menu.add(Menu.NONE, MENU_ITEM_3, Menu.NONE,
"Close").setIcon(R.drawable.icon);
return (super.onCreateOptionsMenu(menu));
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId()){
case MENU_ITEM_1:
msg = Toast.makeText(MenuExample.this,"Menu Item 1 Clicked", Toast.LENGTH_LONG);
msg.show();
break;
case MENU_ITEM_2:
msg = Toast.makeText(MenuExample.this,
"Menu Item 2 Clicked", Toast.LENGTH_LONG);
msg.show();
break;
case MENU_ITEM_3:
finish();
}
return (super.onOptionsItemSelected(item));
}
}
The OUTPUT will be
Wow , Its is Very useful and nice blog
ReplyDeletenice so simple.. thanks
ReplyDeletehello jack! i don't understand what was happend.I copied your code and then launch android emulator.but I can't see anything.OptionMenu can't show on screen.I tried do ways on forum as android-er.blogspot.com and android-coding.blogspot.finally, I still can't do it.help me.thanks a lot!
ReplyDeletethis is a very helpful but also attach XML file code and damo
ReplyDeletenice It's working
ReplyDeleteit need to attach .xml File
ReplyDeleteHi there,
ReplyDeleteNice, somewhat elegant code, but there remains a question:
Does it run itself?
Probably not, so how do I all it from another activity? or is ist automagically called by pressing the menu button on the phone?
Error icon ok, no declare encapsule type.
ReplyDeleteOk. 2name iquals.
ReplyDeletenow, i want to have two button and when i click a button, it will show a menu, and when i click a other button, it will show a other menu, how will i do ? please, let's help me.
ReplyDelete