androidkotlinprogress-barobjectanimator

How to update textview value based on progressbar progress in Kotlin


I would like to use ObjectAnimator to animate by ProgressBar progress. However, I wouldn't be able to update the text since I am not using a for loop. What is the best way to update the Textview while using ObjectAnimator? Here is what I've tried to update it but I failed to do so:

val animation = ObjectAnimator.ofInt(progressbar, "progress", 0, 10000)
   animation.setDuration(2000)
   animation.setInterpolator(DecelerateInterpolator())
   animation.start()
   var progress=progressbar.getProgress() /100
   progtxt.text="$progress%"

Solution

  • With ObjectAnimator you can use an update listener:

        animation.addUpdateListener(object: ValueAnimator.AnimatorUpdateListener {
            override fun onAnimationUpdate(animation: ValueAnimator?) {
                val progress = animation?.animatedValue as Int
                // Update you text view
            }
        })
    

    Here in onAnimationUpdate you can get animated value or progress bar progress depending on what you need and set it to your text view.