Select Menu Item of Navigation Drawer on Android


    NavigationView.OnNavigationItemSelectedListener navigationView_OnNavigationItemSelectedListener = new NavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {

            // uncheck other menus and sub menus
            int menuSize = navigationView.getMenu().size();

            for (int i = 0; i < menuSize; i++) {
                MenuItem menuItem = navigationView.getMenu().getItem(i);

                if (menuItem.hasSubMenu()) {
                    int submenuSize = menuItem.getSubMenu().size();

                    for (int j = 0; j < submenuSize; j++) {
                        MenuItem subItem = menuItem.getSubMenu().getItem(j);
                        subItem.setChecked(false);
                    }
                } else {
                    menuItem.setChecked(false);
                }
            }

            item.setChecked(true);

            if (item.getItemId() == R.id.itemMenuExit) {

                finish();
                System.exit(0);
                return true;
            }

            return false;
        }
    };

References
https://github.com/mhdr/AndroidSamples/tree/master/091