Skip to content

Instantly share code, notes, and snippets.

@Alynva
Created September 19, 2018 17:15
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Alynva/565828f91ba1c28a2c58f33941cdba9b to your computer and use it in GitHub Desktop.
Save Alynva/565828f91ba1c28a2c58f33941cdba9b to your computer and use it in GitHub Desktop.
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
Copy link

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

@Alynva
Copy link
Author

Alynva commented Jul 4, 2019

Hi. Sure, what you need?

@DanielaCisneros
Copy link

DanielaCisneros commented Jul 5, 2019 via email

@Alynva
Copy link
Author

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