0

How can i make the navigation drawer open from the right to the left?

Main.xml

<android.support.design.widget.NavigationView
    android:id="@+id/navigation_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="end"
    app:headerLayout="@layout/drawer_header"
    app:menu="@menu/navigation"
    android:background="#FFE7FF"/>

drawer_header.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="150dp"
android:baselineAligned="false"
android:orientation="vertical">

navigation.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<group android:checkableBehavior="single">
    <item
        android:id="@+id/Home"
        android:checked="true"
        android:icon="@drawable/homelogo"
        android:title="Home" /></group></menu>

This is how i create it in main activity

navigationView = (NavigationView) findViewById(R.id.navigation_view);
    navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(MenuItem menuItem) {
            switch (menuItem.getItemId()) {
              (..........)

How can i make it ? I've tried android:gravity but it doesn't work.

E Heng
  • 91
  • 3
  • 9
  • btnHome.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub hideKeyboard(); handleDrawerLayout(); } }); – Kishore Jethava Aug 22 '15 at 10:40
  • this is duplicate question please refer : http://stackoverflow.com/questions/17852091/how-can-i-open-navigation-drawer-from-right-side-to-left – Shadow Droid Aug 22 '15 at 10:41
  • i've tried , but i'm opening the drawer using navigation view , drawerLayout.openDrawer(navigationView); , so i can't use gravity right inside this line – E Heng Aug 22 '15 at 10:44

3 Answers3

3

In your main.xml there is the android.support.design.widget.NavigationView which has the following attribute: android:layout_gravity="end". You have to set it to right, so it will be like this: android:layout_gravity="right". The rest is done by the design support lib.

Note that the Android Studio will show a warning that you should use end instead of right in order to ensure correct behavior in right-to-left languages. You can ignore this message.

Gergely Kőrössy
  • 5,620
  • 3
  • 28
  • 44
  • I tried but it doesn't change anything , btw i've decided to change it to the left. Thank you so much. – E Heng Aug 22 '15 at 13:23
  • 3
    Simply create a totally blank application, with one navigation drawer activity in it (just defaults of Android Studio) and follow your simple step to change gravity to right. You'll end of with `No drawer view found with gravity LEFT` exception. – Saeed Neamati May 16 '17 at 05:28
1

Try setting android:layout_gravity="end" instead of android:layout_gravity="start"

<android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="end"
    android:fitsSystemWindows="true"
    app:headerLayout="@layout/nav_header_main"
    app:menu="@menu/activity_main_drawer" />
0

it does change , only two things to do ,

After addig a new activity to your android app from android studio->file->new->activity ->Navigation Draw....

you have to change android:layout_gravity="right" in main layout's android.support.design.widget.NavigationView and drawer.closeDrawer(GravityCompat.END); in your OnNavigationItemSelected() function in main activity .