Search code examples
androidandroid-studioandroid-actionbarandroid-actionbar-compat

Action Bar Menu Items Duplicating on Run


I am very new to Java Programming and Android Studio. While creating an Options Menu, I created a list of options in the main menu with two items in a group sub menu.

This is my menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    >
    <item
        android:id="@+id/giveup_setting"
        android:orderInCategory="1"
        app:showAsAction="ifRoom"
        android:title="@string/action_option1"/>
    <item
        android:id="@+id/new_game_setting"
        android:orderInCategory="2"
        app:showAsAction="never"
        android:title="@string/action_option2"/>
    <item
        android:id="@+id/help_setting"
        android:orderInCategory="3"
        app:showAsAction="never"
        android:title="@string/action_option3"/>

    <item
        android:id="@+id/settings_setting"
        android:orderInCategory="4"
        app:showAsAction="never"
        android:title="@string/action_option4" />
    <menu>
    <group android:checkableBehavior="single">
        <item
            android:id="@+id/menu_color"
            android:orderInCategory="1"
            app:showAsAction="never"
            android:title="@string/layout_color"/>
        <item
            android:id="@+id/menu_text"
            android:orderInCategory="2"
            app:showAsAction="never"
            android:title="@string/layout_text"/>
    </group>
    </menu>
    <item
        android:id="@+id/about_setting"
        android:orderInCategory="5"
        app:showAsAction="never"
        android:title="@string/action_option5"/>
    <item
        android:id="@+id/exit_setting"
        android:orderInCategory="6"
        app:showAsAction="never"
        android:title="@string/action_option6"/>

</menu>

An this is how I inflate the menu in my MainActivity:

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        menu.clear();
        getMenuInflater().inflate(R.menu.menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem menu) {
        // Handle item selection
        switch (menu.getItemId()) {
            case R.id.help_setting:
                //show help screens

            return true;

            case R.id.menu_color:
                //change a layout of my main activity
            return true;

            case R.id.menu_text:
                   //change another layout of my main activity
            return true;*/

            case R.id.about_setting:
                   //show my about screen
            return true;

            case R.id.giveup_setting:
                //resets the game

            case R.id.new_game_setting:
                //restarts the app
                return true;

            case R.id.exit_setting:
                //exits the app
                return true;
            default:
                return super.onOptionsItemSelected(menu);
        }

}

On running the app, the menu populates but the "Settings" item (which has the nested group radio buttons) displays twice.

On running the app, the menu populates but the "Settings" item (which has the nested group radio buttons) displays twice.

What am I doing wrong?


Solution

  • if android:id="@+id/settings_setting" is the one you want to have a submenu don't close the item tag. Instead inflate <menu> inside the <item> tag.

    <item
      android:id="@+id/settings_setting"
      android:orderInCategory="4"
      app:showAsAction="never"
      android:title="@string/action_option4">
      <menu>
        <group android:checkableBehavior="single">
            <item
                android:id="@+id/menu_color"
                android:orderInCategory="1"
                app:showAsAction="never"
                android:title="@string/layout_color"/>
            <item
                android:id="@+id/menu_text"
                android:orderInCategory="2"
                app:showAsAction="never"
                android:title="@string/layout_text"/>
        </group>
      </menu>
    </item>