Created
May 13, 2020 04:36
-
-
Save ricardoalcocer/46e8c8983ad8507f64c4c670ec6721d2 to your computer and use it in GitHub Desktop.
Kotlin Android Countup
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
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 | |
} |
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
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