androidactionbardrawertoggle

How to use ActionBarDrawerToggle to close both left & right navigation drawers?


I have the following layout in my activity.xml

`

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/store_page_layout"
    tools:context=".StorePage">
    <include
        android:id="@+id/store_page_toolbar"
        layout="@layout/toolbar"/>
    <android.support.v4.widget.DrawerLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/store_drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/store_page_toolbar">
        <FrameLayout
            android:id="@+id/container_body"
            android:layout_width="fill_parent"
            android:layout_height="@dimen/container_body_height"
            android:layout_weight="1">
            <android.support.v7.widget.CardView
                xmlns:card_view="http://schemas.android.com/apk/res-auto"
                android:id="@+id/card_view"
                android:layout_gravity="center"
                android:layout_width="match_parent"
                android:layout_height="200dp"
                android:layout_marginRight="16dp"
                android:layout_marginLeft="16dp"
                android:elevation="6dp"
                card_view:cardCornerRadius="4dp"
                android:background="@drawable/landing_animated_button_background">
            </android.support.v7.widget.CardView>
        </FrameLayout>
        <android.support.v7.widget.RecyclerView
            android:id="@+id/store_menu_drawer"
            android:layout_width="@dimen/navigation_drawer_width"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:background="@color/black_200"/>
        <android.support.v7.widget.RecyclerView
            android:id="@+id/store_cart_drawer"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="end"
            android:layout_marginLeft="@dimen/nav_drawer_left_min_margin"
            android:background="@color/black_200"/>
    </android.support.v4.widget.DrawerLayout>
</RelativeLayout>

`

No I went ahead and added ActionBarDrawerToggle on my Toolbar widget, the behavior I wanted from the hamburger icon is if I click on it left drawer open up (Working), I click it again left drawer closes (Working), I open right drawer by dragging from right to left plus the hamburger icon changes to arrow (Working), if I click on arrow icon it closes right drawer as well (Not working)

As you can see, I want the hamburger icon to close both right and left drawers based on which one is open, my approach is to listen to click on arrow icon and decide which drawer is open then close it. I am unable to figure out how to set onClickListener on the hamburger or arrow icon automatically added by ActionBarDrawerToggle class.


Solution

  • I finally figured out the solution. Instead of setting setToolbarNavigationClickListener on actionBarDrawerToggle setting setNavigationOnClickListener on Toolbar works perfectly. my code is given below.

    toolbar.setNavigationOnClickListener(new toolBarNavigationIconListener());
    

    And OnClickListener is as follows.

        private class toolBarNavigationIconListener implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            if(!storeDrawer.isDrawerOpen(Gravity.RIGHT) && !storeDrawer.isDrawerOpen(Gravity.LEFT)) {
                storeDrawer.openDrawer(Gravity.LEFT);
            } else if(storeDrawer.isDrawerOpen(Gravity.LEFT)) {
                storeDrawer.closeDrawer(Gravity.LEFT);
            } else if(storeDrawer.isDrawerOpen(Gravity.RIGHT)) {
                storeDrawer.closeDrawer(Gravity.RIGHT);
            }
        }
    }