Skip to content

Instantly share code, notes, and snippets.

@ricardoalcocer
Created May 13, 2020 04:36
Show Gist options
  • Save ricardoalcocer/46e8c8983ad8507f64c4c670ec6721d2 to your computer and use it in GitHub Desktop.
Save ricardoalcocer/46e8c8983ad8507f64c4c670ec6721d2 to your computer and use it in GitHub Desktop.
Kotlin Android Countup
private fun stopCountUp(){
//txtTimer.text = duration.toString()
val t = Toast.makeText(this,"Stopping countup",Toast.LENGTH_SHORT).show()
timer2.cancel()
timer2IsRunning=false
}
private fun startCountUp(){
timer2 = object : CountUpTimer(duration.toLong()) {
override fun onTick(second: Int) {
txtTimer2.text = second.toString()
}
override fun onFinish() {
txtTimer2.text = (duration/1000).toString()
timer2IsRunning=false
}
}
timer2.start()
timer2IsRunning=true
}
import android.os.CountDownTimer
abstract class CountUpTimer protected constructor(private val duration: Long) :
CountDownTimer(duration, INTERVAL_MS) {
abstract fun onTick(second: Int)
override fun onTick(msUntilFinished: Long) {
val second = ((duration - msUntilFinished) / 1000).toInt()
onTick(second)
}
override fun onFinish() {
onTick(duration / 1000)
}
companion object {
private const val INTERVAL_MS: Long = 1000
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment