Android menu

Archived

This page has been archived and will receive no further updates.

http://developer.android.com/guide/topics/ui/menus.html

Hello, Android p.61 -

  1. create res/menu/menu.xml
    <?xml version="1.0" encoding="UTF-8"?>
    <menu xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:id="@+id/menu_help"
            android:title="@string/menu_help_label"
            android:alphabeticShortcut="@string/menu_help_shortcut"
            android:icon="@android:drawable/ic_menu_help" />
        <item android:id="@+id/menu_about"
            android:title="@string/menu_about_label"
            android:alphabeticShortcut="@string/menu_about_shortcut"
            android:icon="@android:drawable/ic_menu_info_details" />
        <item android:id="@+id/menu_settings"
            android:title="@string/menu_settings_label"
            android:alphabeticShortcut="@string/menu_settings_shortcut"
            android:icon="@android:drawable/ic_menu_preferences" />
        <item android:id="@+id/menu_exit"
            android:title="@string/menu_exit_label"
            android:alphabeticShortcut="@string/menu_exit_shortcut"
            android:icon="@android:drawable/ic_menu_close_clear_cancel" />
    </menu>
    
  2. add menu labels to res/values/strings.xml
    <string name="menu_about_label">About</string>
    <string name="menu_about_shortcut">a</string>
    <string name="menu_exit_label">Exit</string>
    <string name="menu_exit_shortcut">x</string>
    <string name="menu_help_label">Help</string>
    <string name="menu_help_shortcut">h</string>
    <string name="menu_settings_label">Settings</string>
    <string name="menu_settings_shortcut">s</string>
    
  3. in your main Activity, override onCreateOptionsMenu to inflate the menu
    /* Inflates the menu */
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu, menu);
        return true;
    }
    
  4. in your main Activity, override onOptionsItemSelected to handle menu selections
    /* Handles menu selections */
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.menu_about:
            startActivity(new Intent(this, About.class));
            return true;
        case R.id.menu_exit:
            finish();
            return true;
        }
        return false;
    }