How to hide the fragment bar button when the navigation drawer is open

advertisements

I use Navigation Drawer and for one of the fragments I have different menu. For that fragment when navigation drawer is open, how to hide the action bar's button? For example on the picture ( http://prntscr.com/7ur712 ) it is button REFRESH.

@Override public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
**setHasOptionsMenu(true);** }

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
**inflater.inflate(R.menu.my_menu, menu);**
}


The best way to update your actionBar buttons is to call the invalidateOptionsMenu() when your NavigationDrawer is opened/closed.

According to the Android training documentation page, when you create your ActionBarDrawerToggle, you should override the onDrawerClosed and the onDrawerOpened methods:

mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
                R.drawable.ic_drawer, R.string.drawer_open, R.string.drawer_close) { 

            /** Called when a drawer has settled in a completely closed state. */
            public void onDrawerClosed(View view) {
                super.onDrawerClosed(view);
                getActionBar().setTitle(mTitle);
                invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
            } 

            /** Called when a drawer has settled in a completely open state. */
            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);
                getActionBar().setTitle(mDrawerTitle);
                invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
            }
        };

Also, in your onPrepareOptionsMenu method, you can detect if your drawer is open or not, and load the menu corresponding to your drawer state:

/* Called whenever we call invalidateOptionsMenu() */
    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        // If the nav drawer is open, hide action items related to the content view
        boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
        menu.findItem(R.id.action_websearch).setVisible(!drawerOpen);
        return super.onPrepareOptionsMenu(menu);
    }

For more information, you can see the Android developper documentation here : https://developer.android.com/training/implementing-navigation/nav-drawer.html (cf section: Listen for Open and Close Events)