Setting Up the Menu

Now that you have a view and an activity, you can move on to step 3: overriding the onCreateOptionsMenu and setting up the menu programmatically (see Listing 5-8).

chapter 5 ■ working with menus and dialogs

Listing 5-8. Setting Up the Menu Programatically ^Override public boolean onCreateOptionsMenu(Menu menu) {

//call the parent to attach any system level menus super.onCreateOptionsMenu(menu);

this.myMenu = menu;

//add a few normal menus addRegularMenultems(menu);

//add a few secondary menus add5SecondaryMenuItems(menu);

//it must return true to show the menu //if it is false menu won't show return true;

The code in Listing 5-8 first calls the parent onCreateOptionsMenu to give the parent an opportunity to add any system-level menus. Note that in releases 1.0, 1.1, and 1.5 of the Android SDK, this method does not add new menu items. The code then remembers the Menu object in order to manipulate it later for demonstration purposes. After that, the code proceeds to add a few regular menu items and a few secondary menu items.

0 0

Post a comment