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