How to show/Hide Navigation Drawer programmatically
AndroidNavigation DrawerAndroid Problem Overview
How can I use button to show/hide Navigation Drawer, I have used this SO link to create and manage Navigation Drawer.
Now i am using (Swipe to right from left - to show) and (Swipe from right to left - to hide)
How may I show/Hide Drawer using button highlighted in below screenshot:
header_home.xml:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:id="@+id/header_layout"
android:gravity="fill_horizontal"
android:layout_gravity="top|center">
<TextView
android:id="@+id/textHeader"
android:text="Home"
android:textColor="#ffffff"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/bg_header"
/>
<ImageButton
android:id="@+id/btnDrawer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:adjustViewBounds="true"
android:background="@drawable/icon_drawer"
android:contentDescription="@string/app_name"
/>
Edited:
btnMenu.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
drawer.openDrawer(Gravity.LEFT);
}
});
I know to close i have to call drawer.closeDrawer(Gravity.LEFT);
but where i have to place this code ?
Android Solutions
Solution 1 - Android
Grab a reference to the DrawerLayout
and call closeDrawer(int) to close it and openDrawer(int) to open it. The int
parameter refers to the gravity. In your case it should be GravityCompat.LEFT
/ GravityCompat.START
, because accordingly to the screenshot you posted, your DrawerLayout
open and close on the left.
Solution 2 - Android
to Close Drawer:
drawer.CloseDrawer((int)GravityFlags.Left);
to Open Drawer:
drawer.OpenDrawer((int)GravityFlags.Left);
Solution 3 - Android
To open the Drawer
DrawerLayout drawer = findViewById(R.id.drawer_layout);
drawer.openDrawer(GravityCompat.START);
To close the drawer
DrawerLayout drawer = findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
Solution 4 - Android
I might be late for this but here is the solution you are looking for:
btnMenu.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(drawer.isDrawerOpen(GravityCompat.START)){
drawerLayout.closeDrawer(GravityCompat.START);
}
else {
drawerLayout.openDrawer(GravityCompat.START);
}
}
});
Solution 5 - Android
If you are using Sliding Drawer Menu, and you want to hide the menu when it is open (when drag from right to left). Then we have to deal with listview object ontouch listener. The code will be like this.
//((( When we drage from Right to left then menu hide ))))
lvMenu.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction())
{
case MotionEvent.ACTION_DOWN:
toggleMenu(v);
break;
case MotionEvent.ACTION_UP:
//showtoast("up");
break;
default:
return false;
}
return false;
}
});
public void toggleMenu(View v) {
mLayout.toggleMenu();
}
For complete code, you can put the comment here, if you have any problem