MonoDroid – creating an options menu

One of the standard Android buttons that is required to be on every phone is the “menu” button. This brings up a little options menu on the bottom of the screen with icons and text labels that perform some action. For instance, on the main screen of the official Twitter app, you’ll get an options menu with two buttons: “Settings” and “Sign out”.

It’s easy to add an options menu with MonoDroid.

In the activity class, just override two methods: OnCreateOptionsMenu and OnOptionsItemSelected. The first one will construct a menu when the user hits the menu button. The second will respond to the action they clicked. Here’s how my code looks right now:

OnCreateOptionsMenu

The “Add” method overload on the IMenu that I’m using takes arguments: groupId, itemId, order, title, respectively.  GroupId and ItemId almost seem arbitrary to me at this point.  Order is the numeric order of how the options will be displayed, and title is the text that will appear with the icon.

Note that there is no Add overload with a parameter to specify the icon.  I added a refresh icon to the project (since MonoDroid doesn’t seem to have Android.R.Drawable.IcMenuRefresh yet for some reason) named “ic_menu_refresh.png”, so once I get the option added, I turn right around and give it an icon with the SetIcon method.

OnOptionsItemSelected

I’m switching on the title of the option selected, which may or may not be good practice, but it works fine.  You could also switch on the ItemId (see above) if you want.  Note that the “ToS” is just an extension method that I wrote because I was tired of getting jerked around by the weird Java string/ToString stuff.  The “Refresh” option only calls the Refresh method that I’m using elsewhere.  Finally, while the default case might never happen, notice that this method returns a “bool”, so it’s a good catch-all that leaves the rest of the work to the super class.

Android Emulator - option menu

That’s option menus in a quick nutshell.  An easy way to add common functionality without junking up your layout.  Feel free to take a look at the complete code at my MonodroidStockPortfolio project on GitHub.

Add a comment »4 comments to this article

  1. This does not work… Can you post a new one with the latest version of Monodroid being used? Thanks!

    Reply

    • What exactly doesn’t work? You can check out the source code at the GitHub link there.

      Reply

  2. Replace my resources with your own..

    public override bool OnCreateOptionsMenu(IMenu menu)
    {
    var item = menu.Add(0, 1, 1, Resource.String.Hello);
    item.SetIcon(Resource.Drawable.Icon);
    return true;
    }

    public override bool OnOptionsItemSelected(IMenuItem item)
    {

    switch (item.ItemId)
    {
    case 1:

    return true;
    default:
    return base.OnOptionsItemSelected(item);
    }
    }

    Reply

  3. Hi Matthew,

    Following link also explains how to add options menu in Mono for Android:

    http://monodroyd.wordpress.com/2011/12/31/creating-an-options-menu-in-mono-for-android-applications/

    Reply

Copyright © All Rights Reserved · Green Hope Theme by Sivan & schiy · Proudly powered by WordPress