Ideally navigation drawer should get closed once some item has been pressed from it, but its not happening automatically. How to do it ? Thanks!
Asked
Active
Viewed 8.4k times
89
-
Which libary do you use to get the navigation drawer? – Ion Aalbers Oct 05 '13 at 05:58
7 Answers
251
Got it!
private DrawerLayout mDrawerLayout;
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerLayout.closeDrawers();
Working perfectly fine.
-
4
-
1Hamzeh - mDrawerLayout.closeDrawer(Gravity.LEFT, false); would result in closing it without animation – Psi-Ed Apr 04 '17 at 09:29
-
9
DrawerLayout mDrawerLayout= (DrawerLayout) findViewById(R.id.drawer_layout)
closeDrawer(); // called when you want to close
public void closeDrawer() {
if (mDrawerLayout.isDrawerOpen(GravityCompat.START)) {
mDrawerLayout.closeDrawer(GravityCompat.START);
}
}

Ashif
- 441
- 5
- 12
4
If you have mDrawerLayout as your drawer layout, you can close it when it is open.
@Override
public void onBackPressed() {
if (this.mDrawerLayout.isDrawerOpen(GravityCompat.START)) {
this.mDrawerLayout.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}

M Shafaei N
- 409
- 3
- 6
3
On the right bottom of onNavigationItemSelected where the switch case ends you should right this. mDrawerLayout.closeDrawers();
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()){
}
mDrawerLayout.closeDrawers();
return true;
}

samplist_mouse
- 53
- 6
1
This work,kotlin code
drawerLayout.closeDrawer(GravityCompat.START)

AmrDeveloper
- 3,826
- 1
- 21
- 30

Karam Karam
- 13
- 3
-
Hi, thanks for your answer, but it's a duplicate of multiple other answer such as [this one](https://stackoverflow.com/a/56766056/10952503) – Elikill58 Aug 31 '21 at 09:13