NavigationView navigationView = findViewById(R.id.nav_view); navigationView.setNavigationItemSelectedListener( new NavigationView.OnNavigationItemSelectedListener() { @Override public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) { // Perform action when a menu item is selected drawerLayout.closeDrawers(); return true; } });
new Handler().postDelayed(new Runnable() { public void run() { drawerLayout.closeDrawers(); } }, 2000);In this example, we use a Handler to run a task after a delay of 2 seconds. The task is to close all opened drawers associated with the DrawerLayout. Package library: android.support.v4.widget.