Skip to content

Commit 9debfa0

Browse files
committed
update: countdown timer
1 parent 930cab4 commit 9debfa0

File tree

1 file changed

+19
-4
lines changed
  • app/src/main/java/com/qomunal/opensource/androidresearch/ui/main

1 file changed

+19
-4
lines changed

app/src/main/java/com/qomunal/opensource/androidresearch/ui/main/MainActivity.kt

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
package com.qomunal.opensource.androidresearch.ui.main
22

33
import android.os.Bundle
4+
import android.os.CountDownTimer
45
import androidx.activity.viewModels
56
import com.qomunal.opensource.androidresearch.common.base.BaseActivity
6-
import com.qomunal.opensource.androidresearch.common.ext.showToast
77
import com.qomunal.opensource.androidresearch.databinding.ActivityMainBinding
8+
import java.text.DecimalFormat
9+
import java.text.NumberFormat
10+
811

912
class MainActivity : BaseActivity<ActivityMainBinding>() {
1013

@@ -23,9 +26,21 @@ class MainActivity : BaseActivity<ActivityMainBinding>() {
2326

2427
override fun initUI() {
2528
binding.apply {
26-
btnTest.setOnClickListener {
27-
showToast("Yes u click on me")
28-
}
29+
object : CountDownTimer(50000, 1000) {
30+
override fun onTick(millisUntilFinished: Long) {
31+
// Used for formatting digits to be in 2 digits only
32+
val f: NumberFormat = DecimalFormat("00")
33+
val hour = (millisUntilFinished / 3600000) % 24
34+
val min = (millisUntilFinished / 60000) % 60
35+
val sec = (millisUntilFinished / 1000) % 60
36+
btnTest.text = f.format(hour) + ":" + f.format(min) + ":" + f.format(sec)
37+
}
38+
39+
override fun onFinish() {
40+
// When the task is over it will print 00:00:00
41+
btnTest.text = "00:00:00"
42+
}
43+
}.start()
2944
}
3045
}
3146

0 commit comments

Comments
 (0)