I have two fragments in an activity. When fragment A is showing, I want the navigation drawer burger icon to show and the navigation drawer to work. When fragment B is showing, I want the back arrow to show and when it's clicked do an up navigation. However, I can't seem to get the new AppCompat v7 toolbar to show the up arrow at all inside my ActionBarActivity unless the nav drawer is open.
In my activity, for my onCreate() method I have...
toolbar = (Toolbar) findViewById(R.id.toolbar);
if (toolbar != null) {
setSupportActionBar(toolbar);
}
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, toolbar, R.string.drawer_open, R.string.drawer_close);
mDrawerLayout.setDrawerListener(mDrawerToggle);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
And then I call mDrawerToggle.syncState();
in my onPostCreate()
I've tried searching on how to programatically trigger the toolbar icon to the back arrow but nothing has worked. From what I've gathered, calling
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
from my fragment should change the icon but that's not the case. This may be a stupid question, but what am I doing wrong?
From what I have seen in the source code of v7
ActionBarDrawerToggle
, you can animate the icon to different states without having the drawer being opened.Morphing between Burger and Arrow depends on values between
0f
and1.0f
, basically these are values that the drawer passes to the ActionBarDrawerToggle.I used
ValueAnimator
to animate values in this range, i.e mimicking the drawer toggling.null
arguments are safe becauseActionBarDrawerToggle
does not care at all about drawer views. Make sure you take a look at new interpolators to do the animation fully-by-the-book of material design guidelines:Another approach is to access
mSlider
private field of theActionBarDrawer
through reflection and callsetPosition(float position)
method to toggle between Burger and Arrow.mSlider
is of type (extends)DrawerArrowDrawable
.Personally, I always try to avoid reflection, as long as there is no other way to do your dirty work.