How to set Navigation Drawer to be opened from right to left
In your main layout set your ListView
gravity to right:
android:layout_gravity="right"
Also in your code :
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
R.drawable.ic_drawer, R.string.drawer_open,
R.string.drawer_close) {
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item != null && item.getItemId() == android.R.id.home) {
if (mDrawerLayout.isDrawerOpen(Gravity.RIGHT)) {
mDrawerLayout.closeDrawer(Gravity.RIGHT);
}
else {
mDrawerLayout.openDrawer(Gravity.RIGHT);
}
}
return false;
}
};
hope it works :)
Android - Is Navigation Drawer from right hand side possible?
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
Android-NavigationView from right to left
First of all replace on DrawerLayout the tools:openDrawer="start"
with tools:openDrawer="end"
. Now your problem is on the toggle, which opens the left drawer, and because you have only right drawer it throws the exception. You can add your own button on the actionbar (on the right side) to open the drawer. To do that change your app_bar.xml like this
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
<FrameLayout
android:id="@+id/drawer_button"
android:layout_width="50dp"
android:layout_height="?attr/actionBarSize"
android:layout_alignParentRight="true"
android:clickable="true">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|center_vertical"
android:src="@mipmap/ic_drawer" />
</FrameLayout>
</RelativeLayout>
</android.support.design.widget.AppBarLayout>
<include layout="@layout/content_main" />
</android.support.design.widget.CoordinatorLayout>
Then change your onCreate method from your Activity like this
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
findViewById(R.id.drawer_button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// open right drawer
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.openDrawer(GravityCompat.END);
}
});
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
Also you can find the image for your drawer here or here.
I hope this will help you. :)
How does Fragment Navigation Drawer open from right to left?
The problem is ActionBarDrawerToggle
always expects the drawer to be on the side corresponding to Gravity.START
. In most locales this is on the left side, hence it tries to open the drawer with Gravity.LEFT
, but yours is on the other side so it throws an exception instead. Whether or not this seems like an intelligent design decision, it is what it is.
If you are following the Material Design guidelines, your drawer will cover the action bar when it opens (it might already be doing this). Specifically for this reason, I don't like using ActionBarDrawerToggle
and don't find it particularly useful; the animation of the menu icon to an arrow is obscured by the drawer, and the other functionality is easy enough to implement directly in the activity. In your case, the animation might actually be useful if your drawer on the other side doesn't cover the icon, but then again you might not want the icon to animate at all anyway.
In your onCreate()
method, all you need to do is
- Call
toolbar.setNavigationOnClickListener()
(or set anOnClickListener
on a view in yourToolbar
) and have this listener toggle the drawer usingdrawerLayout.openDrawer(Gravity.RIGHT)
and the equivalentcloseDrawer()
method. - Use
drawerLayout.addDrawerListener()
to add a listener. When the drawer opens or closes, you can use this listener to change things like the toolbar's icon and/or its contentDescription (for accessibility), or to do anything else you like
How to set Navigation Drawer to be opened from right to left?
Set ListView gravity right
or end
instead of start
android:layout_gravity="right"
Also update the DrawerToggle
to :
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
R.drawable.ic_drawer, R.string.drawer_open,
R.string.drawer_close) {
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item != null && item.getItemId() == android.R.id.home) {
if (mDrawerLayout.isDrawerOpen(Gravity.RIGHT)) {
// Notice the Gravity.Right
mDrawerLayout.closeDrawer(Gravity.RIGHT);
} else {
mDrawerLayout.openDrawer(Gravity.RIGHT);
}
}
return false;
}
};
From Docs :
The drawer view (the ListView) must specify its horizontal gravity with the android:layout_gravity attribute. To support right-to-left (RTL) languages, specify the value with "start" instead of "left" (so the drawer appears on the right when the layout is RTL).
Related Topics
Getcolor(Int Id) Deprecated on Android 6.0 Marshmallow (API 23)
How to Set Ringtone in Android from My Activity
Android and Facebook Share Intent
How to Send a File in Android from a Mobile Device to Server Using Http
Android Context.Getresources.Updateconfiguration() Deprecated
How to Change the Floating Label Color of Textinputlayout
API Key for Gcm Is Suddenly Invalid? Unauthorized (401) Error
Force "Portrait" Orientation Mode
How to Have Placeholders in Strings.Xml for Runtime Values
How to Hang Up Outgoing Call in Android
Communication Between Android Java and Phonegap JavaScript
How to Control the Width and Height of the Default Alert Dialog in Android
Android: How to Auto-Restart an Application After It Has Been "Force Closed"
How to Send a Post Request Using Volley with String Body
Programmatically Go Back to the Previous Fragment in the Backstack
Runtimeexception: Your Content Must Have a Listview Whose Id Attribute Is 'Android.R.Id.List'