0

I am trying to hide the options menu when the Nav Drawer is opened. How do I do this? The hide() doesn't work and I tried a few variations already..

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

    <item android:id="@+id/menu_write"
          android:title="Write"
          android:showAsAction="always"
            />

</menu>


public final class OptionsMenu{
    private Menu menu;

    public void onCreateOptionsMenu(MenuInflater inflater, Menu menu) {
        inflater.inflate(R.menu.sample, menu);
        this.menu = menu;
    }

    public boolean onMenuItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            default:
                return false;
        }
    }

    public void hide(){
        menu.findItem(R.id.menu_points).setVisible(false); //Doesn't work
    }
}
  • Do you want to collapse the popup of the options menu if it is expanded, or just to disable the menu from being openable overall? – Boris Strandjev Mar 15 '14 at 10:58
  • use `closeOptionsMenu()` to programmatically close menu options – Gopal Gopi Mar 15 '14 at 11:00
  • i want to hide the entire options menu when the nav drawer is opened, and then show it when the nav drawer is closed –  Mar 15 '14 at 11:02
  • where you call hide() method? what is menu object? move `menu.findItem(R.id.menu_points).setVisible(false);` to `onCreateOptionsMenu` and test again – Shayan Pourvatan Mar 15 '14 at 11:11

3 Answers3

1

Try this...

private boolean isDrawerOpened; // Global variable

implement DrawerListener...

drawerLayout.setDrawerListener(new DrawerLayout.SimpleDrawerListener() {

    @Override
    public void onDrawerOpened(View view) {
        isDrawerOpened = true;
        invalidateOptionsMenu();
    }

    @Override
    public void onDrawerClosed(View view) {
        isDrawerOpened = false;
        invalidateOptionsMenu();
    }
});

and validate state of DrawerLayout in onCreateOptionsMenu...

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);
    if (!isDrawerOpened) {
        getMenuInflater().inflate(R.menu.sample, menu);
    }
    return true;
}
Gopal Gopi
  • 11,101
  • 1
  • 30
  • 43
0

use closeOptionsMenu() for close option menu

Kanaiya Katarmal
  • 5,974
  • 4
  • 30
  • 56
0

Answer is here Android: How to enable/disable option menu item on button click?

onPrepareOptionsMenu(Menu menu)
Community
  • 1
  • 1