Question

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
    }
}
Was it helpful?

Solution 3

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

onPrepareOptionsMenu(Menu menu)

OTHER TIPS

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;
}

use closeOptionsMenu() for close option menu

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top