16

The title says it all. When I call the mDrawerToggle.setDrawerIndicatorEnabled(false) I don't want the "hamburger" icon to be shown anymore but the backwards navigation arrow.

Unfortunately when I call this method just the title is shown without the backwards arrow nor the "hamburger" icon. After setting the drawerIndicatorEnabled to be true again it shows the "hamburger" icon again.

I set getSupportActionBar().setDisplayHomeAsUpEnabled(true) and getSupportActionBar().setDisplayShowHomeEnabled(true)

Edit: Basically the solution suggested here: Change drawer icon back to back arrow somehow doesn't give me the back arrow.

Does anyone know a solution for this issue? Thank you very much!

Community
  • 1
  • 1
  • I think there's no way. Easiest workaround would be to `setDrawerIndicatorEnabled` to `true` when the drawer slides in, and `false` when it gets closed. (Assuming you are wishing to use the back-arrow to close the drawer) – natario Jan 02 '15 at 12:38
  • But is it normal that it's not possible to enable the backwards navigation arrow anymore after disabling the drawerIndicator (or setting up the listener?)? It worked before when I didnt had any ActionBarDrawerToggle listener and set setHomeAsUpEnabled(true) ... – Marcel_marcel1991 Jan 02 '15 at 12:47
  • Nice thing of a ActionBarDrawerToggle is that it manages icons itself depending on the drawer state. I think it's normal. – natario Jan 02 '15 at 12:49
  • But your workaround still wouldn't work for me. My problem is that I always have the hamburger icon on mDrawerToggle.setDrawerIndicatorEnabled(true) and no icon but just the title on mDrawerToggle.setDrawerIndicatorEnabled(false). How am I suppose to get the backwards navigation arrow then with your solution? – Marcel_marcel1991 Jan 02 '15 at 13:01
  • It wasn't clear before, sorry. – natario Jan 02 '15 at 13:20
  • No I'm sorry my bad :) – Marcel_marcel1991 Jan 02 '15 at 13:24
  • Check out [this answer](https://stackoverflow.com/a/54992406/1223728) – Borzh Mar 04 '19 at 22:49

4 Answers4

38

After hours of trials and errors I came up with a solution that allows to switch from "hamburger" to "arrow" and back. This is very weird and unnatural, don't ask me why it works in this way, but it works. Furthermore, this is the only solution that allowed me to do this, nothing else worked.

I have only one activity with fragments. When I'm switching from one fragment to another, I'm setting boolean variable in my activity displayingInnerFragment. For those fragments, where displayingInnerFragment == true, I show "arrow" in the top left corner, and for all others I show "hamburger". The following code I execute before switching to any fragment:

    ActionBar actionBar = getSupportActionBar();
    if (displayingInnerFragment) {
        actionBar.setDisplayHomeAsUpEnabled(false);
        drawerToggle.setDrawerIndicatorEnabled(false);
        actionBar.setDisplayHomeAsUpEnabled(true);
    } else {
        drawerToggle.setDrawerIndicatorEnabled(true);
    }

Note the double call to actionBar.setDisplayHomeAsUpEnabled() in one branch. This is required for drawerToggle.setDrawerIndicatorEnabled(false) to work. Otherwise it will not work properly. All other options either don't show "arrow" or hide "arrow" or "hamburger" at one moment or another.

afrish
  • 3,167
  • 4
  • 30
  • 38
  • This is weird. What the heck is going on here? – Jeff T Jul 18 '15 at 23:01
  • 1
    Ok. How on earth does this work? And does it work with all Android versions as in it would be safe to publish an app with this solution?... and thanks for saving me hours of tireless work – Dylan Vander Berg Aug 15 '15 at 18:17
  • @user2297366 I have not tested this on different Android versions, but all this stuff is from support lib. And if we use one version of support lib, then I assume it working in the same way on any Android version. At the end of the day it is just a JAR with classes added to your project. – afrish Aug 17 '15 at 16:02
  • amazing. It worked like a charm. Seems like you T&E was really helpful. Appreciated – gaurav414u Aug 22 '15 at 14:23
  • 1
    Holy mother of god. It worked! Thank you so much. There is no way I could've found it out without your answer – Bolein95 Dec 20 '15 at 20:36
  • 6
    It is nice to see the back arrow,but it doesn't catch the click,how to go back to backstack with this displayed back arrow? – Hanry Feb 06 '16 at 18:24
  • Genius... this actually works! There really needs to be an official way to toggle between the hamburger menu and back arrow in the API. The other way of doing it was manually setting the back arrow icon, but AppCompat 23.3.0 broke tinting of said icon when set externally programmatically: https://code.google.com/p/android/issues/detail?id=207612 – Ray W Apr 21 '16 at 13:46
  • 1
    @hanry don't use ActionBarDrawerToggle with the constructor that contains a Toolbar! – Langusten Gustel Oct 21 '16 at 20:13
  • I also put `drawerToggle.syncState();` after this code, and hamburger appeared when it should (sometimes an arrow appeared instead of hamburger). – CoolMind Nov 01 '16 at 19:04
  • Still this "non explainable solution" works like a charm. I tried like 10 solutions and none 100% working as this one. – vinicius gati Apr 29 '19 at 17:29
7

Use like this:

mDrawerToggle.setHomeAsUpIndicator(R.drawable.abc_ic_ab_back_mtrl_am_alpha);
mDrawerToggle.setDrawerIndicatorEnabled(false);
mDrawerToggle.syncState();
ale
  • 6,369
  • 7
  • 55
  • 65
  • 3
    Don't know why answer is down voted. For me adding row with mDrawerToggle.syncState() was a solution. – sunnyday Dec 17 '15 at 06:41
  • This should be marked as the right answer, since it's simple and doesn't contain any hacks. – Rajath Aug 12 '16 at 05:38
  • Thanks for a syncState(), but why should we use R.drawable.abc_ic_ab_back_mtrl_am_alpha, it changes every version of API and why is it here? – CoolMind Nov 01 '16 at 19:15
4

This one stumped me for a while, as I knew there was a default up icon being used (from looking at the source of ActionBarDrawerToggle), and I wanted it to adopt the color set for R.attr.colorControlNormal. Amazingly, the order of these two lines is crucial for actually showing the up arrow:

mDrawerToggle.setDrawerIndicatorEnabled(false);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);

