Animation bar icon programmatically
I have an activity that extends AppCompatActivity
I install Toolbar
like this:
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 icon Toolbar
when I open or close NavigationView
I do 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 it 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 NavigationView
, how can I programmatically animate the arrow icon and return to the hamburger icon.
Something like a gmail app. If we are in the list of letters and click one email, it animates the arrow and shows the contents of the letter, and if we click on the arrow, it animates the hamburger and shows the list of letters.
source to share
Just for people stumbling on this question, I managed to solve this issue:
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();
Obtained solution from here
source to share