Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
setInterval in Kotlin
val handler = Handler(Looper.getMainLooper())
val runnable = object : Runnable {
override fun run() {
if (countProgress.progress > 0) {
val new_time = countProgress.progress.toInt() - 1
timeLabel.text = "$new_time segundos"
countProgress.progress = new_time
}
handler.postDelayed(this, 1000)
}
}
playCount.setOnClickListener() {
handler.postDelayed(runnable, 1000)
}
pauseCount.setOnClickListener() {
handler.removeCallbacks(runnable)
}
@DanielaCisneros

This comment has been minimized.

Copy link

@DanielaCisneros DanielaCisneros commented Jul 4, 2019

Hello, I need a litlle help this code. Please could you answer me.

@Alynva

This comment has been minimized.

Copy link
Owner Author

@Alynva Alynva commented Jul 4, 2019

Hi. Sure, what you need?

@DanielaCisneros

This comment has been minimized.

Copy link

@DanielaCisneros DanielaCisneros commented Jul 5, 2019

@Alynva

This comment has been minimized.

Copy link
Owner Author

@Alynva Alynva commented Jul 5, 2019

timeLabel is just an TexView and countProgress is an ProgressBar, but it can be replaced by any other counter variable. For the setInterval, the if block doesn't matter, just replace it with the code that you want to be runned every N milliseconds (in this case, 1000)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment