Android - Is Navigation Drawer from right hand side possible?
Asked Answered
R

11

68

http://developer.android.com/training/implementing-navigation/nav-drawer.html

According to this doc, it doesn't say if it is possible to implement drawer from right hand side. Is it even possible? :(

Rhinal answered 17/6, 2013 at 20:33 Comment(1)
check this answer : https://mcmap.net/q/245964/-slide-expandablelistview-at-drawerlayout-form-right-to-left it works for me.Zaragoza
E
48

Here is the documentation on the drawer and it appears that you can configure it to pull out from the left or right.

Drawer positioning and layout is controlled using the android:layout_gravity attribute on child views corresponding to which side of the view you want the drawer to emerge from: left or right. (Or start/end on platform versions that support layout direction.)

http://developer.android.com/reference/android/support/v4/widget/DrawerLayout.html

Eduardo answered 17/6, 2013 at 21:2 Comment(5)
How come their example doesn't work in my case? I mean, I set it to Hebrew, and can't see that it goes to the side on the right.Hospitium
I am going to need more to go on. Are you sure everything is configured properly? I would recommend posting a new question.Eduardo
I decided to let it go. I managed to make their project work by using other things, and then I decided to not use it as it might be confusing to have the navigation drawer on the other side. Maybe I will change my mind later.Hospitium
@LarryMcKenzie - Can the drawable menu be set at the right side?Sheffy
@RoCk I assume you are talking about the hamburger menu icon that looks like 3 horizontal lines. It is not a component of the DrawerLayout. So you can create a button anywhere that could open the navigation drawer. It is not a recommended pattern but you could put the icon in the menu which would place it on the right hand side of a Toolbar.Eduardo
A
69

The NavigationDrawer can be configured to pull out from the left, right or both. The key is the order of appearance of the drawers in the XML declaration, and the layout_gravity attribute. Here is an example:

<android.support.v4.widget.DrawerLayout
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <FrameLayout
        android:id="@+id/content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:baselineAligned="false" >
    </FrameLayout>

    <!-- Left drawer -->

    <ListView
        android:id="@+id/left_drawer"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="left"
        android:choiceMode="singleChoice" />

    <!-- Right drawer -->

    <ListView
        android:id="@+id/right_drawer"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="right"
        android:choiceMode="singleChoice" />
</android.support.v4.widget.DrawerLayout>
Ardeth answered 29/9, 2013 at 21:37 Comment(0)
E
48

Here is the documentation on the drawer and it appears that you can configure it to pull out from the left or right.

Drawer positioning and layout is controlled using the android:layout_gravity attribute on child views corresponding to which side of the view you want the drawer to emerge from: left or right. (Or start/end on platform versions that support layout direction.)

http://developer.android.com/reference/android/support/v4/widget/DrawerLayout.html

Eduardo answered 17/6, 2013 at 21:2 Comment(5)
How come their example doesn't work in my case? I mean, I set it to Hebrew, and can't see that it goes to the side on the right.Hospitium
I am going to need more to go on. Are you sure everything is configured properly? I would recommend posting a new question.Eduardo
I decided to let it go. I managed to make their project work by using other things, and then I decided to not use it as it might be confusing to have the navigation drawer on the other side. Maybe I will change my mind later.Hospitium
@LarryMcKenzie - Can the drawable menu be set at the right side?Sheffy
@RoCk I assume you are talking about the hamburger menu icon that looks like 3 horizontal lines. It is not a component of the DrawerLayout. So you can create a button anywhere that could open the navigation drawer. It is not a recommended pattern but you could put the icon in the menu which would place it on the right hand side of a Toolbar.Eduardo
M
25

My App crashed with "No drawer view found with gravity LEFT" error.

So added this to the onOptionsItemSelected:

if (item != null && item.getItemId() == android.R.id.home) {
        if (mDrawerLayout.isDrawerOpen(Gravity.RIGHT)) {
            mDrawerLayout.closeDrawer(Gravity.RIGHT);
        } else {
            mDrawerLayout.openDrawer(Gravity.RIGHT);
        }
    }
Mona answered 14/2, 2014 at 14:16 Comment(0)
S
5

To add to https://mcmap.net/q/245963/-android-is-navigation-drawer-from-right-hand-side-possible solution.

If you're using Navigation Drawer project created by Android Studio, then things will change in onOptionsItemSelected. Since they created the child class, you have to use this code

if (item != null && id == android.R.id.home) {
        if (mNavigationDrawerFragment.isDrawerOpen(Gravity.RIGHT)) {
            mNavigationDrawerFragment.closeDrawer(Gravity.RIGHT);
        } else {
            mNavigationDrawerFragment.openDrawer(Gravity.RIGHT);
        }
        return true;
}

Next. In class NavigationDrawerFragment, you have to create 3 methods:

Method 1

public boolean isDrawerOpen(int gravity) {
    return mDrawerLayout != null && mDrawerLayout.isDrawerOpen(gravity);
}

Method 2

public void closeDrawer(int gravity) {
    mDrawerLayout.closeDrawer(gravity);
}

Method 3

public void openDrawer(int gravity) {
    mDrawerLayout.openDrawer(gravity);
}

Only now, the right-side drawer will function.

Socialist answered 26/9, 2014 at 15:45 Comment(0)
L
3

You can use NavigationView from Material design. For ex :

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <include
        layout="@layout/app_bar_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <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" />

</android.support.v4.widget.DrawerLayout>
Lasley answered 21/2, 2016 at 16:32 Comment(0)
I
1

Then Use these codes @amal i think this ll help you. XML:

<!-- Framelayout to display Fragments -->

