21

I'm trying to animate buttons with fade in animation using AnimatorSet

Button fades in > Click button > Remaining buttons fade out

So in order to do this, I want to set the onClickListner after the animation is completed, but that doesn't seem to work. Clicking a button in the middle of the animation triggers the onClick action:

setQuestion = new AnimatorSet();           
setQuestion.playSequentially(fadeinAnimationQ,fadeinAnimation1,fadeinAnimation2,fadeinAnimation3,fadeinAnimation4,fadeinAnimation5);
setQuestion.start();

This is the method that checks if the animation has finished.

private void checkAnimation() {
    while (true) {
        // Check if animation has ended
        if (setQuestion.isRunning() == false) {
            assignListners();
            break;
        }
    }
}
Carlos Robles
  • 10,424
  • 3
  • 35
  • 53
Zen
  • 2,524
  • 3
  • 19
  • 40
  • Here is how you can add an AnimatorListner: http://stackoverflow.com/questions/7274001/how-do-i-do-something-when-an-animation-finishes – Zen Feb 18 '14 at 12:55

3 Answers3

48

You can set an AnimatorListener on fadeinAnimation5. This will give you an onAnimationEnd callback.

fadeinAnimation5.addListener(new AnimatorListener() {

            @Override
            public void onAnimationStart(Animator animation) {
                // ...
            }

            @Override
            public void onAnimationRepeat(Animator animation) {
                // ...
            }

            @Override
            public void onAnimationEnd(Animator animation) {
                // ...
            }

            @Override
            public void onAnimationCancel(Animator animation) {
                // ...
            }
        });

Or, as suggested by slott use an AnimatorListenerAdapter

fadeinAnimation5.addListener(new AnimatorListenerAdapter() {

    @Override
    public void onAnimationEnd(Animator animation) {
        // ...
    }
}
Community
  • 1
  • 1
Rab Ross
  • 2,050
  • 21
  • 27
  • 8
    If you do not need all the events you could go for AnimatorListenerAdapter which keeps things a bit more simple. – slott Sep 18 '16 at 09:53
  • 1
    A note of warning I fell for. The onAnimationEnd callback is called both if the animation finished OR was cancelled. To get notified only when the animation finished, use withEndAction(Runnable ) instead. – Henrik Gyllensvärd Jul 18 '17 at 12:04
2

I was having a similar problem and here is how I solved it:

private void crossFadeAnimation(final View fadeInTarget, final View fadeOutTarget, long duration){
    AnimatorSet mAnimationSet = new AnimatorSet();
    ObjectAnimator fadeOut = ObjectAnimator.ofFloat(fadeOutTarget, View.ALPHA,  1f, 0f);
    fadeOut.addListener(new Animator.AnimatorListener() {
        @Override
        public void onAnimationStart(Animator animation) {
        }

        @Override
        public void onAnimationEnd(Animator animation) {
            fadeOutTarget.setVisibility(View.GONE);
        }

        @Override
        public void onAnimationCancel(Animator animation) {
        }

        @Override
        public void onAnimationRepeat(Animator animation) {
        }
    });
    fadeOut.setInterpolator(new LinearInterpolator());

    ObjectAnimator fadeIn = ObjectAnimator.ofFloat(fadeInTarget, View.ALPHA, 0f, 1f);
    fadeIn.addListener(new Animator.AnimatorListener() {
        @Override
        public void onAnimationStart(Animator animation) {
            fadeInTarget.setVisibility(View.VISIBLE);
        }

        @Override
        public void onAnimationEnd(Animator animation) {}

        @Override
        public void onAnimationCancel(Animator animation) {}

        @Override
        public void onAnimationRepeat(Animator animation) {}
    });
    fadeIn.setInterpolator(new LinearInterpolator());
    mAnimationSet.setDuration(duration);
    mAnimationSet.playTogether(fadeOut, fadeIn);
    mAnimationSet.start();
}
1

You can actually set a listener to the AnimatorSet directly since AnimatorSet inherits from Animator. Here's some code:

import android.animation.Animator;

        AnimatorSet setQuestion = new AnimatorSet();
        setQuestion.playSequentially(fadeinAnimationQ,fadeinAnimation1,fadeinAnimation2,fadeinAnimation3,fadeinAnimation4,fadeinAnimation5);
        setQuestion.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animator) {

            }

            @Override
            public void onAnimationEnd(Animator animator) {
            // !! turn on your onClickListener here !!
            }

            @Override
            public void onAnimationCancel(Animator animator) {

            }

            @Override
            public void onAnimationRepeat(Animator animator) {

            }
        });
        setQuestion.start();
SMBiggs
  • 9,090
  • 4
  • 54
  • 69