1

I am trying ti implement ActionBarDrawerToggle with custom click events but it seems it's not working.

I've created new ActionBarDrawerToggle like this:

drawerToggle = new ActionBarDrawerToggle(activity, drawer, R.string.open, R.string.close);

Setup ActionBar like this:

getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);

Added custom listener like this:

drawerToggle.setDrawerIndicatorEnabled(false);
drawerToggle.setToolbarNavigationClickListener(myListener);

Result: Click event is never fired.

How can I attach custom click events to ActionBarDrawerToggle? By Google's documentation, the key is only this: drawerToggle.setDrawerIndicatorEnabled(false);. Thank you.

Cristiano
  • 3,099
  • 10
  • 45
  • 67

3 Answers3

3

I created an app as an example. See below please. Note especially three comments in MainActivity.java which describe how you should implement your listener.

Picture is more than a thousand words:

MainActivity.java

import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private ActionBarDrawerToggle mDrawerToggle;
    private DrawerLayout mDrawerLayout;

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

        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.open, R.string.close);
        // 1.
        // if you want the default behaviour for ActionBarDrawerToggle
        // uncomment the first line below and comment out the second one
//        mDrawerLayout.setDrawerListener(mDrawerToggle);
        mDrawerToggle.setDrawerIndicatorEnabled(false);
    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        mDrawerToggle.syncState();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        mDrawerToggle.onConfigurationChanged(newConfig);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // 2.
        // the default behaviour for ActionBarDrawerToggle
        if (mDrawerToggle.onOptionsItemSelected(item)) {
            return true;
        }

        int id = item.getItemId();

        if (id == android.R.id.home) {
            // 3.
            // here you can define your custom click listener / onClick method
            // for ActionBarDrawerToggle
            String customClick = getResources().getString(R.string.custom_click_event);
            Toast.makeText(this, customClick, Toast.LENGTH_LONG).show();
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<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"
    android:fitsSystemWindows="true"
    tools:context=".MainActivity">

    <!-- content -->
    <FrameLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="@string/content"/>

    </FrameLayout>

    <!-- navigation drawer -->
    <FrameLayout
        android:id="@+id/navigation_drawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        android:background="#00ff00">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="@string/navigation_item"/>

    </FrameLayout>

</android.support.v4.widget.DrawerLayout>
janzoner
  • 1,420
  • 1
  • 11
  • 19
3

It doesn't work because you have used a ActionBarDrawerToggle overload which doesn't accept a toolbar parameter. This listener only gets invoked correctly if you create a toggle like this:

drawerToggle = new ActionBarDrawerToggle(activity, drawer, toolbar, R.string.open, R.string.close);

dimsuz
  • 8,969
  • 8
  • 54
  • 88
1

This may be late but I struggled for some time finding a way to capture the onClick event for the navigation drawer icon. This is what I found:

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        toolbar.setTitle("Asset Locator");
        setSupportActionBar(toolbar);

        drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.addDrawerListener(toggle);
        toggle.syncState();

        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

              // You have to manually toggle drawer and set icon here
              if (drawer.isDrawerOpen(GravityCompat.START))
                  drawer.closeDrawer(GravityCompat.START);
              else
                  drawer.openDrawer((int) Gravity.START);
            }
        });