3

I have an activity that extends AppCompatActivity

I'm setting the Toolbar this way:

    mToolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(mToolbar);
    actionBar = getSupportActionBar();
    assert actionBar != null;
    actionBar.setHomeAsUpIndicator(R.drawable.ic_menu);
    actionBar.setDisplayHomeAsUpEnabled(true);
    actionBar.setDisplayShowHomeEnabled(true);

To animate the Toolbar icon when I open or close the NavigationView I'm doing this:

    actionBarDrawerToggle =
            new ActionBarDrawerToggle(this, mNavigationDrawer, R.string.open_drawer,
                    R.string.close_drawer);
    mNavigationDrawer.setDrawerListener(actionBarDrawerToggle);

    actionBarDrawerToggle.syncState();

I can see that when the drawer opens the icon animates to the back arrow and when it closes, it animates from the arrow to the hamburger icon.

I want to know, without opening or closing the NavigationView, how can I programmatically animate the icon to the arrow and back to the hamburger icon.

Something like the gmail app. If we are in the list of emails and press one email, it animates to the arrow and shows the content of the email and if we press the arrow, it animates to the hamburger and shows the list of emails.

Favolas
  • 6,963
  • 29
  • 75
  • 127

1 Answers1

0

Just for people stumbling in this question, I've managed to solve this issue doing this:

ValueAnimator anim = ValueAnimator.ofFloat(start, end);
anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
    @Override
    public void onAnimationUpdate(ValueAnimator valueAnimator) {
        float slideOffset = (Float) valueAnimator.getAnimatedValue();
        toolbarDrawerToggle.onDrawerSlide(drawerLayout, slideOffset);
    }
});
anim.setInterpolator(new DecelerateInterpolator());
// You can change this duration to more closely match that of the default animation.
anim.setDuration(500);
anim.start();

Retrieved the solution from here

Community
  • 1
  • 1
Favolas
  • 6,963
  • 29
  • 75
  • 127