1 min read

Android view animations issue when hiding the animated views

Today I encountered another really interesting issue (or probably a bug?!). This time it is related to views and animating them.

If you animate a view like this:

[view].animate().scaleX(0f).scaleY(0f).setDuration(180).setListener(object:Animator.AnimatorListener {
    override fun onAnimationRepeat(p0: Animator?) {}
    override fun onAnimationCancel(p0: Animator?) {}
    override fun onAnimationStart(p0: Animator?) {}
    override fun onAnimationEnd(p0: Animator?) {
        // Anything you do after the animation is ended

... and then hide the view: [view].visibility = View.GONE

Everything works fine until the next time you set the view's visibility to View.VISIBLE and animate the scale parameters:

[view].visibility = View.VISIBLE

There're problems showing/hiding views like the way mentioned above.

The solution:

After the animation is completed - remove the listeners!!!