0

I have created created one navigation drawer following this tutorial

But what I need is , I need the same drawer menu open from right side . Anyone know how to achieve this?

Following is my xml

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".MainActivity">

<LinearLayout
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:orientation="vertical"
    >
    <include
        android:id="@+id/toolbar"
        layout="@layout/tool_bar"
    />
    <FrameLayout
        android:id="@+id/frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </FrameLayout>

</LinearLayout>
<android.support.design.widget.NavigationView
    android:id="@+id/navigation_view"
    android:layout_height="match_parent"
    android:layout_width="wrap_content"
    android:layout_gravity="right"
    app:headerLayout="@layout/header"
    app:menu="@menu/drawer"
    />

and in code

  // Initializing Drawer Layout and ActionBarToggle
    drawerLayout = (DrawerLayout) findViewById(R.id.drawer);
    ActionBarDrawerToggle actionBarDrawerToggle = new ActionBarDrawerToggle(this,drawerLayout,toolbar,R.string.openDrawer, R.string.closeDrawer){

        @Override
        public void onDrawerClosed(View drawerView) {
            // Code here will be triggered once the drawer closes as we dont want anything to happen so we leave this blank
            super.onDrawerClosed(drawerView);
        }

        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            if (item != null && item.getItemId() == android.R.id.home) {
                if (drawerLayout.isDrawerOpen(Gravity.RIGHT)) {
                    drawerLayout.closeDrawer(Gravity.RIGHT);
                } else {
                    drawerLayout.openDrawer(Gravity.RIGHT);
                }
            }
            return false;
        }

        @Override
        public void onDrawerOpened(View drawerView) {
            // Code here will be triggered once the drawer open as we dont want anything to happen so we leave this blank

            super.onDrawerOpened(drawerView);
        }

    };
  • possible duplicate of [How can I open navigation drawer from right side to left](http://stackoverflow.com/questions/17852091/how-can-i-open-navigation-drawer-from-right-side-to-left) – Parth Bhayani Sep 07 '15 at 12:54
  • check this > http://stackoverflow.com/questions/17156340/android-is-navigation-drawer-from-right-hand-side-possible – Maniya Joe Sep 07 '15 at 13:55
  • check this > http://stackoverflow.com/questions/17156340/android-is-navigation-drawer-from-right-hand-side-possible – Maniya Joe Sep 07 '15 at 13:57

4 Answers4

5

try this in your navigation View

android:layout_gravity="end" in place of android:layout_gravity="start"

 <android.support.design.widget.NavigationView
        android:id="@+id/navigation_view"
        android:layout_height="match_parent"
        android:layout_width="wrap_content"
        android:layout_gravity="end"
        app:headerLayout="@layout/header"
        app:menu="@menu/drawer"
        />

// Add these lines in your java activity file , it will works

 @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        switch (item.getItemId()) {
            case android.R.id.home:
                if (mDrawer.isDrawerOpen(Gravity.RIGHT)) {
                    mDrawer.closeDrawer(Gravity.RIGHT);
                } else {
                    mDrawer.openDrawer(Gravity.RIGHT);
                }
                return true;
            case R.id.action_settings:
                return true;
        }

final update:

I change some lines in your sample project(MainActivity.java), now it is working , use this code.

do not forget to write android:layout_gravity="end" in place of android:layout_gravity="start" in your xml file.

package com.android4dev.navigationview;


import android.support.design.widget.NavigationView;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;


public class MainActivity extends AppCompatActivity {

    Toolbar toolbar;
    NavigationView navigationView;
    DrawerLayout drawerLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        toolbar=(Toolbar)findViewById(R.id.toolbar);
        navigationView=(NavigationView)findViewById(R.id.navigation_view);
        drawerLayout=(DrawerLayout)findViewById(R.id.drawer);


        // Set a Toolbar to replace the ActionBar.
        setToolbarAsActionBar();
        // Setup drawer view
        setupDrawerContent(navigationView);

        // Set the menu icon instead of the launcher icon.
        final ActionBar ab = getSupportActionBar();
        ab.setHomeAsUpIndicator(R.drawable.ic_menu);
        ab.setDisplayHomeAsUpEnabled(true);
        //ab.setDisplayShowTitleEnabled(false);

        Menu menu = navigationView.getMenu();
        MenuItem item = menu.findItem(R.id.starred);
        selectDrawerItem(item);

    }

    private void setToolbarAsActionBar() {
        setSupportActionBar(toolbar);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        switch (item.getItemId()) {
            case android.R.id.home:
                if (drawerLayout.isDrawerOpen(Gravity.RIGHT)) {
                    drawerLayout.closeDrawer(Gravity.RIGHT);
                } else {
                    drawerLayout.openDrawer(Gravity.RIGHT);
                }
                return true;
            case R.id.action_settings:
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    private void setupDrawerContent(NavigationView navigationView) {

        navigationView.setNavigationItemSelectedListener(
                new NavigationView.OnNavigationItemSelectedListener() {
                    @Override
                    public boolean onNavigationItemSelected(MenuItem menuItem) {

                        //Checking if the item is in checked state or not, if not make it in checked state
                        if (menuItem.isChecked()) menuItem.setChecked(false);
                        else menuItem.setChecked(true);

                        selectDrawerItem(menuItem);
                        return true;
                    }
                });
    }

    public void selectDrawerItem(MenuItem menuItem) {
        // Create a new fragment and specify the planet to show based on
        // position

        switch (menuItem.getItemId()) {

            case R.id.inbox:
                Toast.makeText(getApplicationContext(), "Inbox Selected", Toast.LENGTH_SHORT).show();
                ContentFragment fragment = new ContentFragment();
                android.support.v4.app.FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
                fragmentTransaction.replace(R.id.frame, fragment);
                fragmentTransaction.commit();
                break;

            case R.id.starred:
                Toast.makeText(getApplicationContext(), "Stared Selected", Toast.LENGTH_SHORT).show();
                break;

            case R.id.sent_mail:
                Toast.makeText(getApplicationContext(), "Send Selected", Toast.LENGTH_SHORT).show();
                break;

            case R.id.drafts:
                Toast.makeText(getApplicationContext(), "Drafts Selected", Toast.LENGTH_SHORT).show();
                break;

            case R.id.allmail:
                Toast.makeText(getApplicationContext(), "All Mail Selected", Toast.LENGTH_SHORT).show();
                break;

            case R.id.trash:
                Toast.makeText(getApplicationContext(), "Trash Selected", Toast.LENGTH_SHORT).show();
                break;

            case R.id.spam:
                Toast.makeText(getApplicationContext(), "Spam Selected", Toast.LENGTH_SHORT).show();
                break;

            default:
                Toast.makeText(getApplicationContext(), "Somethings Wrong", Toast.LENGTH_SHORT).show();
                break;

        }

        drawerLayout.closeDrawers();
    }

    private ActionBarDrawerToggle setupDrawerToggle() {
        return new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.openDrawer, R.string.closeDrawer);
    }

}
Himanshu Shekher Jha
  • 1,334
  • 2
  • 14
  • 27
  • 2
    tried I'm getting this error. `java.lang.IllegalArgumentException: No drawer view found with gravity LEFT` –  Sep 07 '15 at 13:02
  • While swiping it is working , but on clicking on hamburger icon , it shows error `java.lang.IllegalArgumentException: No drawer view found with gravity LEFT` –  Sep 07 '15 at 13:47
  • I applied my code on my navigation drawer, it's is working fine here .. the only difference is that , I am not working with onOptionItemSelected, I am using setToolbarNavigationClickListener, I am giving u my code , let try with it. – Himanshu Shekher Jha Sep 07 '15 at 16:45
  • I tried with this code also. but doesn't work :( . I don't know what is missing. I am using [this code](https://github.com/AkashBang/NavigationView) . can u pls look into this? where I have to make change here? –  Sep 08 '15 at 04:01
  • Go through this link https://guides.codepath.com/android/Fragment-Navigation-Drawer , and change the code as i above mentioned, it will work – Himanshu Shekher Jha Sep 08 '15 at 05:12
  • Thank you, it works now , it was the problem with the toolbar –  Sep 08 '15 at 08:53
  • 1
    I still get this error java.lang.IllegalArgumentException: No drawer view found with gravity LEFT @user3339689 can you please suggest what problem is with toolbar? – Pinkesh Darji Oct 10 '17 at 10:17
1

It's possible. You must set android:layout_gravity="right" in your Nav Drawer and also change the gravity:

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

That should work!

Mariano Zorrilla
  • 7,165
  • 2
  • 34
  • 52
  • 1
    I'm getting this `java.lang.IllegalArgumentException: No drawer view found with gravity LEFT` –  Sep 07 '15 at 13:04
  • can you post your DrawerLayout declaration and xml? – Mariano Zorrilla Sep 07 '15 at 13:06
  • 1
    While swiping it is working , but on clicking on hamburger icon , it shows error java.lang.IllegalArgumentException: No drawer view found with gravity LEFT – –  Sep 07 '15 at 13:49
0

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); 
        } 
    } 
 });
SHS
  • 1,414
  • 4
  • 26
  • 43
0

Try this it is working for me

@Override
        public boolean onOptionsItemSelected(MenuItem item) {

            switch (item.getItemId()) {
                case android.R.id.home:
                    if (drawer.isDrawerOpen(Gravity.RIGHT)) {
                        drawer.closeDrawer(Gravity.RIGHT);
                    } else {
                        drawer.openDrawer(Gravity.RIGHT);
                    }
                    return true;
                case R.id.action_settings:
                    // do what you want
                    return true;
            }
            return false;
        }