How do I change a TextView based on time in Kotlin?

I want to change what my textview displays every 10 seconds. I have an array list that the textview will choose from and I want it to change in the UI every 10 seconds. I do know how to initiate this command with a button and an onclick listener, however I need to do it automatically without any user input. Any way to do this in Kotlin?

Thanks Umar


You should use a Handler and a Runnable.
Declare two global variables in your activity:

private var handler = Handler()
private var updateTextViewTask = object : Runnable {
    override fun run() {
         // Update your text view UI here
         textView.text = "New text"

         handler.postDelayed(this, 10 * 1000)

Then, to start and stop the routine just call in your code

Leave a Reply

Your email address will not be published. Required fields are marked *