0

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.

Anfal
  • 344
  • 1
  • 3
  • 15

2 Answers2

0

This is explained in the documentation

http://developer.android.com/reference/android/support/v7/app/ActionBarDrawerToggle.html#setToolbarNavigationClickListener(android.view.View.OnClickListener)

public void setToolbarNavigationClickListener (View.OnClickListener onToolbarNavigationClickListener)

When DrawerToggle is constructed with a Toolbar, it sets the click listener on the Navigation icon. If you want to listen for clicks on the Navigation icon when DrawerToggle is disabled (setDrawerIndicatorEnabled(boolean), you should call this method with your listener and DrawerToggle will forward click events to that listener when drawer indicator is disabled.

Kuffs
  • 35,581
  • 10
  • 79
  • 92
  • I did try that as well & still got no response. `actionBarDrawerToggle.setToolbarNavigationClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Log.d("ICON_CLICKED", "Clicked"); } });` – Anfal Dec 07 '15 at 11:48
  • I would check your code carefully then as I doubt the documentation would state this if it did was not true. – Kuffs Dec 07 '15 at 12:12
  • could you point out on some example code using this listener? maybe i can compare mine with it to see whats going on wrong. – Anfal Dec 07 '15 at 12:55
0

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);
        }
    }
}
Anfal
  • 344
  • 1
  • 3
  • 15
  • Thanks for sharing your solution. To understand it I would like to know how is your variable `storeDrawer` defined? – Bruno Bieri Aug 13 '19 at 14:38