0

I need to temporarily disable the menu on the app bar (the one with the 3 dots, screenshot attached) and I haven't found a way to do so through the Android APIs. This is for an app tutorial experience. Once the tutorial is over, I need to re-enable it.

Menu illustration

Ideally, I'd like it to stay visible but not clickable. That way when a user presses it the menu won't open. I could also live with temporarily making it not visible (but also haven't found a way to do this).

Does anyone have any insight into how to do this?

Edit: To clarify, I need to disable the menu button itself, not the menu items inside of it.

Thanks!

Luis Naranjo
  • 649
  • 7
  • 18
  • Maybe you will find some solution at https://stackoverflow.com/questions/5440601/android-how-to-enable-disable-option-menu-item-on-button-click – Dungnbhut Oct 04 '17 at 00:58
  • 3
    Possible duplicate of [Enable/Disable ActionBar Menu Item](https://stackoverflow.com/questions/14169040/enable-disable-actionbar-menu-item) – Nouman Ch Oct 04 '17 at 01:04
  • Thank you for taking a look, but both of these links show how to disable the items inside of the menu. I need a way to disable the entire menu so that when you press the three dots, it won't open. I've edited my post to clarify. – Luis Naranjo Oct 06 '17 at 00:44

2 Answers2

0

Just remove or comment out your method onCreateOptionsMenu

Nabin Bhandari
  • 15,949
  • 6
  • 45
  • 59
0

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();
Sam
  • 5,342
  • 1
  • 23
  • 39
  • Could you clarify by what you mean by a grouping? And how do you set the grouping icon to be the triple dot? And to answer your question: If there is a way to dynamically remove it and put it back at runtime, that is also an acceptable approach for me. Do know if this is possible? – Luis Naranjo Oct 06 '17 at 00:45