Skip to content Skip to sidebar Skip to footer

Android-NavigationView From Right To Left

I'm using The last version of Android Studio (1.5) and I want to make a menu using Drawer Layout, for position its call GravityCompat. I'm trying to use this components and modify

Solution 1:

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


Solution 2:

use a custom drawerLayout just like this :

public class TaskManagerDrawerLayout  extends DrawerLayout {

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

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

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

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    try {
        return super.onInterceptTouchEvent(ev);
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
        return false;
    }
}

@Override
public void closeDrawer(int gravity) {
    super.closeDrawer(GravityCompat.END);
}

@Override
public void openDrawer(int gravity) {
    super.openDrawer(GravityCompat.END);
}

}

Solution 3:

After implementing left side navigation drawer change the following properties.

tools:openDrawer="start" in DrawerLayout
android:layout_gravity="end" in NavigationView  
toolbar.setNavigationIcon (R.color.transparent); mDrawerToggle.setDrawerIndicatorEnabled (false);

toolbar.setNavigationOnClickListener (new View.OnClickListener () { @Override public void onClick(View view) { if (mDrawerLayout.isDrawerOpen (Gravity.END)) { mDrawerLayout.closeDrawer (Gravity.END); } else { mDrawerLayout.openDrawer (Gravity.END); } } });

Post a Comment for "Android-NavigationView From Right To Left"