Providing Users with Options and Context Menus Android

You need to be aware of two special application menus for use within your Android applications: the options menu and the context menu.

Enabling the Options Menu

The Android SDK provides a method for users to bring up a menu by pressing the menu key from within the application.You can use options menus within your application to bring up help, to navigate, to provide additional controls, or to configure options. The OptionsMenu control can contain icons, submenus, and keyboard shortcuts.

An options menu.

An options menu

For an options menu to show when a user presses the Menu button on their device, you need to override the implementation of onCreateOptionsMenu() in your Activity. Here is a sample implementation that gives the user three menu items to choose from:

For each of the items that are added, we also set a built-in icon resource and assign an Intent to each item. We give the item title with a regular text string, for clarity. You can use a resource identifier, as well. For this example, there is no other handling or code needed. When one of these menu items is selected, the Activity described by the Intent starts.

This type of options menu can be useful for navigating to important parts of an application, such as the help page, from anywhere within your application. Another great use for an options menu is to allow configuration options for a given screen. The user can configure these options in the form of checkable menu items. The initial menu that appears when the user presses the menu button does not support checkable menu items. Instead, you must place these menu items on a SubMenu control, which is a type of Menu that can be configured within a menu. SubMenu objects support checkable items but do not support icons or other SubMenu items. Building on the preceding example, the following is code for programmatically adding a SubMenu control to the previous Menu:

This code would be inserted before the return statement in the implementation of the onCreateOptionsMenu()method. It adds a single menu item with an icon to the previous menu, called “Style.”When the “Style” option is clicked, a pop-up menu with the two items of the SubMenu control is displayed. These items are grouped together and the checkable icon, by default, looks like the radio button icon. The checked state is assigned during creation time.

To handle the event when a menu option item is selected, we also implement the onOptionsItemSelected() method, as shown here:

This method must call the super class’s onOptionsItemSelected() method for basic behavior to work. The actual MenuItem object is passed in, and we can use that to retrieve the identifier that we previously assigned to see which one was selected and perform an appropriate action. Here, we switch the values and return. By default, a Menu control goes away when any item is selected, including checkable items. This means it’s useful for quick settings but not as useful for extensive settings where the user might want to change more than one item at a time.

As you add more menu items to your options menu, you might notice that a “More” item automatically appears. This happens whenever more than six items are visible. If the user selects this, the full menu appears. The full, expanded menu doesn’t show menu icons and although checkable items are possible, you should not use them here. Additionally, the full title of an item doesn’t display. The initial menu, also known as the icon menu, shows only a portion of the title for each item. You can assign each item a condensedTitle attribute, which shows instead of a truncated version of the regular title. For example, instead of the title Instant Message, you can set the condensedTitle attribute to “IM.”

Enabling the ContextMenu

The ContextMenu is a subtype of Menu that you can configure to display when a long press is performed on a View. As the name implies, the ContextMenu provides for contextual menus to display to the user for performing additional actions on selected items.

ContextMenu objects are slightly more complex than OptionsMenu objects. You need to implement the onCreateContextMenu() method of your Activity for one to display. However, before that is called, you must call the registerForContextMenu() method and pass in the View for which you want to have a context menu. This means each View on your screen can have a different context menu, which is appropriate as the menus are designed to be highly contextual.

Here we have an example of a Chronometer timer, which responds to a long click with a context menu:

After the call to the registerForContextMenu() method has been executed, the user can then long click on the View to open the context menu. Each time this happens, your Activity gets a call to the onCreateContextMenu() method, and your code creates the menu each time the user performs the long click.

The following is an example of a context menu for the Chronometer control, as previously used:

Recall that any View control can register to trigger a call to the onCreateContextMenu() method when the user performs a long press. That means we have to check which View control it was for which the user tried to get a context menu. Next, we inflate the appropriate menu from a menu resource that we defined with XML. Because we can’t define header information in the menu resource file, we set a stock Android SDK resource to it and add a title. Here is the menu resource that is inflated:

This defines three menu items. If this weren’t a context menu, we could have assigned icons. However, context menus do not support icons, submenus, or shortcuts.

Now we need to handle the ContextMenu clicks by implementing the onContext Item Selected() method in our Activity. Here’s an example:

Because we have only one context menu in this example, we find the Chronometer view for use in this method. This method is called regardless of which context menu the selected item is on, though, so you should take care to have unique resource identifiers or keep track of which menu is shown. This can be accomplished because the context menu is created each time it’s shown.

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd Protection Status

Android Topics