2

in my app i need to change the image of a toolbar button programmatically, and for so I´m using:

public boolean onCreateOptionsMenu( Menu menu ) {
        getMenuInflater().inflate(R.menu.main_activity_menu, menu);
            toolbarButton = menu.findItem(R.id.action_button);
            return true;
        }

toolbarButton.setIcon(R.drawable.ic_settings);

And it works perfectly, the problem is that it happens too fast, so i´d need to add an animation to the transition, but I don´t know how to add an animation to a toolbar button. Could somebody help me please? Thanks a lot!

Javierd98
  • 708
  • 9
  • 27

2 Answers2

3

Okay, so finally thanks to Victorldavila i managed to get it working, this is my final code:

View button = toolbar.findViewById(R.id.action_button);

//Remove icon animation
if (button != null) {
   Animation animation = AnimationUtils.loadAnimation(getBaseContext(), R.anim.fragment_fade_out);
   animation.setStartOffset(0);
   button.startAnimation(animation);
}

//Add icon animation
if (button != null) {
   Animation animation = AnimationUtils.loadAnimation(getBaseContext(), R.anim.fragment_slide_in_up);
   animation.setStartOffset(0);
   button.startAnimation(animation);
}
Tom11
  • 2,419
  • 8
  • 30
  • 56
Javierd98
  • 708
  • 9
  • 27
1

Maybe these answers can help you:

https://stackoverflow.com/a/39182291/2759449

https://stackoverflow.com/a/30787535/2759449

Community
  • 1
  • 1
victorldavila
  • 301
  • 2
  • 13