Download Teach Yourself Android Application Development in 24

Transcript
Working with Other Menu Types
139
<menu
xmlns:android=”http://schemas.android.com/apk/res/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>
</menu>
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.
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.
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(R.menu.gameoptions, menu);
menu.findItem(R.id.help_menu_item).setIntent(
new Intent(this, QuizHelpActivity.class));
menu.findItem(R.id.settings_menu_item).setIntent(
new Intent(this, QuizSettingsActivity.class));
return true;
}
Did you
Know?