I have a fadeout animation in a view (which is inside a fragment), and everytime the animation happens, after it finishes the view redraws itself again. I found a work around doing view.SetVisibility(View.GONE) . But it doesn't wait for the animation to finish. I would like to execute this setVisibility code only after the animation has finished. What is the best way to do that?
            Asked
            
        
        
            Active
            
        
            Viewed 8.1k times
        
    101
            
            
         
    
    
        Paulo Barros
        
- 2,740
- 8
- 28
- 36
- 
                    Post your code how you are showing the animation.... – Lalit Poptani Sep 30 '11 at 06:08
5 Answers
189
            You can add Animation listener to your animation object like
anim.setAnimationListener(new Animation.AnimationListener(){
    @Override
    public void onAnimationStart(Animation arg0) {
    }           
    @Override
    public void onAnimationRepeat(Animation arg0) {
    }           
    @Override
    public void onAnimationEnd(Animation arg0) {
    }
});
 
    
    
        blessanm86
        
- 31,439
- 14
- 68
- 79
- 
                    5If you want to prevent the fadeout animation to start again while the animation is in progress, use `if (!anim.hasStarted() || anim.hasEnded())` to detect if the animation is still running. – theczechsensation Dec 28 '14 at 06:29
48
            
            
        Functionally the same as the accepted answer but in a much more concise way:
// Add/Remove any animation parameter
theView.animate()
        .alpha(0)
        .setDuration(2000)
        .withEndAction(new Runnable() {
            @Override
            public void run() {
                theView.setVisibility(View.GONE);
            }
        });
Enjoy :)
 
    
    
        Son Truong
        
- 13,661
- 5
- 32
- 58
 
    
    
        Antzi
        
- 12,831
- 7
- 48
- 74
- 
                    4Clear and one-liner. Top answer! Can be simplyfied with the use of lambda `.withEndAction(() -> theView.setVisibility(View.GONE));` – theBugger Oct 04 '16 at 11:44
- 
                    2
- 
                    
- 
                    Important info:- The action is only run if the animation ends normally, if the ViewPropertyAnimator is canceled during that animation, the runnable will not run. – Sarthak_ssg5 Jun 07 '19 at 07:51
- 
                    1Works like a charm, also when using navigation component in the runnable. (Animation listener alwys crashed when I was navigating with the nav component.) – Luigi_Papardelle Jan 21 '20 at 15:28
- 
                    
- 
                    
10
            
            
        Simply take your animation object and add animation listener to it. Here is the example code :
rotateAnimation.setAnimationListener(new AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                // TODO Auto-generated method stub
            }
            @Override
            public void onAnimationRepeat(Animation animation) {
                // TODO Auto-generated method stub
            }
            @Override
            public void onAnimationEnd(Animation animation) {
                // TODO Auto-generated method stub
**// WRITE HERE WHATEVER YOU WANT ON THE COMPLETION OF THE ANIMATION**
            }
        });
 
    
    
        Gaurav Arora
        
- 8,282
- 21
- 88
- 143
5
            
            
        Example for Kotlin
var fadeOutImage = findViewById<ImageView>(R.id.fade_out_Image)
    val fadeOutAnimation = R.anim.fade_out_animation
    val animation = AnimationUtils.loadAnimation(this, fadeOutAnimation)
    fadeOutImage.startAnimation(animation)
    animation.setAnimationListener(object : Animation.AnimationListener {
        override fun onAnimationStart(p0: Animation?) {
//                not implemented
        }
        override fun onAnimationRepeat(p0: Animation?) {
//                not implemented
        }
        override fun onAnimationEnd(p0: Animation?) {
            fadeOutImage.visibility = View.INVISIBLE
        }
    })
 
    
    
        richc
        
- 1,648
- 5
- 20
- 48
- 
                    you might save some lines using `fade_out_Image.animate().alpha(0f).setDuration(100L).withEndAction {fade_out_Image.visibility = View.GONE}.start()` – longi Jul 18 '19 at 10:28
- 
                    Comment : it is mandatory use fadeOutImage.startAnimation(animation) and not fadeOutImage = animation – Dimitri Leurs May 12 '22 at 10:18
