Android - Is Navigation Drawer from right hand side possible?

AndroidNavigation Drawer

Android Problem Overview


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? :(

Android Solutions


Solution 1 - Android

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>

Solution 2 - Android

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

Solution 3 - Android

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);
        }
    }

Solution 4 - Android

To add to https://stackoverflow.com/a/21781710/437039 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.

Solution 5 - Android

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>

Solution 6 - Android

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

http://schemas.android.com/apk/res/android" android:id="@+id/drawer_layout" android:layout_width="match_parent" android:layout_height="match_parent" >

<!-- 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");
	}
}

Solution 7 - Android

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://stackoverflow.com/a/19358114/1572408

Solution 8 - Android

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.

Solution 9 - Android

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);});

Solution 10 - Android

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser2062024View Question on Stackoverflow
Solution 1 - AndroidJeshurunView Answer on Stackoverflow
Solution 2 - AndroidLarry McKenzieView Answer on Stackoverflow
Solution 3 - AndroidamalBitView Answer on Stackoverflow
Solution 4 - AndroidsandaloneView Answer on Stackoverflow
Solution 5 - AndroidGunaseelanView Answer on Stackoverflow
Solution 6 - AndroidRohit GoswamiView Answer on Stackoverflow
Solution 7 - AndroidRudiView Answer on Stackoverflow
Solution 8 - AndroidVikash SharmaView Answer on Stackoverflow
Solution 9 - AndroidMohit BaghelView Answer on Stackoverflow
Solution 10 - AndroidСергей ЗайцевView Answer on Stackoverflow