views:

100

answers:

1

hi,

can any one guide me how to add some menu related buttons at the bottom of each activity?

+5  A: 

Do you mean the Options Menu?

alt text

If so, you'll need to add some code like this to your Activity:

private static final int MENU_SEARCH = Menu.FIRST;
private static final int MENU_PREFERENCES = Menu.FIRST + 1;
private static final int MENU_HELP = Menu.FIRST + 2;

/* Creates the menu items */
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    menu.add(Menu.NONE, MENU_SEARCH, Menu.NONE, "Search")
            .setIcon(android.R.drawable.ic_menu_search);
    menu.add(Menu.NONE, MENU_PREFERENCES, Menu.NONE, "Preferences")
            .setIcon(android.R.drawable.ic_menu_preferences);
    menu.add(Menu.NONE, MENU_HELP, Menu.NONE, "Help")
            .setIcon(android.R.drawable.ic_menu_help);
    return true;
}

/* Handles item selections */
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case MENU_SEARCH:
            search();
            return true;
        case MENU_PREFERENCES:
            preferences();
            return true;
        case MENU_HELP:
            showHelp();
            return true;
    }

    return super.onMenuItemSelected(featureId, item);
}

Note that Menu.add() returns the MenuItem created so you can chain the call to setIcon().

Dave Webb