Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hide a Navigation Drawer Menu Item - Android

I have a navigation drawer. When an event is called, I want to hide one of my navigation menu item for user. How can I do that?

protected void onCreate(Bundle savedInstanceState) {     super.onCreate(savedInstanceState);     setContentView(R.layout.sliding_menu);      if (android.os.Build.VERSION.SDK_INT > 9) {         StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();         StrictMode.setThreadPolicy(policy);     }     mTitle = mDrawerTitle = getTitle();      // load slide menu items     navMenuTitles = getResources().getStringArray(R.array.nav_drawer_items);      // nav drawer icons from resources     navMenuIcons = getResources()             .obtainTypedArray(R.array.nav_drawer_icons);      mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout1);     mDrawerList = (ListView) findViewById(R.id.list_slidermenu1);      navDrawerItems = new ArrayList<NavDrawerItem>();      // adding nav drawer items to array     // Home     navDrawerItems.add(new NavDrawerItem(navMenuTitles[0], navMenuIcons.getResourceId(0, -1)));     // Profil     navDrawerItems.add(new NavDrawerItem(navMenuTitles[1], navMenuIcons.getResourceId(1, -1)));     // Prospek     navDrawerItems.add(new NavDrawerItem(navMenuTitles[2], navMenuIcons.getResourceId(2, -1)));     // Announcement     navDrawerItems.add(new NavDrawerItem(navMenuTitles[3], navMenuIcons.getResourceId(3, -1), true,             cariTotal("http://192.168.0.23/api/android/announcement/").toString()));     // Logout     navDrawerItems.add(new NavDrawerItem(navMenuTitles[4], navMenuIcons.getResourceId(4, -1)));      // Recycle the typed array     navMenuIcons.recycle();     //set event     mDrawerList.setOnItemClickListener(new SlideMenuClickListener());     // setting the nav drawer list adapter     adapter = new NavDrawerListAdapter(getApplicationContext(),             navDrawerItems);     mDrawerList.setAdapter(adapter);     // enabling action bar app icon and behaving it as toggle button     getSupportActionBar().setDisplayHomeAsUpEnabled(true);     getSupportActionBar().setHomeButtonEnabled(true);      mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,             R.string.app_name,             R.string.app_name     ){         public void onDrawerClosed(View view) {             getSupportActionBar().setTitle(mTitle);             // calling onPrepareOptionsMenu() to show action bar icons             invalidateOptionsMenu();         }          public void onDrawerOpened(View drawerView) {             getSupportActionBar().setTitle(mDrawerTitle);             //mDrawerList.bringToFront();             //mDrawerLayout.requestLayout();             // calling onPrepareOptionsMenu() to hide action bar icons             invalidateOptionsMenu();         }     };     mDrawerLayout.setDrawerListener(mDrawerToggle);      if (savedInstanceState == null) {         // on first time display view for first nav item         displayView(0);     } } private void displayView(int position) {     // update the main content by replacing fragments     android.app.Fragment fragment = null;     switch (position) {         case 0:             fragment = new HomeFragment();             break;         case 1:             fragment = new ProfilFragment();             break;         case 2:             fragment = new ProspekFragment();             break;         case 3:             fragment = new AnnouncementFragment();             break;         case 4:             Logout();             break;         default:             break;     }      if (fragment != null) {         android.app.FragmentManager fragmentManager = getFragmentManager();         fragmentManager.beginTransaction()                 .replace(R.id.container, fragment).commit();          // update selected item and title, then close the drawer         mDrawerList.setItemChecked(position, true);         mDrawerList.setSelection(position);         setTitle(navMenuTitles[position]);         mDrawerLayout.closeDrawer(mDrawerList);     } else {         // error in creating fragment         Log.e("SlidingMenu", "Error in creating fragment");     } } 

I want to hide my navigation drawer menu item at index 2. How to do that?

UPDATE I have 5 menu items in my navigation drawer, I want to hide one of them when some event is called.

like image 446
huzain07 Avatar asked Aug 12 '15 02:08

huzain07


People also ask

How can I hide menu items in Android?

Hide button by default in menu xml By default the share button will be hidden, as set by android:visible="false" .

How do I hide navigation view in Android?

On Android 4.1 and higher, you can set your application's content to appear behind the navigation bar, so that the content doesn't resize as the navigation bar hides and shows. To do this, use SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION .

How do I hide navigation options from navigation drawer sidebar?

To hide the navigation option from the navigation drawer we will use drawerContent prop of Drawer. Navigator. This prop provides independence to replace default navigation drawer with our custom one. We have added some custom code to customise the navigation drawer.


Video Answer


2 Answers

NavigationView navigationView; @Override protected void onCreate(Bundle savedInstanceState) {     super.onCreate(savedInstanceState);     setContentView(R.layout.activity_main);     hideItem();   }   private void hideItem()    {      navigationView = (NavigationView) findViewById(R.id.nav_view);      Menu nav_Menu = navigationView.getMenu();      nav_Menu.findItem(R.id.nav_settings).setVisible(false);    } 
like image 129
Aj121 Avatar answered Sep 26 '22 02:09

Aj121


This will work as setInvisibility(GONE):

 Menu nav_Menu = navigationView.getMenu();  nav_Menu.findItem(R.id.nav_profile).setVisible(false);  nav_Menu.findItem(R.id.menu_login).setVisible(true);  nav_Menu.findItem(R.id.menu_logout).setVisible(false);  nav_Menu.findItem(R.id.menu_regsitration).setVisible(true); 
like image 31
Pravin Dharam Avatar answered Sep 24 '22 02:09

Pravin Dharam