I'm working with Navigation Drawer and I want to click the button on activity and Navigation Drawer should show, but I don't know how to do it. Normally, when we swipe from left edge to the right the navigation will be shown. as such, I want to be able to open the navigation drawer with the click of a button.
Asked
Active
Viewed 1.2k times
-1
-
Could you post your code into your question? – Dima Kozhevin Oct 14 '17 at 15:22
-
You need to post your code if you need a clearer answer. – Darush Oct 14 '17 at 15:41
-
Navigation Bar and Navigation Drawer are different things: https://developer.android.com/training/system-ui/navigation.html – Darush Oct 14 '17 at 15:43
-
Follow this tutorial: https://medium.com/@janishar.ali/navigation-drawer-android-example-8dfe38c66f59 – Darush Oct 14 '17 at 15:46
-
Possible duplicate of [How to open navigation drawer on button click in main fragment?](https://stackoverflow.com/questions/19442841/how-to-open-navigation-drawer-on-button-click-in-main-fragment) – Zeeshan Ayaz Jun 26 '19 at 14:41
3 Answers
1
Use the following methods to open and close your navigation drawer:
DrawerLayout drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
//To Open:
drawerLayout.openDrawer(Gravity.START);
//To Close:
drawerLayout.closeDrawer(Gravity.END);
Source1 for more information:

Darush
- 11,403
- 9
- 62
- 60
-
i had a variable private NavigationView navigationBar;btnButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { navigationBar.show(); ?????????? Toast.makeText(PlaylistActivity.this, "CLicked", Toast.LENGTH_SHORT).show(); } }); – Danh Oct 14 '17 at 15:34
-
@Danh NavigationView is typically placed inside a DrawerLayout so you need to address your navigation drawer. – Darush Oct 14 '17 at 15:38
-
1
Button hamMenu = findViewById(R.id.ham_menu);
hamMenu.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DrawerLayout navDrawer = findViewById(R.id.drawer_layout);
// If the navigation drawer is not open then open it, if its already open then close it.
if(!navDrawer.isDrawerOpen(GravityCompat.START)) navDrawer.openDrawer(Gravity.START);
else navDrawer.closeDrawer(Gravity.END);
}
});

Zeeshan Ayaz
- 858
- 6
- 11
1
You can also use a method and Use the exact same steps given by @Zeeshan.
public void hamMenu(View view){
DrawerLayout drawer = findViewById(R.id.drawer_layout);
if (!drawer.isDrawerOpen(GravityCompat.START)) {
drawer.openDrawer(GravityCompat.START);
} else {
drawer.closeDrawer(GravityCompat.END);
}
}
Use this method in the button which you need android:Onclick="hamMenu"
in .xml
.

cantona_7
- 1,127
- 4
- 21
- 40