Reputation:
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
}
}
Upvotes: 0
Views: 2886
Reputation: 11131
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;
}
Upvotes: 1
Reputation:
Answer is here Android: How to enable/disable option menu item on button click?
onPrepareOptionsMenu(Menu menu)
Upvotes: 0