Android Tutorial
Software Setup and Configuration
Android Studio
File Structure
Components
Core Topics
Layout
View
Button
Intent and Intent Filters
Toast
RecyclerView
Fragments
Adapters
Other UI Component
Image Loading Libraries
Date and Time
Material Design
Bars
Working with Google Maps
Chart
Animation
Database
Advance Android
Jetpack
Architecture
App Publish
App Monetization
CountDownTimer
is a useful utility class in Android that allows you to schedule a countdown until a time in the future, with regular notifications on intervals along the way. This can be used for various purposes like showing a timer on the screen, scheduling an event to happen after a delay, or building a game mechanism, etc.
To use the CountDownTimer
, you need to:
onTick()
method, which will be executed at every interval you've defined.onFinish()
method, which will be executed when the countdown is done.Imagine you're building a simple countdown of 10 seconds that updates a TextView
every second and displays a toast message when the countdown finishes.
activity_main.xml
):<TextView android:id="@+id/timerTextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Starting..." android:textSize="24sp"/>
class MainActivity : AppCompatActivity() { private lateinit var timerTextView: TextView private lateinit var countDownTimer: CountDownTimer override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) timerTextView = findViewById(R.id.timerTextView) countDownTimer = object : CountDownTimer(10000, 1000) { override fun onTick(millisUntilFinished: Long) { val secondsRemaining = millisUntilFinished / 1000 timerTextView.text = "$secondsRemaining seconds remaining" } override fun onFinish() { timerTextView.text = "Done!" Toast.makeText(this@MainActivity, "Countdown finished!", Toast.LENGTH_SHORT).show() } } countDownTimer.start() } override fun onDestroy() { super.onDestroy() countDownTimer.cancel() // Always cancel the timer to avoid memory leaks. } }
In the above example:
onTick()
method will be called every 1,000 milliseconds (1 second).onFinish()
method is called when the countdown is finished.Remember to cancel the CountDownTimer
in onDestroy()
or other appropriate lifecycle methods to ensure no unwanted operations happen if the Activity is destroyed.
Implementing CountDownTimer in Android Kotlin:
// Example of implementing CountDownTimer val countDownTimer = object : CountDownTimer(60000, 1000) { override fun onTick(millisUntilFinished: Long) { // Update UI or perform actions on each tick } override fun onFinish() { // Perform actions when the countdown is finished } } // Start the countdown timer countDownTimer.start()
Creating a countdown timer in Android:
// Example of creating a countdown timer val countDownTimer = object : CountDownTimer(60000, 1000) { override fun onTick(millisUntilFinished: Long) { // Update UI or perform actions on each tick } override fun onFinish() { // Perform actions when the countdown is finished } } // Start the countdown timer countDownTimer.start()
CountDownTimer for countdown functionality in Android:
// Example of using CountDownTimer for countdown val countDownTimer = object : CountDownTimer(60000, 1000) { override fun onTick(millisUntilFinished: Long) { // Update UI or perform actions on each tick } override fun onFinish() { // Perform actions when the countdown is finished } } // Start the countdown timer countDownTimer.start()
Customizing CountDownTimer appearance in Android:
// Example of updating a TextView with CountDownTimer val textView = findViewById<TextView>(R.id.textView) val countDownTimer = object : CountDownTimer(60000, 1000) { override fun onTick(millisUntilFinished: Long) { // Update the TextView with the remaining time textView.text = "Time remaining: ${millisUntilFinished / 1000} seconds" } override fun onFinish() { textView.text = "Countdown finished!" } } // Start the countdown timer countDownTimer.start()
Handling events with CountDownTimer in Android:
onTick
and onFinish
events that you can handle to perform actions at specified intervals during the countdown.// Example of handling events with CountDownTimer val countDownTimer = object : CountDownTimer(60000, 1000) { override fun onTick(millisUntilFinished: Long) { // Update UI or perform actions on each tick } override fun onFinish() { // Perform actions when the countdown is finished } } // Start the countdown timer countDownTimer.start()
Updating UI with CountDownTimer in Android:
// Example of updating UI with CountDownTimer val progressBar = findViewById<ProgressBar>(R.id.progressBar) val countDownTimer = object : CountDownTimer(60000, 1000) { override fun onTick(millisUntilFinished: Long) { // Update the progress bar based on the remaining time val progress = (millisUntilFinished / 1000).toInt() progressBar.progress = progress } override fun onFinish() { progressBar.progress = 0 } } // Start the countdown timer countDownTimer.start()
Pausing and resuming CountDownTimer in Android:
// Example of pausing and resuming CountDownTimer val countDownTimer = object : CountDownTimer(60000, 1000) { override fun onTick(millisUntilFinished: Long) { // Update UI or perform actions on each tick } override fun onFinish() { // Perform actions when the countdown is finished } } // Start the countdown timer countDownTimer.start() // Pause the countdown timer countDownTimer.cancel() // Resume the countdown timer with the remaining time countDownTimer.start()
Countdown timer for game development in Android:
// Example of using CountDownTimer in game development val countDownTimer = object : CountDownTimer(30000, 1000) { override fun onTick(millisUntilFinished: Long) { // Update UI or perform actions on each tick updateGameUI(millisUntilFinished) } override fun onFinish() { // Perform actions when the game timer is finished endGame() } } // Start the game countdown timer countDownTimer.start()