Search code examples
androidnavigationnavigation-drawerillegalargumentexceptiondrawerlayout

Android navigation drawer right side not working


I have a navigation drawer and I try to change its opening position. I want my navigation drawer to open on the right side.

This is my navigation drawer xml code:

<ge.me.custom.CustomDrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout5"
android:layout_width="200dp"
android:layout_height="match_parent" >
<FrameLayout
    android:id="@+id/content_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
<!-- The navigation drawer -->
<LinearLayout
    android:id="@+id/drawer"
    android:layout_width="200dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:background="#09b52f"
    android:orientation="vertical" >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <RelativeLayout
            android:id="@+id/slidemenumainlayout"
            android:layout_width="match_parent"
            android:layout_height="120dp"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true" >

            <ImageView
                android:id="@+id/slidemenuuserimage"
                android:layout_width="70dp"
                android:layout_height="70dp"
                android:layout_alignParentBottom="true"
                android:layout_alignParentLeft="true"
                android:layout_marginBottom="16dp"
                android:layout_marginLeft="14dp"
                android:background="@drawable/ic_user_blue" />

        </RelativeLayout>

        <ListView
            android:id="@+id/drawer_list"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignRight="@+id/slidemenumainlayout"
            android:layout_below="@+id/slidemenumainlayout"
            android:divider="#00af25"
            android:dividerHeight="1dp" >
        </ListView>
    </RelativeLayout>
</LinearLayout>

This is my custom drawer:

public class CustomDrawerLayout extends DrawerLayout {

    public CustomDrawerLayout(Context context) {
        super(context);
    }

    public CustomDrawerLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CustomDrawerLayout(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        widthMeasureSpec = MeasureSpec.makeMeasureSpec(
            MeasureSpec.getSize(widthMeasureSpec), MeasureSpec.EXACTLY);
        heightMeasureSpec = MeasureSpec.makeMeasureSpec(
            MeasureSpec.getSize(heightMeasureSpec), MeasureSpec.EXACTLY);
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

}

And this code is inside the main activity:

  mDrawerToggle = new ActionBarDrawerToggle(getActivity(), mDrawerLayout,
            R.drawable.ic_drawer, R.string.app_name,
            R.string.app_name) {

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

    mDrawerLayout.setDrawerListener(mDrawerToggle);

I wrote a method to open my drawer on the right side but when I run my app, it crashes.

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

If I call the method like this:

openDrawer(Gravity.START);

I do not get an error but the navigation drawer's start position is left.

This is my error:

java.lang.IllegalArgumentException: No drawer view found with gravity RIGHT

How can I open the navigation drawer on the right side?


Solution

  • Try setting layout_gravity to right in your xml:

    <LinearLayout
        android:id="@+id/drawer"
        android:layout_width="200dp"
        android:layout_height="match_parent"
        android:layout_gravity="right"
        android:background="#09b52f"
        android:orientation="vertical" >