Login     Sign Up
Tugadar (@admin)
21 days ago
26 Views

Submenus and Context Menus: Context menus are displayed using the same floating window as the submenus shown in Figure 4-5.

While their appearance is the same, the two menu types are populated differently.

Creating Submenus

Submenus are displayed as regular Menu Items that, when selected, reveal more items. Traditionally, submenus are displayed using a hierarchical tree layout. Android uses a different approach to simplify menu navigation for small-screen devices. Rather than a tree structure, selecting a submenu presents a single floating window that displays all of its Menu Items.

You can add submenus using the addSubMenu method. It supports the same parameters as the add method used to add normal Menu Items, allowing you to specify a group, unique identifier, and text string for each submenu. You can also use the setHeaderIcon and setIcon methods to specify an icon to display in the submenu’s header bar or the regular icon menu, respectively.

The Menu Items within a submenu support the same options as those assigned to the icon or extended menus. However, unlike traditional systems, Android does not support nested submenus.

The code snippet below shows an extract from an implementation of the onCreateMenuOptions code that adds a submenu to the main menu, sets the header icon, and then adds a submenu Menu Item:

SubMenu sub = menu.addSubMenu(0, 0, Menu.NONE, “Submenu”); sub.setHeaderIcon(R.drawable.icon); sub.setIcon(R.drawable.icon);

MenuItem submenuItem = sub.add(0, 0, Menu.NONE, “Submenu Item”);

Using Context Menus

Context Menus are contextualized by the currently focused View and are triggered by pressing the trackball, middle D-pad button, or the View for around 3 seconds.

You define and populate Context Menus similarly to the Activity menu. There are two options available for creating Context Menus for a particular View.

Creating Context Menus

The first option is to create a generic Context Menu for a View class by overriding a View’s onCreateContextMenu handler as shown below:

@Override

public void onCreateContextMenu(ContextMenu menu) {

super.onCreateContextMenu(menu);

menu.add(“ContextMenuItem1”);

}

The Context Menu created here will be available from any Activity that includes this View class.

The more common alternative is to create Activity-specific Context Menus by overriding the onCreateContextMenu method and registering the Views that should use it. Register Views using registerForContextMenu and passing them in, as shown in the following code:

@Override

public void onCreate(Bundle icicle) {

super.onCreate(icicle);

EditText view = new EditText(this);

setContentView(view);

registerForContextMenu(view);

}

Once a View has been registered, the onCreateContextMenu handler will be triggered whenever a Context Menu should be displayed for that View.

Override onCreateContextMenu and check which View has triggered the menu creation to populate the menu parameter with the appropriate contextual items, as shown in the following code snippet:

@Override

public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {

super.onCreateContextMenu(menu, v, menuInfo);

menu.setHeaderTitle(“Context Menu”);

menu.add(0, menu.FIRST, Menu.NONE,

“Item 1”).setIcon(R.drawable.menu_item);

menu.add(0, menu.FIRST+1, Menu.NONE, “Item 2”).setCheckable(true); menu.add(0, menu.FIRST+2, Menu.NONE, “Item 3”).setShortcut(‘3’, ‘3’); SubMenu sub = menu.addSubMenu(“Submenu”); sub.add(“Submenu Item”);

}

As shown above, the ContextMenu class supports the same add method as the Menu class, so you can populate a Context Menu in the same way as Activity menus — including support for submenus — but icons will never be displayed. You can also specify the title and icon to display in the Context Menu’s header bar.

Android supports late runtime population of Context Menus using Intent Filters. This mechanism lets you populate a Context Menu by specifying the kind of data presented by the current View, and asking other Android applications if they support any actions for it. The most common example of this behav-ior is the cut/copy/paste Menu Items available on EditText controls. This process is covered in detail in the next chapter.

Handling Context Menu Selections

Context Menu Item selections are handled similarly to the Activity menu. You can attach an Intent or Menu Item Click Listener directly to each Menu Item, or use the preferred technique of overriding the onContextItemSelected method on the Activity.

This event handler is triggered whenever a Context Menu Item is selected within the Activity. A skel-eton implementation is shown below:

@Override

public boolean onContextItemSelected(MenuItem item) { super.onContextItemSelected(item);

... Handle menu item selection ... ] return false;

}