Creating an Options Menu in Mono for Android Applications

All Android devices have the “Menu” button that displays the options menu on the bottom of the screen. Normally, the options menu provides additional functionality, navigation or settings for your application.

Creating an options menu in a Mono for Android application can be accomplished by overriding OnCreateOptionsMenu and OnOptionsItemSelected methods in your activity.

We will use the sample application code that we have previously created and used.

PRELIMINARY SETTINGS

First, set the titles (captions) for the options menu in the Strings.xml file so that we will be able to call them inside the activity file. We will display a Terms of Service page and close the application as our options:

And then, add new icons that will be displayed above the menu titles. We will use the following icons from Android’s own drawable resources to be able to demonstrate how to add an external icons to the menu options:

Add each of these PNG files by right-clicking on the Drawable folder:

Be sure that the Build Action property on both of these files are set to “AndroidResource”:

OVERRIDING THE METHODS

Each Android activity has its own menu that is created by the OnCreateOptionsMenu method. The OnOptionsItemSelected method handles the menu items events.

The menu.Add method has the following parameters:

GroupId: Groups menu options to apply configs on all items of a certain menu group
ItemId: A unique integer value to identify the menu option (menu item)
Order: Controls the numeric order of menu options in a single menu
TitleRes: The text (caption) of the menu option (as a resource identifier)

Notice that the OnCreateOptionsMenu method returns a boolean value. It has to return true for the menu to be visible. If it returns false, the menu will be hidden.

RUN via EMULATOR

Launch the emulator and deploy your application to your selected virtual device:

Note: How to accomplish this step was explained before in the “Launch Emulator and Debug” section of the “Hello World” Application post.

CONCLUSION

Menus are great to display additional options that are not directly visible on the main screen of any mobile application. This post showed how to create menu options on the bottom of the screen in a Mono for Android application.

About these ads

Leave a Comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

%d bloggers like this: