Get selected menu item's index value in the navigation drawer

One way to do is store menu item ID in variable e.g checkedItemID

navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
  @Override
  public boolean onNavigationItemSelected(MenuItem menuItem) {
      menuItemID=menuItem.getItemId();
   }
)}

Then on implement

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

        public void onDrawerClosed(View view) {
            super.onDrawerClosed(view);
            // Do whatever you want here

         navigationView.getMenu().findItem(menuItemID).setChecked(false);
        }

        public void onDrawerOpened(View drawerView) {
            super.onDrawerOpened(drawerView);
            // Do whatever you want here
        }
    };
// Set the drawer toggle as the DrawerListener
mDrawerLayout.setDrawerListener(mDrawerToggle);

Make a variable position of int datatype. Firstly set its value to 0 and onNavigationItemSelected change its value to menuItem index(like 0 or 1 or 2 and so on).Now this position will provide you the index of selected menuItem.

int position = 0;
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
    @Override
    public boolean onNavigationItemSelected(MenuItem menuItem) {
        switch (menuItem.getItemId()) {
           case R.id.first:
                position = 0;
                break;
           case R.id.second:
                position = 1;
                break;
           case R.id.third:
                position = 2;
                break;
        }
        return true;
    }
});

Try this to get navigation item at position 0 getItem(index) will give you desired item

navigationView.getMenu().getItem(selectedposition).setChecked(false);

also use this link for reference for getting seleted item http://thegeekyland.blogspot.in/2015/11/navigation-drawer-how-set-selected-item.html

use below code for getting selected position

int selectedposition= 0;

    navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(MenuItem menuItem) {
            switch (menuItem.getItemId()) {
               case R.id.firstitemid:
                    selectedposition= 0;
                    break;
               case R.id.seconditemid:
                    selectedposition= 1;
                    break;
               case R.id.thirditemid:
                    selectedposition= 2;
                    break;
            }
            return true;
        }
    });

Tags:

Android