Check if AnimatorSet has finished animation?

Zen picture Zen · Feb 18, 2014 · Viewed 19.6k times · Source

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;
        }
    }
}

Answer

Rab Ross picture Rab Ross · Feb 18, 2014

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) {
        // ...
    }
}