Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set Navigation Drawer to be opened from right to left

First of all I know this question appeared here before but after trying a lot I still didn't succeed. I working on the example from Android Developers site.

I'm trying to set the menu to be opened from right to left instead of how its implementing in the example (from left to right). In addition I want to move the open menu button to the right side of the action bar. I also red some answers here, for example in this answer.

I try to change the gravity of the views and the layouts but I get the error:

no drawer view found with absolute gravity LEFT

Can you please help me to figure out what is the problem in my code and what should I change in order to set the menu to be opened from the right, and to move the action bar button to the right side?

the xml code is here:

<android.support.v4.widget.DrawerLayout      xmlns:android="http://schemas.android.com/apk/res/android"     android:id="@+id/drawer_layout"     android:layout_gravity="right"     android:layout_width="match_parent"     android:layout_height="match_parent" >      <FrameLayout         android:id="@+id/content_frame"         android:layoutDirection="rtl"         android:layout_width="match_parent"         android:layout_height="match_parent"         />      <ListView android:id="@+id/left_drawer"         android:layout_width="200dp"         android:layout_height="match_parent"         android:layout_gravity="right"         android:choiceMode="singleChoice"         android:divider="@android:color/transparent"         android:dividerHeight="10dp"         android:background="#111"/>  </android.support.v4.widget.DrawerLayout> 
like image 670
galvan Avatar asked Aug 31 '13 10:08

galvan


People also ask

How do I customize my navigation drawer?

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml. Step 3 − Add the following code to res/layout/nav_header_main.

How do I open a navigation drawer?

The user can view the navigation drawer when they swipe a finger from the left edge of the activity. They can also find it from the home activity (the top level of the app) by tapping the app icon (also known as the Android "hamburger" menu) in the action bar.

How do you change the Drawerlayout icon on the right side of the device screen?

Edit. According to Creating a Navigation Drawer, 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).


2 Answers

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 :)

like image 65
Rudi Avatar answered Sep 20 '22 03:09

Rudi


Add this code to manifest:

<application android:supportsRtl="true"> 

and then write this code on Oncreate:

getWindow().getDecorView().setLayoutDirection(View.LAYOUT_DIRECTION_RTL); 

It works for me. ;)

like image 43
Amir Bax Avatar answered Sep 22 '22 03:09

Amir Bax