The best way to hide all items in a menu with just one command is to use "group" on your menu xml. Just add all menu items that will be in your overflow menu inside the same group.
In this example we have two menu items that will always show (regular item and search) and three overflow items:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/someItemNotToHide1"
android:title="ITEM"
app:showAsAction="always" />
<item
android:id="@+id/someItemNotToHide2"
android:icon="@android:drawable/ic_menu_search"
app:showAsAction="collapseActionView|ifRoom"
app:actionViewClass="android.support.v7.widget.SearchView"
android:title="Search"/>
<group android:id="@+id/overFlowItemsToHide">
<item android:id="@+id/someID"
android:orderInCategory="1" app:showAsAction="never" />
<item android:id="@+id/someID2"
android:orderInCategory="1" app:showAsAction="never" />
<item android:id="@+id/someID3"
android:orderInCategory="1" app:showAsAction="never" />
</group>
</menu>
Then, on your activity (preferable at onCreateOptionsMenu), use command setGroupVisible to set all menu items visibility to false or true.
public boolean onCreateOptionsMenu(Menu menu) {
menu.setGroupVisible(R.id.overFlowItems, false); // Or true to be visible
}
If you want to use this command anywhere else on your activity, be sure to save menu class to local, and always check if menu is null, because you can execute before createOptionsMenu:
Menu menu;
public boolean onCreateOptionsMenu(Menu menu) {
this.menu = menu;
}
public void hideMenus() {
if (menu != null) menu.setGroupVisible(R.id.overFlowItems, false); // Or true to be visible
}