Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Current Selected Item of Navigation Drawer in Android

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;
        }
    });
like image 721
akkk Avatar asked Jul 12 '16 18:07

akkk


People also ask

How to use DrawerLayout in Android?

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.

What is onNavigationItemSelected?

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?

Which method is used to handle click on the menu items of the navigation view? You have to use OnNavigationItemSelectedListener(MenuItem item) method.


1 Answers

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;
}
like image 153
Michael Spitsin Avatar answered Nov 13 '22 11:11

Michael Spitsin