Having two groups with one choice in ActionBar doesn't work, but adding a popup menu instead doesn't work

I am writing an Android application where the user must choose how and what to display on the chart. These parameters are expressed in two groups of menus with one choice (switches), both of which should be accessible from the action bar.

The first group works great. It is added at the end of my ActionBar XML as follows:

<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, the two merges merge into one selection list. In other words, the parameters from both lists are displayed together, and if I select the parameter related to the first list, I cannot select any of the second.

Instead, I want two separate switch lists . My next idea was to add another ActionBar button that, when pressed, launched a popup menu . But when I click the button, I get an IllegalStateException saying that my "MenuPopupHelper cannot be used without binding."

Here is my popup menu code:

In my ActionBar XML file:

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

My new XML menu:

 <?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 to create two sets of checked menu items that are both bound to an ActionBar?

+4
source share
1 answer

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

 <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 additional handler code or pop-up menu is required to display such a menu.

+19
source

Source: https://habr.com/ru/post/1435178/


All Articles