I want to generate 10 milli second in Kotlin for my Rubik's cube solver app, how can i do that? I have a custom view which is a timer i want to update the timer in every 10 milli second
Asked
Active
Viewed 669 times
0
-
Does this answer your question? [How to call a function after delay in Kotlin?](https://stackoverflow.com/questions/43348623/how-to-call-a-function-after-delay-in-kotlin) – marcinj Jan 02 '20 at 16:22
-
@marcinj no iwant to generate 10 milli second time base – Aseem Salim Jan 02 '20 at 16:25
2 Answers
0
You can use the timer inline function or Timer class in java like this
val timer = timer(period = 10) {
//Do something here
}
Schedules an action to be executed periodically, starting after the specified delay (expressed in milliseconds) and with the interval of period milliseconds between the end of the previous task and the start of the next one.
remember to cancel the timer when you are done or your activity or fragment is destroyed
timer.cancel()

Mojtaba Haddadi
- 1,346
- 16
- 26

Amir Hossein Mirzaei
- 2,325
- 1
- 9
- 17
0
You need to replace outputText with the thing you want to display it on
The code for the countdown:
val ttlTime: Long = // input the amount of ms here
object : CountDownTimer(ttlTime, 10) {
override fun onTick(millisUntilFinished: Long) {
outputText.text = "time remaining: " + millisUntilFinished / 10
}
override fun onFinish() {
// code for what needs to happen after the countdown is finished
}
}.start()
If you want to display it proper:
val ttlTime: Long = // input the amount of ms here
object : CountDownTimer(ttlTime, 10) {
override fun onTick(millisUntilFinished: Long) {
val microSecond = millisUntilFinished.toString().takeLast(3)
val second = (millisUntilFinished / 1000) % 60
val minute = millisUntilFinished / (1000 * 60) % 60
var finalTime = microSecond
if (second > 0) {
finalTime = "$second $finalTime"
if (minute > 0) {
finalTime = "$minute $finalTime"
}
} else if (minute > 0) {
finalTime = "$minute 0 $finalTime"
}
outputText.text = "time remaining: $finalTime"
}
override fun onFinish() {
// code for what needs to happen after the countdown is finished
}
}.start()
source: https://developer.android.com/reference/kotlin/android/os/CountDownTimer
(this is in java but if you paste it in android studio you can convert it to kotlin)
If you want the timer to go up:
val startTiming = Calendar.getInstance()
Handler().post(object : Runnable {
override fun run() {
Handler().postDelayed(this, 10)
updateTime(startTiming)
}
})
fun updateTime(startTime: Calendar) {
val currentTime = Calendar.getInstance()
val diff = currentTime.timeInMillis - startTime.timeInMillis
val microSecond = diff.toString().takeLast(3)
val second = (diff / 1000) % 60
val minute = diff / (1000 * 60) % 60
var finalTime = microSecond
if (second > 0) {
finalTime = "$second $finalTime"
if (minute > 0) {
finalTime = "$minute $finalTime"
}
}
else if (minute > 0) {
finalTime = "$minute 0 $finalTime"
}
outputText.text = finalTime
}

Renza Polza
- 87
- 1
- 8