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.CoordinatorLayoutxmlns: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.AppBarLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:theme="@style/AppTheme.AppBarOverlay"><RelativeLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"><android.support.v7.widget.Toolbarandroid:id="@+id/toolbar"android:layout_width="match_parent"android:layout_height="?attr/actionBarSize"android:background="?attr/colorPrimary"app:popupTheme="@style/AppTheme.PopupOverlay" /><FrameLayoutandroid:id="@+id/drawer_button"android:layout_width="50dp"android:layout_height="?attr/actionBarSize"android:layout_alignParentRight="true"android:clickable="true"><ImageViewandroid: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><includelayout="@layout/content_main" /></android.support.design.widget.CoordinatorLayout>

Then change your onCreate method from your Activity like this

@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbartoolbar= (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    findViewById(R.id.drawer_button).setOnClickListener(newView.OnClickListener() {
        @OverridepublicvoidonClick(View v) {
            // open right drawerDrawerLayoutdrawer= (DrawerLayout) findViewById(R.id.drawer_layout);
            drawer.openDrawer(GravityCompat.END);
        }
    });

    NavigationViewnavigationView= (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 :

publicclassTaskManagerDrawerLayoutextendsDrawerLayout {

publicTaskManagerDrawerLayout(Context context) {
    super(context);
}

publicTaskManagerDrawerLayout(Context context, AttributeSet attrs) {
    super(context, attrs);
}

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

@OverridepublicbooleanonInterceptTouchEvent(MotionEvent ev) {
    try {
        returnsuper.onInterceptTouchEvent(ev);
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
        returnfalse;
    }
}

@OverridepublicvoidcloseDrawer(int gravity) {
    super.closeDrawer(GravityCompat.END);
}

@OverridepublicvoidopenDrawer(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"