Ideally navigation drawer should get closed once some item has been pressed from it, but its not happening automatically. How to do it ? Thanks!
How to close navigation drawer when an item is pressed from it?
Asked Answered
Which libary do you use to get the navigation drawer? –
Iroquois
Got it!
private DrawerLayout mDrawerLayout;
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerLayout.closeDrawers();
Working perfectly fine.
Can it be closed without animation? –
Linkous
Hamzeh - mDrawerLayout.closeDrawer(Gravity.LEFT, false); would result in closing it without animation –
Boschbok
mDrawerLayout.closeDrawer(GravityCompat.START, false); –
Pigeonwing
For me this one worked -
mDrawerLayout.closeDrawer(Gravity.START, false);
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);
}
}
closeDrawer();//when you want to call
public void closeDrawer() {
if (mDrawerLayout.isDrawerOpen(GravityCompat.START)) {
mDrawerLayout.closeDrawer(GravityCompat.START);
}
}
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();
}
}
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;
}
This work,kotlin code
drawerLayout.closeDrawer(GravityCompat.START)
Hi, thanks for your answer, but it's a duplicate of multiple other answer such as this one –
Antihelix
© 2022 - 2024 — McMap. All rights reserved.