Simply make a custom XML menu that includes a grouping, but make the grouping icon the triple dot. Then with no children elements it will do nothing. You can add items dynamically to the grouping in code if you decide to make it clickable later as well. Although I'm not sure why you would leave it visible if you don't want them to click it. Why not just remove it when it needs to be unavailable?
EDIT FOR CLARITY:
So you can use a menu with nothing in the XMl Group if you would like.
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<group android:id="@+id/myGroup">
</group>
</menu>
Then inflate this and it won't be clickable, but this does not seem like a good solution so i hope you choose a different direction.
You can do two other ways as well.
get a reference to the menu item you want to remove like:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_filter"
android:icon="@drawable/ic_menu_filter"
app:showAsAction="always"
android:actionViewClass="android.widget.ImageButton"
android:title="@string/filter">
//other items here
</menu>
MenuItem mMenuFilterItem = null;
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu_news_fragment, menu);
super.onCreateOptionsMenu(menu, inflater);
mMenuFilterItem = menu.findItem(R.id.action_filter)
//then you can hide and show as necessary
if(shouldHide){
mMenuFilterItem.setVisible(false);
}
}
//then in other areas of code you can hide and show using the same as above.
Alternatively, "which would probably be best", you could simply invalidate the menu and skip inflating.
setHasOptionsMenu(false); //if fragment and
getActivity().invalidateOptionsMenu();
//this will cause it to skip the onCreateOptionsMenu in the fragment and redraw it from scratch and will leave it empty. Then just reverse to put it back.
Now if you are in activity, then you would just do
MenuItem mMenuFilterItem = null;
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
if(!shouldHide){
inflater.inflate(R.menu.menu_news_fragment, menu);
}
super.onCreateOptionsMenu(menu, inflater);
}
//then when you need to hide it just set
shouldHide = true;
invalidateOptionsMenu()
//then to put it back
shouldHide = false;
invalidateOptionsMenu();