<FrameLayout
    android:id="@+id/frame_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="48dp"
        android:background="@drawable/counter_bg" >

        <ImageView
            android:id="@+id/iconl"
            android:layout_width="25dp"
            android:layout_height="wrap_content"
            android:layout_gravity="left"
            android:layout_marginLeft="12dp"
            android:layout_marginRight="12dp"
            android:src="@drawable/ic_launcher" />

        <ImageView
            android:id="@+id/iconr"
            android:layout_width="25dp"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:layout_marginRight="17dp"
            android:src="@drawable/ic_launcher" />
    </RelativeLayout>
</FrameLayout>

<!-- Listview to display slider menu -->

<ListView
    android:id="@+id/list_slidermenu"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:background="@color/list_background"
    android:choiceMode="singleChoice"
    android:divider="@color/list_divider"
    android:dividerHeight="1dp"
    android:listSelector="@drawable/list_selector" />

<ListView
    android:id="@+id/list_slidermenu2"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="end"
    android:background="@color/list_background"
    android:choiceMode="singleChoice"
    android:divider="@color/list_divider"
    android:dividerHeight="1dp"
    android:listSelector="@drawable/list_selector" />

//set the required images

Activity code :

ImageView iconl,iconr;

private DrawerLayout mDrawerLayout;
private ListView mDrawerList,mDrawerList2;

ImageView iconl,iconr;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    iconl = (ImageView)findViewById(R.id.iconl);
    iconr = (ImageView)findViewById(R.id.iconr);

    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mDrawerList = (ListView) findViewById(R.id.list_slidermenu);
    mDrawerList2 = (ListView) findViewById(R.id.list_slidermenu2);
    mDrawerList.setOnItemClickListener(new SlideMenuClickListener());
    mDrawerList2.setOnItemClickListener(new SlideMenuClickListener());
    iconl.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            mDrawerLayout.openDrawer(Gravity.START);
            mDrawerLayout.closeDrawer(Gravity.END);
        }
    });
    iconr.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            mDrawerLayout.openDrawer(Gravity.END);
            mDrawerLayout.closeDrawer(Gravity.START);
        }
    });
}
}

and here you can set your own list adapter for both lists and on item click call displayView(position); method where you can add your fragment to framelayout.

/**
 * Diplaying fragment view for selected nav drawer list item
 * */
private void displayView(int position) {
    // update the main content by replacing fragments
    Fragment fragment = null;
    switch (position) {
    case 0:
        fragment = new HomeFragment();
        break;


    default:
        break;
    }

    if (fragment != null) 
    {
        FragmentManager fragmentManager = getFragmentManager();
        fragmentManager.beginTransaction()
        .replace(R.id.frame_container, fragment).commit();

        // update selected item and title, then close the drawer
        mDrawerList.setItemChecked(position, true);
        mDrawerList.setSelection(position);

        mDrawerLayout.closeDrawer(mDrawerList);
    } else {
        // error in creating fragment
        Log.e("MainActivity", "Error in creating fragment");
    }
}
Irreversible answered 25/4, 2014 at 7:38 Comment(0)
Z
1

I know this is an old question but for those who are still looking for the answer :

Yes, it is possible. Please check my answer on the link below :

https://mcmap.net/q/235036/-how-to-set-navigation-drawer-to-be-opened-from-right-to-left

Zaragoza answered 30/7, 2014 at 7:55 Comment(0)
M
1

To set navigation drawer from right of the screen, make drawer layout parent of the navigation view and set layout gravity of navigation view to the right.

Menado answered 14/4, 2018 at 18:4 Comment(0)
W
1

write this code into your Main.java and im1 is your navigationbar icon on the top right in xml file.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);
    im1=findViewById(R.id.humburgericon);
    im1.setOnClickListener(new View.OnClickListener() {
        @SuppressLint("WrongConstant")
        @Override
        public void onClick(View v) {
              drawerLayout.openDrawer(Gravity.END);});
Whap answered 11/1, 2020 at 7:37 Comment(0)
C
1

Navigation Drawer from right hand side is possible. And this easier than it seems. In my opinion the most simple solution is:

  1. Extend DrawerLayout class and override open() and close() functions as below

     class EndDrawerLayout : DrawerLayout {
         constructor(context: Context) : super(context)
         constructor(context: Context, attrs: AttributeSet?) : super(context, attrs)
         constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int): super(context, attrs, defStyleAttr)
         override fun open() = openDrawer(GravityCompat.END)
         override fun close() = closeDrawer(GravityCompat.END)
     }
    
  2. Use the custom DrawerLayout in your XML

  3. Set NavigationView attribute android:layout_gravity="end"

     <com.custom.myapplication.ui.EndDrawerLayout
         xmlns:android="http://schemas.android.com/apk/res/android"
         xmlns:app="http://schemas.android.com/apk/res-auto"
         xmlns:tools="http://schemas.android.com/tools"
         android:id="@+id/drawer_layout"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         android:fitsSystemWindows="true"
         tools:openDrawer="end">
    
     <include
         android:id="@+id/app_bar_main"
         layout="@layout/app_bar_main"
         android:layout_width="match_parent"
         android:layout_height="match_parent" />
    
     <com.google.android.material.navigation.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" />
    
     </com.custom.myapplication.ui.EndDrawerLayout>
    
  4. Enjoy

Caphaitien answered 26/3, 2022 at 9:6 Comment(0)
E
0

The correct answer is: android:layoutDirection="rtl"

<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/my_drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layoutDirection="rtl"
tools:openDrawer="start"
tools:context=".MainActivity">

This also applies to the toolbar, which is then opened from right to left

Esquivel answered 25/11, 2023 at 13:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.