How can i get Current selected item of navigation drawer? My menu is stored in drawer_menu.xml
navigationView = (NavigationView) findViewById(R.id.navigation_view);
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.change_sec:
Intent intent_sec = new Intent(MainActivity.this, ClassDataProvider.class);
startActivityForResult(intent_sec, 9);
drawerLayout.closeDrawers();
break;
case R.id.holiday_list:
Intent intent = new Intent(MainActivity.this, HolidayList.class);
startActivity(intent);
drawerLayout.closeDrawers();
break;
}
return true;
}
});
To use a DrawerLayout, position your primary content view as the first child with width and height of match_parent and no layout_gravity> . Add drawers as child views after the main content view and set the layout_gravity appropriately. Drawers commonly use match_parent for height with a fixed width.
onNavigationItemSelected(MenuItem item) Called when an item in the navigation menu is selected.
Which method is used to handle click on the menu items of the navigation view? You have to use OnNavigationItemSelectedListener(MenuItem item) method.
For example, you can use:
private int getCheckedItem(NavigationView navigationView) {
Menu menu = navigationView.getMenu();
for (int i = 0; i < menu.size(); i++) {
MenuItem item = menu.getItem(i);
if (item.isChecked()) {
return i;
}
}
return -1;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With