Adding an Options Menu to the Game Screen

To add an options menu to the game screen, you need to add a special type of resource called a menu resource. You can then change QuizGameActivity class to enable an options menu and handle menu selections.

Adding Menu Resources

For your options menu, you create a menu definition resource in XML and save it to the /res/menu resource directory as gameoptions.xml.

A menu resource contains a <menu> tag followed by a number of <item> child elements. Each <item> element represents a menu option and has a number of attributes. The following are some commonly used attributes:

► id—This attribute allows you to easily identify the specific menu item.

► title—This attribute is the string shown for the options menu item.

► icon—This is a drawable resource representing the icon for the menu item.

Your options menu will contain only two options: Settings and Help. Therefore, your gameoptions.xml menu resource is fairly straightforward:

<menu xmlns:android=""> <item android:id="@+id/settings_menu_item" android:title="@string/menu_item_settings"

android:icon="@android:drawable/ic_menu_preferences"></item> <item android:id="@+id/help_menu_item" android:title="@string/menu_item_help" android:icon="@android:drawable/ic_menu_help"></item>

You set the title attribute of each menu option by using the same String resources you used on the main menu screen. Note that instead of adding new drawable resources for the options menu icons, you use built-in drawable resources from the Android SDK to have a common look and feel across applications.

Did you Know?

Adding an Options Menu to an Activity

For an options menu to show when the user presses the Menu button on the game screen, you must provide an implementation of the onCreateOptionsMenu() method in the QuizGameActivity class. Specifically, you need to inflate (load) the menu resource into the options menu and set the appropriate Intent information for each menu item. Here is a sample implementation of the onCreateOptionsMenu() method for QuizGameActivity: ©Override public boolean onCreateOptionsMenu(Menu menu) { super.onCreateOptionsMenu(menu);

getMenuInflater().inflate(, menu); menu.findItem( new Intent(this, QuizHelpActivity.class)); menu.findItem( new Intent(this, QuizSettingsActivity.class)); return true;

You can use the built-in drawable resources provided in the android.R.drawable class just as you would use resources you include in your application package. If you want to see what each of these shared resources looks like, check the Android SDK installed on your machine. Specifically, browse the /platforms directory, choose the appropriate target platform, and check its /data/res/drawable directory.

Handling Options Menu Selections

To listen for when the user launches the options menu and selections a menu option, you implement the onOptionsItemSelected() method of the activity. For example, you start the appropriate activity by extracting the intent from the menu item selected as follows:

@Override public boolean onOptionsItemSelected(MenuItem item) { super.onOptionsItemSelected(item); startActivity(item.getIntent()); return true;

Did you Know?

The method given here for handling onOptionsItemSelected() works as designed. It's not technically required if the only thing your menu will do is launch the Intent set via the setIntent() method. However, to add any other functionality to each MenuItem requires the implementation of this method.

There you have it: You have created an options menu on the game screen. If you save the class and run the application once more, you will see that you can navigate to the game screen, press the Menu button, and use a fully functional options menu (see Figure 8.5).


The Been There, Done That! game screen with an options menu.

Was this article helpful?

0 0

Post a comment