Controlling the Map with Menu Buttons

We want to give the user the ability to turn on satellite, traffic, and street views of the map. In addition, we'll throw in a few menu buttons to enable zooming and another way of getting to the Jobs List.

Android has a sophisticated set of menu capabilities that includes three types of menus (options, context, and submenus), each with its own capabilities, icon menu buttons, and other advanced features. We just use text-based menu buttons, and so we need to do two things:

1. Create the menu of buttons that will be displayed.

2. Catch the menu events and invoke appropriate actions.

The following code creates the menu in /**

* Set up menus for this page

* @see

@Override public boolean onCreateOptionsMenu(Menu menu) {

boolean supRetVal = super.onCreateOptionsMenu(menu); menu.add(Menu.NONE, 0, Menu.NONE, getString(R.string.map_menu_zoom_in)); menu.add(Menu.NONE, 1, Menu.NONE, getString(R.string.map_menu_zoom_out)); menu.add(Menu.NONE, 2, Menu.NONE, getString(R.string.map_menu_set_satellite)); menu.add(Menu.NONE, 3, Menu.NONE, getString(R.string.map_menu_set_map)); menu.add(Menu.NONE, 4, Menu.NONE, getString(R.string.map_menu_set_traffic)); menu.add(Menu.NONE, 5, Menu.NONE, getString(R.string.map_menu_show_list)); return supRetVal;

We create menu buttons by overriding the onCreateOptionsMenu method, where we are passed a menu parameter for the Activity's menu. After dutifully allowing the superclass a chance to do what it needs to do, we simply add items (buttons) to the menu using menu.add. The version of menu.add that we've chosen takes four parameters:

int groupid

Android allows you to group menu items so you can quickly change the whole menu at once. We don't have a need for that in MicroJobs, so Menu.NONE says we don't need it. int itemid

We need a unique identifier for this menu item so we can tell later whether it was picked.

int order

The itemid we defined in the second parameter does not imply order. If we cared about the order in which the items were presented, we'd do that with this parameter. Since we don't care, we use Menu.NONE again. int titleRes

The ID of the string resource we want to use for the button title. Note that this is an integer, not a string, so the menu strings need to be predefined in string.xml, under the res directory. You recall that Android takes care of compiling the strings in res/strings.xml into a .java file ( that assigns an integer to each string. The getString method retrieves that integer for you (despite the name, the method returns an integer, not a string).

To catch the menu events, we override the onOptionsItemSelected method: /**

* @see

@Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case 0:

// Toggle satellite views mvMap.setSatellite(!mvMap.isSatellite()); return true; case 3:

// Toggle street views mvMap.setStreetView(!mvMap.isStreetView()); return true; case 4:

// Toggle traffic views mvMap.setTraffic(!mvMap.isTraffic()); return true; case 5:

// Show the job list activity startActivity(new Intent(MicroJobs.this, MicroJobsList.class)); return true;

return false;

We are passed the selected MenuItem, and the switch has a case for each button that we defined for the menu. The code for each case is similar to code that we've seen before.

0 0

Post a comment