0

I have added some checkboxs in my overflow menu. I want my overflow menu to hold rather than disappear once I click the checkbox in the overflow menu. How can I do that? Thanks for help.

This is my menu xml file.

<item
    android:id="@+id/action_check"
    android:title="@string/action_check"
    android:orderInCategory="1"
    app:showAsAction="never"
    android:visible="true"
    android:checkable="true"/>
<item android:id="@+id/notification"
    android:orderInCategory="2"
    android:title="@string/notification"
    app:showAsAction="never"
    android:visible="true"
    android:checkable="true"/>
<item android:id="@+id/about"
    android:orderInCategory="3"
    android:title="@string/about"
    app:showAsAction="never"></item>
1

2 Answers 2

4

This is how i did it.

Add following code in your activity where option menu is implemented.

@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        //your checking other stuff
        item.setChecked(!item.isChecked());

        //main part for holding onto the menu
        item.setShowAsAction(MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
        item.setActionView(new View(this));
        item.setOnActionExpandListener(new MenuItem.OnActionExpandListener() {
            @Override
            public boolean onMenuItemActionExpand(MenuItem item) {
                return false;
            }

            @Override
            public boolean onMenuItemActionCollapse(MenuItem item) {
                return false;
            }
        });
        return false;
    }

By adding the line: item.setShowAsAction(MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW); I am marking the item as it has expandable/collapsible behavior so it will call the setOnActionExpandListener.

This line here: item.setActionView(new View(this)); is a view when item is in expanded state.It is just a dummy view because we will never let it expand how i am going to explain next.

You see that i am returning false from both the methods of setOnActionExpandListener to suppress expansion and collapsing of the item so the view we gave in previous step would never show up and menu would remain open.

Following would be your menu file:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <group android:checkableBehavior="all">


        <item
            android:id="@+id/action_check"
            android:orderInCategory="1"
            android:title="Title 1"
            app:showAsAction="never" />

        <item
            android:id="@+id/notification"
            android:orderInCategory="2"
            android:title="Title 2"
            app:showAsAction="never" />

        <item
            android:id="@+id/about"
            android:orderInCategory="3"
            android:title="Title 3"
            app:showAsAction="never" />

    </group>

</menu>

Notice the line group android:checkableBehavior="all" is to tell that all items in the group will have checkable behavior so that you don't have to write checkable in each item.

0

group is a collection of menu items that shares certain traits, for more information see

<group android:id="@+id/group>
    <item
        android:id="@+id/action_check"
        android:title="@string/follow"
        android:orderInCategory="1"
        app:showAsAction="never"
        android:visible="true"
        android:checkable="true"/>
    <item android:id="@+id/notification"
        android:orderInCategory="2"
        android:title="@string/notification"
        app:showAsAction="never"
        android:visible="true"
        android:checkable="true"/>
    <item android:id="@+id/about"
        android:orderInCategory="3"
        android:title="@string/about_us"
        app:showAsAction="never"/>
</group>
3
  • What exactly this code does ? See How do I write a good answer?. Commented Sep 5, 2018 at 4:32
  • group is a collection of menu items that shares certain traits, for more information see
    – Vishal
    Commented Sep 5, 2018 at 4:54
  • Add your explaination to answer not in comments . Commented Sep 5, 2018 at 5:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.