setDrawerIndicatorEnabled must be called on your ActionBarDrawerToggle before setDisplayHomeAsUpEnabled is called on your ActionBar. Both lines must be called for the default (tinted) arrow to show.

Paul Burke
  • 25,496
  • 9
  • 66
  • 62
1

I also face the same problem (it's either the < icon or the menu icon shown, but not both) and finally found what's working for me.

Additional Info : I'm using Support library 25, so it might play a factor in here as I didn't test with previous library version. I'm using Toolbar set as action bar, and set up the ActionDrawerToggle with method that has Toolbar parameter in it. I only tested this on Android M device, though.

The code below is how I enabled/disable the navigation drawer (function is resides in NavigationDrawer Fragment).

public void setDrawerEnabled(final boolean enabled) {

    int lockMode = enabled ? DrawerLayout.LOCK_MODE_UNLOCKED :
            DrawerLayout.LOCK_MODE_LOCKED_CLOSED;

    mDrawerLayout.setDrawerLockMode(lockMode);
    mDrawerToggle.setDrawerIndicatorEnabled(enabled);

    ActionBar actionBar = null;
    if (getActivity() instanceof AppCompatActivity) {
        actionBar = ((AppCompatActivity) getActivity()).getSupportActionBar();
    }

    if (actionBar != null) {
        actionBar.setDisplayHomeAsUpEnabled(!enabled);
        actionBar.setDefaultDisplayHomeAsUpEnabled(!enabled);
        actionBar.setDisplayShowHomeEnabled(enabled);
        actionBar.setHomeButtonEnabled(enabled);
    }

    mDrawerToggle.syncState();

    mDrawerToggle.setToolbarNavigationClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            if (!mDrawerToggle.isDrawerIndicatorEnabled())
                getActivity().onBackPressed();

        }
    });
}

In my case, setting the actionBar.setHomeButtonEnabled(enabled); forces it to draw the menu icon (or else, in my case, when first fragment is resumed, there won't be any navigation icon), although it will stop rendering the < icon for other fragments. Setting the mDrawerToggle.syncState(); after changing the navigation icons remedy that for me, though. Now both icons are shown correctly!

I hope this helps someone!

margie
  • 431
  • 4
  • 7