Menu (OptionMenu)
Android has 2 types of menu.
One of Option menu. Option Menu is menu to show up when pushing menu button.
It displays 6 panels menu bottom of display.
Maximum number is 6, if you want to add more than 6, need to use other category or submenu.
Steps
- Implement public boolean onCreateOptionsMenu(Menu menu) to add Menu UI
- Implement public boolean onOptionsItemSelected(MenuItem item) to handle clicking
Method
| Method | Description |
|---|---|
| onCreateOptionsMenu | Only when calling option menu(once) |
| onPrepareOptionsMenu | Everytime to display option menu |
| onOptionsItemSelected | When selecting menu item |
Sample
public class MenuActivity extends Activity
{
private static final int MENU_ID_MENU1 = (Menu.FIRST + 1);
private static final int MENU_ID_MENU2 = (Menu.FIRST + 2);
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// We can keep Menu menu out of this method
MenuItem item1 = menu.add(Menu.NONE,MENU_ID_MENU1,Menu.NONE, "Android");
MenuItem item2 = menu.add(Menu.NONE,MENU_ID_MENU2,Menu.NONE, "iOS");
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
boolean ret = true;
switch ( item.getItemId() )
{
case MENU_ID_MENU1:
ret = true;
break;
case MENU_ID_MENU2:
ret = true;
break;
default:
ret = super.onOptionsItemSelected(item);
}
return ret;
}
}
Sub Menu
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// We can keep Menu menu out of this method
MenuItem item1 = menu.add(Menu.NONE,MENU_ID_MENU1,Menu.NONE, "Android");
MenuItem item2 = menu.add(Menu.NONE,MENU_ID_MENU2,Menu.NONE, "iOS");
// Sub Menu
SubMenu item3 = menu.addSubMenu(Menu.NONE, MENU_ID_MENU3, Menu.NONE,"Others");
item3.setIcon(android.R.drawable.ic_menu_more);
item3.add(Menu.NONE, SUBMENU_MENU1, Menu.NONE, "BlackBerry");
item3.add(Menu.NONE, SUBMENU_MENU2, Menu.NONE, "Windows Phone");
return super.onCreateOptionsMenu(menu);
}
Sub menu shows up when selecting “Others”



Awsome site! I am loving it!! Will come back again. I am taking your feeds also.
Just beneath, are many absolutely not connected websites to ours, nonetheless, they are surely worth going over.
just beneath, are a lot of completely not connected internet sites to ours, on the other hand, they’re certainly really worth going over
I just want to mention I am newbie to blogging and honestly liked you’re page. More than likely I’m planning to bookmark your site . You absolutely have exceptional posts. Bless you for sharing with us your web page.