Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show/Hide Navigation Drawer programmatically

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:

enter image description here

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 ?

like image 639
Sun Avatar asked Sep 15 '15 10:09

Sun


People also ask

How do I open navigation drawer programmatically?

btnMenu. setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub drawer. openDrawer(Gravity. LEFT); } });

How do I hide navigation view in Android?

On Android 4.1 and higher, you can set your application's content to appear behind the navigation bar, so that the content doesn't resize as the navigation bar hides and shows. To do this, use SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION .

How do I show navigation drawer in all activities?

The user can view the navigation drawer when they swipe the activity's screen from the left edge of the android device. A user can also find it from the activity, by tapping the app icon (also known as the “hamburger” menu) in the action bar.


2 Answers

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.

like image 95
Blackbelt Avatar answered Sep 18 '22 20:09

Blackbelt


to Close Drawer:

drawer.CloseDrawer((int)GravityFlags.Left); 

to Open Drawer:

drawer.OpenDrawer((int)GravityFlags.Left); 
like image 34
yaniv maymon Avatar answered Sep 19 '22 20:09

yaniv maymon