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>
This is how i did it.
Add following code in your activity where option menu is implemented.
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 thesetOnActionExpandListener
.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:
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.group is a collection of menu items that shares certain traits, for more information see