1

What i want here when i move to another Fragment by clicking in NavigationDrawer menu button then button should be disabled.

Because addToBackStack(); method add multiple times in their stack when click again and again. So wanted to disable it when i move to another fragment.

ʍѳђઽ૯ท
  • 16,646
  • 7
  • 53
  • 108
user7387275
  • 137
  • 2
  • 7

4 Answers4

5

To disable toggle button in navigation drawer use

drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);

to enable use LOCK_MODE_LOCKED_OPEN replacing LOCK_MODE_LOCKED_CLOSED

To disable drawer item click

  1. Refer Hide Some Navigation Drawer Menu Item - Android there you can hide it
  2. If you do not wish to hide in onNavigationItemSelected where you check (id == R.id.whatevertheitemid) also use a Boolean to allow access as you wish

eg

if (id == R.id.whatevertheitemid && isAccessGiven) { // do your task
 }
Community
  • 1
  • 1
Charuක
  • 12,953
  • 5
  • 50
  • 88
2

Its helps me to solve this issue:

     @Override
        public boolean onNavigationItemSelected(MenuItem item) {
              int id = item.getItemId();

     if(id==R.id.nav_item1){   //use can write your menu item here
         return false;
            }

            DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
            drawer.closeDrawer(GravityCompat.START);
            return true;
        }
1

Use this to disable menu item:

NavigationView navigationView;

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

Now in menu item click:

Menu menuView= navigationView.getMenu();
menuView.getItem(ID).setEnabled(false);
Divyesh Patel
  • 2,576
  • 2
  • 15
  • 30
  • Thanks for answer. Now i think its not good way to disabling menuItem. Is there any other to do so that if fragment is already open then it will not addToBackStack(). – user7387275 Jan 07 '17 at 15:58
  • Try to check fragment instance. If it is null then open fragment otherwise do not allow click action – Divyesh Patel Jan 07 '17 at 16:40
1

in kotlin:

navigation.menu.findItem(R.id.your_target_item_id).setEnabled(false)

this is work for me and disabled item in drawerToggle menu

in Java:

NavigationView navigation = (NavigationView) findViewById(R.id.navigation);
navigation.getMenu().getMenu().findItem(R.id.your_target_item_id).setEnabled(false);
Ramin eghbalian
  • 2,348
  • 1
  • 16
  • 36