Last active
September 1, 2019 16:13
-
-
Save Lzyct/b3d5f6b5e3993a720b8dcf5007718802 to your computer and use it in GitHub Desktop.
Sample create countdown timer
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
| /** | |
| * This sample count down timer | |
| * for auction app when bid start and end | |
| */ | |
| private lateinit var countDownTimer: CountDownTimer | |
| private var difference = 0L | |
| private var isStart = false | |
| private var startTime = "" // you can chage it with Date | |
| private var endTime = "" | |
| private fun yourMethodHere(){ | |
| //using 24 Hours Format | |
| startTime = "14:00" | |
| endTime = "16:00" | |
| //set count down timer | |
| checkCountDown() | |
| countDownTimer = object : CountDownTimer(difference, 500) { | |
| override fun onFinish() { | |
| /** | |
| * If isStart true countdown start again to check | |
| * when auction time ended | |
| * | |
| */ | |
| if (isStart) { | |
| checkCountDown() | |
| if (::countDownTimer.isInitialized) countDownTimer.start() // restart countdown | |
| } else { | |
| //set count down time text end here | |
| } | |
| } | |
| override fun onTick(millisUntilFinished: Long) { | |
| val second = millisUntilFinished / 1000 | |
| /** | |
| * Sample with day | |
| * String.format("%02d Day %02d:%02d:%02d", | |
| second / (3600 * 24), // Day | |
| second / 3600 % 24, // Hour | |
| (second % 3600) / 60, // Minute | |
| (second % 60)) // Second | |
| */ | |
| bind.tvCountDownAuction.text = String.format("%02d:%02d:%02d", | |
| second / 3600 % 24, | |
| (second % 3600) / 60, | |
| (second % 60)) | |
| } | |
| } | |
| countDownTimer.start() | |
| } | |
| /** | |
| * This method to check is when bid start or bid end | |
| */ | |
| @SuppressLint("SimpleDateFormat") | |
| private fun checkCountDown() { | |
| //Change date format here for example using 24 Hours format | |
| val simpleDateFormat = SimpleDateFormat("HH:mm") | |
| val currTime = simpleDateFormat.parse(simpleDateFormat.format(Date())) | |
| val startTime = simpleDateFormat.parse(startTime) | |
| val endTime = simpleDateFormat.parse(endTime) | |
| if (currTime >= startTime) { | |
| //set false to because is end of auction time | |
| isStart = false | |
| //set title countdown auction end timer here | |
| //update different to calculate start time to end time | |
| difference = endTime.time - currTime.time | |
| } else if (endTime >= currTime) { | |
| //set true to because is start of auction time | |
| isStart = true | |
| //set title countdown auction end timer here | |
| //update different to calculate start time to end time | |
| difference = startTime.time - currTime.time | |
| } | |
| } | |
| override fun onDestroy() { | |
| super.onDestroy() | |
| countDownTimer.cancel() // cancel countdown on destory or you can put it on onPause | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment