0

I'm creating a drawer with my own toolbar using setSupportActionBar. Previously I was using

new ActionBarDrawerToggle(this, drawer, toolbar, "open", "close");

but because it was implemented with my own toolbar, my onOptionsItemSelected() method wasn't firing, so using Andrei Lupsa's answer here I changed to the following constructor from the docs and the method began working again.

new ActionBarDrawerToggle(this, drawer, "open", "close");

Unfortunately, now the drawer listener is not working anymore. It displays the hamburger icon on the bar, but doesn't open when pressed. Here's the implementation:

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

getSupportActionBar().setDisplayShowTitleEnabled(false);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);

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

My AndroidManifest uses the following style:

<style name="AppTheme.NoActionBar">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>

    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
</style>

But I've also tried

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

because I thought it had to do with the ActionBarDrawerToggle no longer recognizing the toolbar, but if that were the case, then why does the hamburger icon still display?

Edit Adding onOptionsItemSelected()

@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        switch(id) {
            case R.id.action_settings:
                break;
            case android.R.id.home:
                getSupportFragmentManager().popBackStack();
                break;
        }

        return super.onOptionsItemSelected(item);
    }

android.R.id.home was created to only show when BackStack > 0, so when at the base activity, the drawer would show, and when entering the next fragment, the drawer would switch to the home arrow and be able to bring the user back to the base.

Community
  • 1
  • 1
Ben
  • 757
  • 1
  • 7
  • 15
  • post your `onOptionsItemSelected()` code – rafsanahmad007 Mar 29 '17 at 19:44
  • Are you sure something isn't covering the `Toolbar`, blocking the clicks on the toggle? – Mike M. Mar 29 '17 at 21:34
  • I think you must be right because I just found out that I can swipe the drawer into view, but I have no idea what could cover it or cause the button not to work. – Ben Mar 30 '17 at 00:27
  • Check your layout. In `ViewGroup`s like `RelativeLayout` and `FrameLayout`, the child `View`s can overlap, so if you have something listed after the `Toolbar` that fills their parent in such a `ViewGroup`, it would cover it. Also check that your `FragmentTransaction`s aren't transacting `Fragment`s into the `Toolbar`'s parent. That would have the same effect. – Mike M. Mar 30 '17 at 02:17
  • Nope, everything is placed within a `FrameLayout` below the toolbar. I also wasn't having issues clicking it with `ActionBarDrawerToggle(this, drawer, toolbar, "open", "close")`, so I don't think it has to do with overlapping. Note that I can also interact with the three dots for the menu on the right-side just fine. – Ben Mar 30 '17 at 02:51
  • 1
    You realize you have to call `drawerToggle.onOptionsItemSelected(item)` in the `Activity`'s `onOptionsItemSelected()` method to make the drawer open/close, yeah? I assumed you knew that, since you seem to want `onOptionsItemSelected()` to fire, but I guess you don't actually have it in your snippet. If you don't really need that method to fire for the toggle, you can just go back to the toggle constructor that takes your `Toolbar` as an argument. `onOptionsItemSelected()` will still run for the options menu items. – Mike M. Mar 30 '17 at 03:02
  • That was absolutely the problem! I didn't call `return drawerToggle.onOptionsItemSelected(item)` in the select method. Thanks a lot! If you put that as a solution I'll mark it as correct. – Ben Mar 30 '17 at 03:11
  • 1
    Ah, cool. Sorry 'bout that. I shoulda realized that earlier. Anyhoo, I'll just close this as a duplicate, since I've answered this issue here before. Thanks, though. Glad you got it working. Cheers! – Mike M. Mar 30 '17 at 03:13

0 Answers0