-1

I want use two DrawerLayouts in one Activity. One of them opens on the left side and the other of them opens on the right side. They should open after a click on the ToolBar.

For example : if I click the left button on the ToolBar, it opens the left DrawerLayout and if click the right button on the ToolBar, it opens the right DrawerLayout.

Please see these images to understand my meaning.

my XML code :

<android.support.v4.widget.DrawerLayout 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:id="@+id/DrawerLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout 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:background="@color/search_hint"
        tools:context=".Main_Page">

        <include
            android:id="@+id/app_bar"
            layout="@layout/app_toolbar" />

        <com.ogaclejapan.smarttablayout.SmartTabLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:id="@+id/viewpagertab"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:layout_below="@+id/app_bar"
            android:background="@color/primaryColor"
            android:clipToPadding="true"
            android:paddingLeft="10dp"
            android:paddingRight="10dp"
            app:stl_defaultTabTextColor="@color/search_hint"
            app:stl_defaultTabTextHorizontalPadding="18dp"
            app:stl_defaultTabTextSize="15sp"
            app:stl_distributeEvenly="true"
            app:stl_dividerColor="@color/transparent"
            app:stl_dividerThickness="0dp"
            app:stl_indicatorColor="@color/black_20"
            app:stl_indicatorCornerRadius="18dp"
            app:stl_indicatorGravity="center"
            app:stl_indicatorInterpolation="smart"
            app:stl_indicatorThickness="36dp"
            app:stl_underlineColor="@color/transparent"
            app:stl_underlineThickness="0dp" />

        <android.support.v4.view.ViewPager
            android:id="@+id/viewpager"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@id/viewpagertab">

        </android.support.v4.view.ViewPager>


        <co.aenterhy.toggleswitch.ToggleSwitchButton
            android:id="@+id/toggle"
            style="@style/ToggleSwitchButton" />

    </RelativeLayout>

    <android.support.design.widget.NavigationView
        android:id="@+id/main_drawer"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:menu="@menu/menu_main__page"
        app:headerLayout="@layout/header_drawer_main"
        app:itemTextColor="@color/primaryColor"
        app:itemIconTint="@color/primaryColor"/>

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

java code :

toolbar = (Toolbar) findViewById(R.id.app_bar);
TextView mTitle = (TextView) toolbar.findViewById(R.id.toolbar_title);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
mTitle.setText("title");
Drawer = (DrawerLayout) findViewById(R.id.DrawerLayout);
mNaviView = (NavigationView) findViewById(R.id.main_drawer);
mNaviView.setNavigationItemSelectedListener(this);
mDrawerToggle = new ActionBarDrawerToggle(this, Drawer, toolbar, R.string.openDrawer, R.string.closeDrawer) {

    @Override
    public void onDrawerOpened(View drawerView) {
        super.onDrawerOpened(drawerView);
        if (con.isOnline()) {
            new get_menu_info().execute(public_username);
        }
    }

    @Override
    public void onDrawerClosed(View drawerView) {
        super.onDrawerClosed(drawerView);

    }
};
Drawer.setDrawerListener(mDrawerToggle);
mDrawerToggle.syncState();

ImageView e1 = (ImageView) toolbar.findViewById(R.id.right_tool);
e1.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        Drawer.openDrawer(Gravity.RIGHT);
    }
});

LogCot Error :

10-15 10:44:31.130    7061-7061/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.tellfa.smsbox, PID: 7061
    java.lang.IllegalArgumentException: No drawer view found with gravity RIGHT
            at android.support.v4.widget.DrawerLayout.openDrawer(DrawerLayout.java:1322)
            at com.tellfa.smsbox.activities.Main_Page$5.onClick(Main_Page.java:210)
            at android.view.View.performClick(View.java:4764)
            at android.view.View$PerformClick.run(View.java:19844)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5349)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:908)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:703)

enter image description here enter image description here

ydstsh
  • 183
  • 2
  • 11

4 Answers4

2

You should put the following in a click listener for the Toolbar icon:

drawerLayout.openDrawer(GravityCompat.START);

See the docs for more info.

fractalwrench
  • 4,028
  • 7
  • 33
  • 49
2

For the Activity XML, you just add another View using android:layout_gravity="end", and you interact with the right one the same as you would with the left one.

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

<!-- The main content view -->

<FrameLayout
    android:id="@+id/content_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<!-- The left navigation drawer -->

<ListView
    android:id="@+id/left_drawer"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:background="@color/list_background"
    android:choiceMode="singleChoice"
    android:divider="@color/list_divider"
    android:dividerHeight="1dp" />

<!-- The right navigation drawer -->

<ListView
    android:id="@+id/right_drawer"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="end"
    android:background="@color/list_background"
    android:choiceMode="singleChoice"
    android:divider="@color/list_divider"
    android:dividerHeight="1dp" />

</android.support.v4.widget.DrawerLayout>
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
1

You can open or close your drawer with the code below.

/**
* For your right drawer
*/
drawerLayout.openDrawer(Gravity.RIGHT); 

drawerLayout.closeDrawer(Gravity.RIGHT);

/**
* For your right drawer
*/
drawerLayout.openDrawer(Gravity.LEFT); 

drawerLayout.closeDrawer(Gravity.LEFT);
vguzzi
  • 2,420
  • 2
  • 15
  • 19
1

Assuming your drawers are leftDrawer and rightDrawer respectively, you should be able to use

drawerLayout.openDrawer(leftDrawer);

to open the left drawer,

drawerLayout.closeDrawer(rightDrawer);

to open the right drawer

drawerLayout.closeDrawer(leftDrawer);

to close the left drawer, and

drawerLayout.closeDrawer(rightDrawer);

to close the right drawer, respectively.

Update (code posted)

Use

Drawer.closeDrawer(mNaviView);

and

Drawer.closeDrawer(mNaviView);

See also DrawerLayout Double Drawer (Left and Right Drawers simultaneously).

Community
  • 1
  • 1
serv-inc
  • 35,772
  • 9
  • 166
  • 188