Android Animation Listener

I think you need this.

fadeInAnimation.setAnimationListener(new Animation.AnimationListener() {
    @Override
    public void onAnimationStart(Animation animation) {

    }

    @Override
    public void onAnimationEnd(Animation animation) {

    }

    @Override
    public void onAnimationRepeat(Animation animation) {

    }
});

Using Kotlin

        //OR using Code
        val rotateAnimation = RotateAnimation(
                0f, 359f,
                Animation.RELATIVE_TO_SELF, 0.5f,
                Animation.RELATIVE_TO_SELF, 0.5f

        )
        rotateAnimation.duration = 300
        rotateAnimation.repeatCount = 2

        //Either way you can add Listener like this
        rotateAnimation.setAnimationListener(object : Animation.AnimationListener {

            override fun onAnimationStart(animation: Animation?) {
            }

            override fun onAnimationRepeat(animation: Animation?) {
            }

            override fun onAnimationEnd(animation: Animation?) {

                val rand = Random()
                val ballHit = rand.nextInt(50) + 1
                Toast.makeText(context, "ballHit : " + ballHit, Toast.LENGTH_SHORT).show()
            }
        })

        ivBall.startAnimation(rotateAnimation)

In case someone needs the solution in kotlin:

fadeInAnimation.setAnimationListener(object: Animation.AnimationListener {
        override fun onAnimationRepeat(animation: Animation?) {
            TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
        }

        override fun onAnimationEnd(animation: Animation?) {
            TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
        }

        override fun onAnimationStart(animation: Animation?) {
            TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
        }

    })

If you only need an end-action it would suffice to use .withEndAction(Runnable)

fadeInAnimation.withEndAction(new Runnable() {
    @Override
    public void run() {
        ... do stuff
    }
})

Tags:

Android