Search code examples
androidnavigationviewside-menu

Android NavigationView from right side


I have created an Android application, in that I want to open slide menu from right side and when drawer is open I want to slide the view like attached image.

enter image description here

Code:

<android.support.v4.widget.DrawerLayout
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:openDrawer="start">


    <android.support.design.widget.NavigationView
        android:id="@+id/navigationView_Right_Home"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="right"
         android:fitsSystemWindows="true"  />

</android.support.v4.widget.DrawerLayout>

For slide view I have used below code, but it's not working

        @Override
        public void onDrawerSlide(View drawerView, float slideOffset) {
            super.onDrawerSlide(drawerView, slideOffset);
            View container = findViewById(R.id.ll);
            float moveFactor = (slideOffset - drawerView.getWidth());

            container.setTranslationX(moveFactor);

            drawer.bringChildToFront(drawerView);
            drawer.requestLayout();
        }

Solution

  • To slide the View to the left with the drawer, your moveFactor would be as follows:

    float moveFactor = -drawerView.getWidth() * slideOffset;
    

    Do note the negative sign in front of drawerView.getWidth().

    The slideOffset is a fraction that ranges from 0f to 1f. Multiplying that by the drawer's width gives the actual offset, and negating it, we translate the content View to the left.