Get selected index value of menu item in navigation box

I implemented a navigation box, it works fine. The only problem I have to make the selected item is to deselect when closing the navigator. I wonder how I could get the index value for the selected menu item.

navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() { @Override public boolean onNavigationItemSelected(MenuItem menuItem) { menuItem.setChecked(false); } )} 
+6
source share
6 answers

One way to do this is to store the identifier of the menu item in a variable, for example checkedItemID

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

Then for implementation

 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); 
+8
source

Make the variable position an int data type . First set the value to 0 and onNavigationItemSelected change its value to menuItem index (e.g. 0 or 1 or 2, etc.). Now this position will provide you with the index of the 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; } }); 
+4
source

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

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

also use this link for reference to get the missing element http://thegeekyland.blogspot.in/2015/11/navigation-drawer-how-set-selected-item.html

use the code below to get the 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; } }); 
+2
source

Thatโ€™s all you need, it deselects and selects a new one.

 navigationView.setCheckedItem(item.getItemId()); 

And it should go to the bottom of onNavigationItemSelected.

To make this clearer here, an example

 @Override public boolean onNavigationItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.nav_home: controller.setFragment(new HomeFragment()); break; case R.id.nav_settings: controller.setFragment(new SettingsFragment()); break; case R.id.nav_logout: Intent intent = new Intent(getApplicationContext(), LoginActivity.class); intent.putExtra(autoLogin, false); startActivity(intent); break; } navigationView.setCheckedItem(item.getItemId()); DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); drawer.closeDrawer(GravityCompat.START); return false; } 
+1
source

First assign your navigationMenu to the class field

 private NavigationView navigationView; private int currentPosition; ... navigationView = (NavigationView) findViewById(R.id.nav_view); 

Now in the menu callback you can navigate through the menu items:

 public boolean onNavigationItemSelected(MenuItem item) { for (int i=0;i<navigationView.getMenu().size();i++){ if(item==navigationView.getMenu().getItem(i)){ currentPosition=i; break; } } .... } 

You can change the order of menu items without breaking anything

0
source

Another easy way that worked for me

 public boolean onNavigationItemSelected(MenuItem item) { int index = (item.getItemId() % item.getGroupId())-1; .... } 

I think this would be much more efficient than using a For or Switch loop.

If you still have problems, my suggestion would be to check your object id and group id in Log-cat so that you can better understand what you are dealing with.

0
source

All Articles