Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Having two single-selection groups in ActionBar doesn't work, but attaching a pop up menu instead doesn't work either

I am writing an Android app where the user must choose how and what to display on a graph. These options are expressed in two single-selection menu groups (radio buttons), both of which should be accessible from the action bar.

The first group works fine. It's added at the end of my ActionBar XML like this:

<group android:checkableBehavior="single" android:showAsAction="never" >
    <item android:id="@+id/menu_choice_1" android:title="Choice 1" />
    <item android:id="@+id/menu_choice_2" android:title="Choice 2" android:checked="true"/>
</group>

When I add a second <group> below the first one, however, the two merge into one single-selection list. In other words, the options from both lists are rendered together and if I choose an option pertaining to the first list, I cannot choose anything from the second.

Instead, I want two separate lists of radio buttons. My next idea was to add another button the ActionBar that, when clicked, would launch a pop-up menu. But when I click the button, I get an IllegalStateException, saying that my "MenuPopupHelper cannot be used without an anchor".

Here is my attempted pop-up menu code:

In my ActionBar XML:

<item android:id="@+id/menu_openothermenu"
  android:title="@string/openothermenustr"
  android:showAsAction="always" />

My new menu XML:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <group android:checkableBehavior="single">
        <item android:id="@+id/menu_2_choice_1" android:title="@string/2_choice_1" />
        <item android:id="@+id/menu_2_choice_2" android:title="@string/2_choice_2" android:checked="true"/>
    </group>
</menu>

Code in my Activity:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
    SharedPreferences.Editor editor;

    switch (item.getItemId()) {
    case R.id.openothermenu:
        Menu m = (Menu) findViewById(R.menu.other_menu);
        PopupMenu popup = new PopupMenu(this, findViewById(R.menu.main_menu));
        popup.setOnMenuItemClickListener(this);
        MenuInflater inflater = popup.getMenuInflater();
        inflater.inflate(R.menu.other_menu, popup.getMenu());
        /* This commented block doesn't work either, and prevents execution
        // Restore saved chosen value
        int chosen = settings.getInt(MENU_2_PREFS, -1);
        switch(chosen)
        {
            case 1:
                m.findItem(R.id.menu_1_choice_1).setChecked(true);
                updateVisibleThings();
                break;
            default:
            case 2:
                m.findItem(R.id.menu_2_choice_2).setChecked(true);
                updateOtherVisibleThings();
                break;
        }
        */
        popup.show();
        return true;
    default:
        return super.onOptionsItemSelected(item);
    }
}

@Override
public boolean onMenuItemClick(MenuItem item) {
    SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
    SharedPreferences.Editor editor;

    switch(item.getItemId()) {
    case R.id.menu_2_choice_1:
        if (item.isChecked()) item.setChecked(false);
        else item.setChecked(true);
        updateVisibleThings();

        // save chosen setting
        editor = settings.edit();
        editor.putInt(MENU_2_PREFS, 1);
        editor.commit(); // Commit the edits!

        return true;
    case R.id.menu_2_choice_2:
        if (item.isChecked()) item.setChecked(false);
        else item.setChecked(true);
        updateOtherVisibleThings();

        // save chosen setting
        editor = settings.edit();
        editor.putInt(MENU_2_PREFS, 2);
        editor.commit(); // Commit the edits!

        return true;
    default:
        return true;
    }
}

How can I create two sets of checkable menu items such that both are attached to the ActionBar?

like image 885
Maxim Zaslavsky Avatar asked Sep 20 '12 02:09

Maxim Zaslavsky


People also ask

How do you make a menu item invisible?

Call invalidateOptionsMenu() when you want to hide the option.

How do you add action items to the action bar in Android?

All action buttons and other items available in the action overflow are defined in an XML menu resource. To add actions to the action bar, create a new XML file in your project's res/menu/ directory. The app:showAsAction attribute specifies whether the action should be shown as a button on the app bar.

Where is action bar in Android?

Android ActionBar is a menu bar that runs across the top of the activity screen in android. Android ActionBar can contain menu items which become visible when the user clicks the “menu” button.


1 Answers

I found an elegant way to solve this that was unfortunately not in the documentation. Add the following code to your ActionBar menu XML:

<item android:id="@+id/menu_openothermenu"
      android:title="@string/openothermenustr"
      android:showAsAction="always">
    <menu>
        <group android:checkableBehavior="single">
            <item android:id="@+id/menu_2_choice_1" android:title="@string/2_choice_1"  android:showAsAction="never" />
            <item android:id="@+id/menu_2_choice_2" android:title="@string/2_choice_2"  android:showAsAction="never" android:checked="true" />
        </group>
     </menu>
</item>

No extra handler code or popup menu implementation is necessary for such a menu to appear.

like image 191
Maxim Zaslavsky Avatar answered Nov 09 '22 11:11

Maxim Zaslavsky