Created
May 13, 2020 04:32
-
-
Save ricardoalcocer/915f882ad107d3110a4afa4b7948dc57 to your computer and use it in GitHub Desktop.
Kotlin Android Timer
This file contains hidden or 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
btnStartInfiniteTimer.setOnClickListener{ | |
if (!isInfiniteTimerRunning){ | |
infiniteTimer=Timer() | |
isInfiniteTimerRunning = true | |
infiniteTimer.scheduleAtFixedRate(object : TimerTask() { | |
override fun run() { | |
infiniteTimerTick() | |
} | |
}, 0, 1000) | |
} | |
} | |
btnStopInfiniteTimer.setOnClickListener { | |
if (infiniteTimer != null) { | |
infiniteTimer.cancel() | |
infiniteTimer.purge() | |
infiniteTimerCounter=0 | |
isInfiniteTimerRunning = false | |
} | |
} | |
private fun infiniteTimerTick(){ | |
runOnUiThread { | |
txtInfiniteTimer.text = infiniteTimerCounter.toString() | |
} | |
infiniteTimerCounter ++ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment