Created
September 19, 2018 17:15
-
-
Save Alynva/565828f91ba1c28a2c58f33941cdba9b to your computer and use it in GitHub Desktop.
setInterval in Kotlin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
timeLabel
is just an TexView andcountProgress
is an ProgressBar, but it can be replaced by any other counter variable. For thesetInterval
, theif
block doesn't matter, just replace it with the code that you want to be runned every N milliseconds (in this case, 1000)