views:

451

answers:

1

I need a very simple menu which probably contains only one or two items: settings/options, where pressing one of them should show some customer defined parameters (is it called dialog), e.g., number of results shown. Is there any good tutorial on creating such kind of menus? I've looked at the "notepad" example in android, it doesn't really help.

+1  A: 

Depending on what you're asking for, these are either "Options Menus" or "Context Menus", and creating them is very easy. Here's a link to the page on the Developers' Website explaining how to do menus.

Here's a basic example of code for options menus, adapted from my game:

public boolean onCreateOptionsMenu(Menu menu){
    // Define your menu, giving each button a unique identifier numbers
    // (MENU_PAUSE, etc)
    // This is called only once, the first time the menu button is clicked
    menu.add(0, MENU_PAUSE, 0, "Pause").setIcon(android.R.drawable.ic_media_pause);         
    menu.add(0, MENU_RESUME, 0, "Resume").setIcon(android.R.drawable.ic_media_play);
    return true;
}


public boolean onPrepareOptionsMenu(Menu menu){
    // This is called every time the menu button is pressed. In my game, I
    // use this to show or hide the pause/resume buttons depending on the
    // current state
}


public boolean onOptionsItemSelected(MenuItem item){
    // and this is self explanatory
    boolean handled = false;

    switch (item.getItemId()){
    case MENU_PAUSE:
        pauseGame();
        handled = true;
        break;

    case MENU_RESUME:
        resumeGame();
        handled = true;
        break;
    }
    return handled;
}

Edit: See the comments for some details on AlertDialogs

Steve H
The thing is, I want another dialog/menu to pop-up after the users press the setting option. What is the standard way of doing that?
Yang
Ah, then it seems that you want to create an `AlertDialog` after the user presses something in your menu. AlertDialogs can have simple text and buttons, lists with checkboxes or things like that. For an example of a simple yes/no AlertDialog, have a look at this answer (http://stackoverflow.com/questions/2478517/how-to-display-a-yes-no-dialog-box-in-android/2478662#2478662). If you want something with lists, read through the Android Developers' resources on Dialogs (http://developer.android.com/guide/topics/ui/dialogs.html#AlertDialog).
Steve H