Android - problems with verifiable menu items

I read the instructions on the Android developers page to get the Check menu items:

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

this is my xmlmenu:

<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <group android:checkableBehavior="all"> <item android:id="@+id/regu" android:title="@string/Regulatory" /> <item android:id="@+id/warn" android:title="@string/Warning" /> <item android:id="@+id/temp" android:title="@string/Temporary" /> <item android:id="@+id/bicy" android:title="@string/Bicycle" /> </group> </menu> 

And here is my code:

  @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.regu: if (item.isChecked()) { item.setChecked(false); currAvailableOptions++; } else if(0 != currAvailableOptions) { item.setChecked(true); currAvailableOptions--; } return true; case R.id.warn: if (item.isChecked()) { item.setChecked(false); currAvailableOptions++; } else if(0 != currAvailableOptions) { item.setChecked(true); currAvailableOptions--; } return true; case R.id.temp: if (item.isChecked()) { item.setChecked(false); currAvailableOptions++; } else if(0 != currAvailableOptions) { item.setChecked(true); currAvailableOptions--; } return true; default: return super.onOptionsItemSelected(item); } } public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.app_menu, menu); return true; } 

The problem is that when I clicked one item, the menu item disappeared. No need to stay visible to check other menu items?

Any idea?

Greetings

+6
source share
4 answers

Visible items are displayed only in the submenu or in the context menu.

And with the submenu they (Google) mean:

Submenu A floating list of menu items that appears when the user touches a menu item that contains a submenu.

Since your menu items are not submenu items, they will not work.

+3
source

I know this is not a direct answer to your question, but please consider the following code instead of your switch, this may help you find the problem.

 public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.regu: case R.id.warn: case R.id.temp: if (item.isChecked()) currAvailableOptions++; else if(currAvailableOptions != 0) currAvailableOptions--; item.setChecked(!item.isChecked()); return true; default: return super.onOptionsItemSelected(item); } } 
+2
source

What are currAvailableOptions? I looked at the article you contacted and there was nothing there. It would seem that all you need to do is check:

  if (item.isChecked()) item.setChecked(false); else item.setChecked(true); 

or at least what the textbook says. Maybe you should give it another read? Hope this helps.

0
source

You should add break commands ; after each case :

 public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.item1: item.setChecked(!item.isChecked()); break; case R.id.item2: item.setChecked(!item.isChecked()); break; default: break; } return super.onOptionsItemSelected(item); } 
-1
source

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


All Articles