Wednesday, April 24, 2019

Hiding menu items of Navigation Drawer of Android to manage dynamic menu display

Navigation Drawer in android development is an important feature  that we can implement on demand.  In the navigation drawer different types of menu resides. In some case, we may need to load different menu options in the navigation drawer depending on users types. This can be implemented easily by hiding menu items from the already existing drawer.


NavigationView naviView;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    hideItem();
  }


private void hideItem()
   {
     naviView = (NavigationView) findViewById(R.id.nav_view);
     Menu naviMenu = naviView.getMenu();
     naviMenu.findItem(R.id.nav_create).setVisible(false);
   }


The above hideItem() function will invisible the item nav_create from the menu of the drawer.


N.B: Menu itmes exist in res->menu->_drawer.xml  
     _drawer.xml point from the .xml of res->layout