Stay organized with collections
Save and categorize content based on your preferences.
CountDownTimer
abstract class CountDownTimer
Schedule a countdown until a time in the future, with regular notifications on intervals along the way. Example of showing a 30 second countdown in a text field:
Kotlin
object : CountDownTimer(30000, 1000) {
override fun onTick(millisUntilFinished: Long) {
mTextField.setText("seconds remaining: " + millisUntilFinished / 1000)
}
override fun onFinish() {
mTextField.setText("done!")
}
}.start()
Java
new CountDownTimer(30000, 1000) {
public void onTick(long millisUntilFinished) {
mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
}
public void onFinish() {
mTextField.setText("done!");
}
}.start();
The calls to
onTick(long)
are synchronized to this object so that one call to
onTick(long)
won't ever occur before the previous callback is complete. This is only relevant when the implementation of
onTick(long)
takes an amount of time to execute that is significant compared to the countdown interval.
Summary
Public methods |
Unit |
Cancel the countdown.
|
abstract Unit |
Callback fired when the time is up.
|
abstract Unit |
Callback fired on regular interval.
|
CountDownTimer! |
Start the countdown.
|
Public constructors
CountDownTimer
CountDownTimer(
millisInFuture: Long,
countDownInterval: Long)
Parameters |
millisInFuture |
Long: The number of millis in the future from the call to start() until the countdown is done and onFinish() is called. |
countDownInterval |
Long: The interval along the way to receive onTick(long) callbacks. |
Public methods
cancel
fun cancel(): Unit
Cancel the countdown.
onFinish
abstract fun onFinish(): Unit
Callback fired when the time is up.
onTick
abstract fun onTick(millisUntilFinished: Long): Unit
Callback fired on regular interval.
Parameters |
millisUntilFinished |
Long: The amount of time until finished. |
Content and code samples on this page are subject to the licenses described in the Content License. Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
Last updated 2025-02-10 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-02-10 UTC."],[],[],null,["# CountDownTimer\n\nAdded in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)\n\nCountDownTimer\n==============\n\n*** ** * ** ***\n\nKotlin \\|[Java](/reference/android/os/CountDownTimer \"View this page in Java\") \n\n```\nabstract class CountDownTimer\n```\n\n|---|--------------------------------|\n| [kotlin.Any](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-any/index.html) ||\n| ↳ | [android.os.CountDownTimer](#) |\n\nSchedule a countdown until a time in the future, with regular notifications on intervals along the way. Example of showing a 30 second countdown in a text field:\n\n\n### Kotlin\n\n```kotlin\nobject : CountDownTimer(30000, 1000) {\n \n override fun onTick(millisUntilFinished: Long) {\n mTextField.setText(\"seconds remaining: \" + millisUntilFinished / 1000)\n }\n \n override fun onFinish() {\n mTextField.setText(\"done!\")\n }\n }.start()\n \n```\n\n### Java\n\n```kotlin\nnew CountDownTimer(30000, 1000) {\n \n public void onTick(long millisUntilFinished) {\n mTextField.setText(\"seconds remaining: \" + millisUntilFinished / 1000);\n }\n \n public void onFinish() {\n mTextField.setText(\"done!\");\n }\n }.start();\n \n```\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\nThe calls to [onTick(long)](#onTick(kotlin.Long)) are synchronized to this object so that one call to [onTick(long)](#onTick(kotlin.Long)) won't ever occur before the previous callback is complete. This is only relevant when the implementation of [onTick(long)](#onTick(kotlin.Long)) takes an amount of time to execute that is significant compared to the countdown interval.\n\nSummary\n-------\n\n| Public constructors ||\n|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|---|\n| [CountDownTimer](#CountDownTimer(kotlin.Long,%20kotlin.Long))`(`millisInFuture:` `[Long](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-long/index.html)`, `countDownInterval:` `[Long](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-long/index.html)`)` \u003cbr /\u003e |\n\n| Public methods ||\n|---------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| [Unit](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-unit/index.html) | [cancel](#cancel())`()` Cancel the countdown. |\n| abstract [Unit](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-unit/index.html) | [onFinish](#onFinish())`()` Callback fired when the time is up. |\n| abstract [Unit](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-unit/index.html) | [onTick](#onTick(kotlin.Long))`(`millisUntilFinished:` `[Long](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-long/index.html)`)` Callback fired on regular interval. |\n| [CountDownTimer](#)! | [start](#start())`()` Start the countdown. |\n\nPublic constructors\n-------------------\n\n### CountDownTimer\n\nAdded in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels) \n\n```\nCountDownTimer(\n millisInFuture: Long, \n countDownInterval: Long)\n```\n\n| Parameters ||\n|---------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `millisInFuture` | [Long](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-long/index.html): The number of millis in the future from the call to [start()](#start()) until the countdown is done and [onFinish()](#onFinish()) is called. |\n| `countDownInterval` | [Long](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-long/index.html): The interval along the way to receive [onTick(long)](#onTick(kotlin.Long)) callbacks. |\n\nPublic methods\n--------------\n\n### cancel\n\nAdded in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels) \n\n```\nfun cancel(): Unit\n```\n\nCancel the countdown. \n\n### onFinish\n\nAdded in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels) \n\n```\nabstract fun onFinish(): Unit\n```\n\nCallback fired when the time is up. \n\n### onTick\n\nAdded in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels) \n\n```\nabstract fun onTick(millisUntilFinished: Long): Unit\n```\n\nCallback fired on regular interval.\n\n| Parameters ||\n|-----------------------|------------------------------------------------------------------------------------------------------------------|\n| `millisUntilFinished` | [Long](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-long/index.html): The amount of time until finished. |\n\n### start\n\nAdded in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels) \n\n```\nfun start(): CountDownTimer!\n```\n\nStart the countdown."]]