Home » Android » android – How to cancel an animation, but not to end it?

android – How to cancel an animation, but not to end it?

Posted by: admin June 15, 2020 Leave a comment


I have a TranslateAnimation on my class. The animation starts automatically.

I set button that if it is clicked, the animation will be canceled (animation.cancel();).

I also set an AnimationListener for my class. If my animation ends, I will start a new Activity (you go to the menu).

public void onAnimationEnd(Animation animation) {
    startActivity(new Intent(Class.this, Class2.class));

My app is relying on that the user has to click the button before the animation ends.
The problem is that animation.cancel(); is admitted as the end of the animation.

How to cancel the animation in the other way that was not counted as the end of the animation? Is that possible?

Thanks in advance!

How to&Answers:

animation.cancel() is calling the animation listener as API documentation describes:

Cancelling an animation invokes the animation listener, if set, to
notify the end of the animation. If you cancel an animation manually,
you must call reset() before starting the animation again.

If you want different behaviour on cancel() and onAnimationEnd() i would suggest a boolean variable, which can be set on button click, and onanimationend checks whether it is true.


Once the animation is cancelled, you can remove the listener thus preventing onAnimationEnd from being called:

public void onAnimationCancel(Animator animation) {