(Android phone – how-to/example)
Maybe you’ve noticed the menu that appears when you are in desktop and press “menu-key”, almost all applications implement this feature, and it looks pretty nice. If you want to include it in your code, this is how.
First override the onCreateOptionsMenu.
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
super.onCreateOptionsMenu(menu);
menu.add(PREFERENCES_GROUP_ID, SETTINGS_ID, 0, R.string.menu_settings)
.setIcon(android.R.drawable.ic_menu_preferences);
menu.add(PREFERENCES_GROUP_ID, HELP_ID, 0, R.string.menu_help)
.setIcon(android.R.drawable.ic_menu_help);
menu.add(PREFERENCES_GROUP_ID, FEEDBACK_ID, 0, R.string.menu_feedback)
.setIcon(android.R.drawable.ic_menu_send);
menu.add(PREFERENCES_GROUP_ID, ABOUT_ID, 0, R.string.menu_about)
.setIcon(android.R.drawable.ic_menu_info_details);
return true;
}
In this example we just add 4 items to the menu (preferences, help, feedback and about). Each item has a specific icon. There is a default set of icons, you may wish to use as all the other applications. In our example we used ic_menu_preferences for preferences, etc.. Note: that you can use your own icons as well.
Also for each item you have to add a default action.
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId()) {
case SETTINGS_ID: /* .. start settings activity .. */ break;
case HELP_ID: /* .. start help activity .. */ break;
case FEEDBACK_ID: /* .. start feedback activity .. */ break;
case ABOUT_ID: /* .. start about activity .. */ break;