How to Add Menu Items to Android Apps

I’ve been looking at the new Android phones and a lot of them do not have keyboards. I don’t know why the industry is still playing “me too” with iPhone-like interfaces. But as an app developer, I have to be aware of this issue. Considering that keyboard-less android phones have few buttons, the menu button becomes more important. That meant I had to get past a developer hurdle – learning how to add menus to my android apps.

If you’ve seen my apps from last year, you might have noticed that most of them had a menu item… one… singular… a menu item. Even though I read Google’s android documentation, I couldn’t figure out how to add more than one menu item to an app.

  • http://developer.android.com/guide/topics/ui/menus.html

I dropped the menu code into my eclipse project. Naturally, errors appeared and they were highlighted in red. I trimmed down the code until the errors disappeared.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
    /* Creates the menu items */
    public boolean onCreateOptionsMenu(Menu menu) {
        menu.add(0, MENU_CONTACT, 0, "little screens for big discussions!");
        return true;
    }
 
    /* Handles item selections */
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
    	case MENU_CONTACT:
    		mWebView.loadUrl("file:///android_asset/contact.html");
            return true;
        }
        return false;
    }

The problem was with the top… not the code here… near the top of my Java file. You can’t just expect to paste in code and have an android app work… ha ha… no… that would be way to easy. This was the line…

1
    private static final int MENU_CONTACT = 0;

What does that mean?! I have no idea. I let eclipse fix the errors and it dropped that code into my app. Eclipse highlighted MENU_CONTACT with a red squiggly line. The balloon pop-up stated that, “MENU_CONTACT cannot be resolved.” I chose one of the quick fixes. I created a constant called ‘MENU_CONTACT’ and it put a new line of code near the top of my java file. But when I tried to add another menu item, with a separate case, More red squiggly lines appeared. The error stated, “duplicate case” with no additional instructions.

Eclipse cannot resolve MENU_CONTACT... but can you resolve it?!
Eclipse cannot resolve MENU_CONTACT... but can you resolve it?!

I clicked around, trying to figure things out. Eventually, I decided to change the number in the “private static final int MENU_CONTACT” line. I changed it from zero to one… and I gave the second menu item constant a different number… the number two. Could something so simple work? Why yes, it did. Soon after, I launched my app with a new menu and even sub menus. How does it work? I DON’T KNOW! What is a private static final int and why does it need a different number? It is a mystery to me, but my apps seem to work. There were 22 constants in the finished app.

Basically, it seems that I had to number my constants. That sounds weird, doesn’t it? I was happy by this accomplishment, but it’s not something I can throw into normal conversation.

“Hi Mike, how are you?!”
“I numbered my constants, allowing me to add multiple options to my Android app menu.”

Anyway, if you’re having trouble understanding Google’s menu tutorial, this might be something to consider. I was having a hard time with this problem, until I realized what the “duplicate case” error meant.

2 thoughts on “How to Add Menu Items to Android Apps”

  1. The MENU_CONTACT is just used with the case to select what action to preform when it is clicked on. Defining them as integers with variable names is easier than trying to keep track of which integer relates to which option menu, especially on larger menus.

    So the following sets up the variable (static meaning it doesn’t change throughout the program, a constant):
    private static final int MENU_CONTACT = 0;

    The following sets the action when the menu is pressed, pass MENU_CONTACT (0) to our next function.
    public boolean onCreateOptionsMenu(Menu menu) {
    menu.add(0, MENU_CONTACT, 0, “little screens for big discussions!”);
    return true;
    }

    This function then uses a switch with the integer passed (in this case MENU_CONTACT) which is zero, and so the mWebView will load a webpage.

    public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case MENU_CONTACT:
    mWebView.loadUrl(“file:///android_asset/contact.html”);
    return true;
    }
    return false;
    }

    Hope that helps.

  2. A private static final int is just an int with a few different properties. I hope the private already makes sense, but the static modifier means the int belongs to the class rather than objects of that class. So consider this class:

    class Foo
    {
    public static final int BAR = 42;

    public void someMethod ()
    {
    System.out.println(BAR);
    }
    }

    Since BAR is public in my example, you can access it from other classes like someVariable = Foo.BAR;. Also, inside the class Foo, you can access BAR without typing the “Foo” part.

    Contrary to what Mat said, a static variable can be changed. What makes a static final int non-changeable is the “final” modifier.

    The real point about the menus is that Android keeps track of the menu items by assigning an int, which you supply, to each of them. So each menu item should have a different int assigned to it. The easiest way for the programmer to keep track of all the ints they’re using for each possible menu item, like Mat said, is to define them as final ints. Then in your onOptionsItemSelected, you can test which menu item was selected by testing the value of item.getItemId().

    Kinda late, but hope this helps, too.

Leave a Reply