Thursday, February 24, 2011

Working with Option Menus in Android

I've been working with android a lot these last days, mostly because I want to have my app in the android market before Google IO so at least I can say I have something to show :) I am developing a simple app just for starting and getting familiar with the platform.

Anyways, while doing the interface I noticed that it is really easy to create UI using xml especially menus. Android provides 3 kinds of menus: context menus, options menu and sub-menus. This post is about only Options Menu, I will cover the other 2 in later posts though.

Each activity has a single menu and it is created using the method onCreateOptionMenu, you can override this method and inflate the xml that contains the menu or you can add items manually, here is an example of both:
inflating the xml:
The menu structure is located in the noteslistmenu XML file. We are passing this file as a parameter.


adding them manually:


Basically both of them will generate the same menu but I prefer XML since it gives you the freedom to edit the file and add more items with out the need to re-compile the whole project again and it also allows you to add a lot more properties to your menu items.

This is how the menu looks like:

If you want to format the rows, you can group them or you can set the android:orderInCategory property. This property sets the order/priority of the items in the group and/or the menu.

This is another capture of this example and the XML file to generate it:
XMLmenu file

final result

No comments:

Post a Comment