0

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 Answer 1

2

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

2
  • 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
    Commented Apr 7, 2010 at 21:57
  • 1
    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 (stackoverflow.com/questions/2478517/…). If you want something with lists, read through the Android Developers' resources on Dialogs (developer.android.com/guide/topics/ui/dialogs.html#AlertDialog). Commented Apr 7, 2010 at 22:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.