Skip to content Skip to sidebar Skip to footer

Android Checkable Menu Item

I have the following menu layout in my Android app:

Solution 1:

Layout looks right. But you must check and uncheck menu item in code.

From the documentation:

When a checkable item is selected, the system calls your respective item-selected callback method (such as onOptionsItemSelected()). It is here that you must set the state of the checkbox, because a checkbox or radio button does not change its state automatically. You can query the current state of the item (as it was before the user selected it) with isChecked() and then set the checked state with setChecked().

Solution 2:

Wrap the items in a group element, like this:

<menuxmlns:android="http://schemas.android.com/apk/res/android"><groupandroid:checkableBehavior="all"><itemandroid:id="@+id/item1"android:titleCondensed="Options"android:title="Highlight Options"android:icon="@android:drawable/ic_menu_preferences"></item><itemandroid:id="@+id/item2"android:titleCondensed="Persist"android:title="Persist"android:icon="@android:drawable/ic_menu_preferences"android:checkable="true"></item></group></menu>

From the Android docs:

The android:checkableBehavior attribute accepts either:

single - Only one item from the group can be checked (radio buttons)

all - All items can be checked (checkboxes)

none - No items are checkable

Solution 3:

You can create a checkable menu item by setting the actionViewClass to a checkable widget like android.widget.CheckBox

res/menu/menu_with_checkable_menu_item.xml

<menuxmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"><itemandroid:id="@+id/action_favorite"android:checkable="true"android:title="@string/action_favorite"app:actionViewClass="android.widget.CheckBox"app:showAsAction="ifRoom|withText" /></menu>

And you can can even style it to be a checkable star if you set actionLayout to a layout with a styled android.widget.CheckBox

res/layout /action_layout_styled_checkbox.xml

<CheckBoxxmlns:android="http://schemas.android.com/apk/res/android"style="?android:attr/starStyle"android:layout_width="wrap_content"android:layout_height="wrap_content" />

res/menu/menu_with_checkable_star_menu_item.xml

<menuxmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"><itemandroid:id="@+id/action_favorites"android:checkable="true"android:title="@string/action_favorites"app:actionLayout="@layout/action_layout_styled_checkbox"app:showAsAction="ifRoom|withText" /></menu>

To set the value

menuItem.setChecked(true/false);

To get the value

menuItem.isChecked()

Cast MenuItem to CheckBox

CheckBox checkBox= (CheckBox) menuItem.getActionView();

Solution 4:

I've found that the best solution was to just use the onOptionsItemSelected() method as of my current API (27-28).

@OverridepublicbooleanonOptionsItemSelected(MenuItem item) 
   {    

//Copy from here...intitemId= item.getItemId();

       if(item.isChecked())                          
       { 
           if(R.id.edit_tile_checkbox == itemId)     //Individual checkbox logic
           {   /*TODO unchecked Action*/} 
           item.setChecked(false);                   //Toggles checkbox state.
       }
       else
       {
            if(R.id.edit_tile_checkbox == itemId)    //Individual checkbox logic
            {/*TODO checked Action*/}
            item.setChecked(true);                   //Toggles checkbox state.
       }
//...To here in to your onOptionsItemSelected() method, then make sure your variables are all sweet.returnsuper.onOptionsItemSelected(item);
   }

I spent way to long on here for this answer. and for whatever reason, the answers above didn't help (I'm a returning newbie I probably mucked something up I'm sure). There could be a better way of doing this so helpful criticism is welcomed.

Solution 5:

Answering because the answers here seem long and convoluted.. I have some exact Kotlin code here

Override your activity at the top and override the function onMenuItemClick, Have a function to handle the button click to open the menu.

Have an array or list which holds the checked value and sets the check when the menu is re-created

Note: This code does not keep the menu open, It only ensures that checked items remain checked. I noted there are lots of solutions to that on stack overflow, so have a look at them if that's what you desire

classexampleActivity : AppCompatActivity(), PopupMenu.OnMenuItemClickListener {
   privatevar checkChecked = arrayListOf(false,false)
   //some codefunclickBTN(v: View){
        val popup = PopupMenu(this,v)
        popup.setOnMenuItemClickListener(this)
        popup.inflate(R.menu.yourmenufilename)
        //assuming you have 2 or more menu items
        popup.menu[0].isChecked = checkChecked[0]
        popup.menu[1].isChecked = checkChecked[1]
        popup.show()
  }

  overridefunonMenuItemClick(item: MenuItem?): Boolean {
     when(item?.itemID){
        R.id.item0 -> {
                item.isChecked = !item.isChecked
                checkChecked[0] = item.isChecked
                returntrue
        }
        R.id.item1 -> {
                item.isChecked = !item.isChecked
                checkChecked[1] = item.isChecked
                returntrue
        }
  }
}

of course in XML you should have your Button and Menu setup. An example menu is here

<?xml version="1.0" encoding="utf-8"?><menuxmlns:android="http://schemas.android.com/apk/res/android"><itemandroid:id="@+id/item0"android:title="@string/hi"android:checkable="true"/><itemandroid:id="@+id/item1"android:title="@string/yo"android:checkable="true"/></menu>

Post a Comment for "Android Checkable Menu Item"