Need to create stopwatch in android, for now I'v got somethink like this, simple function to start counting and display time, but i dont know what am I doing wrong with postdelayed:
fun runTimer() {
val timeView = findViewById<TextView>(R.id.time_view)
val handler = Handler()
handler.post(Runnable {
var hours = seconds / 3600
var minutes = (seconds % 3600) / 60
var secs = seconds % 60
var time = String.format("%d:%02d:%02d", hours, minutes, secs)
timeView.setText(time)
if (running) {
seconds++
}
handler.postDelayed(this, 1000)
})
}
what exactly should i put instead of this? (need to be runnable type)
I believe in your use case the most suitable solution would be to use a Chronometer - you can read more about it here. Also if you want to watch a video tutorial you can check this video. Hope this helps. Additionally if you want to have the functionality to be able to set a specific time frame and countdown using a CountDownTimer is a good option - documentation