sexta-feira, 2 de novembro de 2012

Menus in Android



Hello everyone.

In this post, it'll be seen the concept of Menus on Android.

In devices that have Android OS, usually there are 3 physical buttons: Home, Back and Menu. The Home button is the button that returns to the home screen and, when held, shows the user all currently open applications. The Back button is the button that exits the current screen of the system. And the Menu button is a button that calls specific functions in each application. This varies from application to application.

There are several types of menu on Android:


  • Options Menu → This is the most common type of menu in Android. For him to be called, simply click the menu physical button.
  • Context Menu → This menu is called when we click and hold a particular visual component of Android, such as a button. In doing so, it will be shown on the screen a list of options, which is our context menu.
  • PopUp Menu → This is the most current implementation of menu. This menu is shown on Action Bar, toolbars that are shown at the top of the screen. The Action Bar in Android were created in the API 11 (3.0 or Honeycomb), and since then appears in all subsequent versions of API's. These menus appear in the Action Bar, and Android devices that have no physical button menu.


These types of menu will be presented below, divided into 3 different sections.

Options Menu
A OptionMenu is shown below.


It can be called by both by Java source code, as by an XML file. In both cases, the implementation of a menu is made by the method public boolean onCreateOptionsMenu(Menu menu). The creation of a menu via Java is shown below:


In the above source code, we can see 4 arguments in the method MenuItem.add:


  • group → Used to define in which group the menu should appear. Usually this value is 0.
  • id → Identifier of the menu item. This parameter is used to differentiate the actions generated by the menu. Each event must perform a different action, and for that the id of the item can be used. It is usually created a constant to identify each item.
  • order → Indicates the order of this item, or 0 if the order is not important.
  • text → Represents the text of this menu option.


And now, below we have the creation of the menu options via XML.


And how it's referenced in Java.


To invoke this menu, simply click the Menu physical button. Then, the menu options are shown on the screen. And for some action to occurs when you click a menu option, we need to use another event, called public boolean onOptionsItemSelected(MenuItem item). The declaration of this method is shown below.


And the execution of this action, below.


And it's done! We already know how to create an OptionsMenu.

Context Menu
This type of menu is activated when we click and hold on any visual component, such as a button. A ContextMenu is shown below.


To invoke a ContextMenu, we need to declare some methods in the source code of Java. The image below shows a source code.


Note that in this source code we have new events and methods:


  • registerForContextMenu(mButton) → With this method, we recorded which visual component will receive the context menu event.
  • public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo)
    With this method, we do a similar work to the method onCreateOptionMenu (report which XML file contains the menu options that will be triggered by context menu).
  • public boolean onContextItemSelected(MenuItem item) → Faz o trabalho análogo ao método onOptionsItemSelected. Ou seja, chama a ação que a opção de menu clicada possui.
    Does the analogous work of the method onOptionsItemSelected.


There is not much secret about the ContextMenu.

PopUp Menu
So far, we have learned how to call a OptionsMenu and a ContextMenu. Both can be used at the same time. However, with the PopUpMenu, the situation is somewhat different.

A PopUpMenu is shown below:


The PopUpMenu is a menu that is shown in the ActionBar of an Activity. A ActionBar is a component that was created from the Android API 11 (3.0 or Honeycomb). It's toolbars shown at the top of the screen. Since they were created, they appear in all subsequent API versions.

However, with the creation of this menu, the OptionsMenu stopped working. In Android versions higher than 3.0, OptionsMenu no longer work natively. There are ways to make it work, but they are not standard.

For we use a PopUpMenu, we just need to be in a project with API 3.0 (or higher), and use the same methods and events explained above in the section on OptionsMenu.

For further informations, a good tip is this link.

-----

That's all. Now we can call our menus in Android applications. There are many other menu options, but the main (and most used) were exposed in this topic.

If someone has a question, suggestion or criticism, feel at ease.