How to hide the navigation box menu item programmatically?

I want to hide the menu item in the navigation box menu and show it, depending on the type of user who uses the application in accordance with the code specified in the menu item, returns null:

DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open,R.string.navigation_drawer_close);
    drawer.setDrawerListener(toggle);
    toggle.syncState();

    MenuItem target = (MenuItem)drawer.findViewById(R.id.nav_target);

    target.setVisible(false);
+4
source share
1 answer

Fixed by creating a menu and using

menu.findItem(R.id.nav_target)

as suggested by @ droid8421.

Fixed Code:

NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);

Menu menu =navigationView.getMenu();

MenuItem target = menu.findItem(R.id.nav_target);

target.setVisible(false);
+20
source

All Articles