How to set timer in android?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
There is no single timer API that is correct for every Android use case. A countdown shown on screen, a short UI delay, and work that must happen later in the background are three different problems and should be solved with different tools.
That is why the real question is not just "how do I set a timer," but "what kind of timed behavior do I need?" Once that is clear, the right API becomes much easier to choose.
Use CountDownTimer for Visible Countdowns
If the user should see a ticking countdown, CountDownTimer is the simplest built-in choice:
This fits quiz timers, resend-code countdowns, and other user-visible countdown UI.
Use a Delayed Callback for One-Shot UI Work
If you only need to run something once after a short delay on the main thread, a delayed callback is often enough:
This is a better fit than older Java TimerTask code for most UI delays because it is explicitly tied to the main thread.
Coroutines Are Often the Cleanest Modern Choice
If your project already uses Kotlin coroutines, a lifecycle-aware coroutine is usually the cleanest way to express a delay:
This reads well, integrates naturally with modern Android architecture, and avoids some of the callback clutter that grows around handlers in larger screens.
Do Not Use UI Timers for Background Scheduling
If the task must happen later even when the app is not on screen, UI timers are the wrong tool. For background work, the better choices are:
- '
WorkManagerfor deferrable background jobs' - '
AlarmManagerfor cases that truly need alarm-style scheduling'
Trying to keep an Activity timer alive in the background is unreliable because Android is free to stop or destroy the process.
Choose the Timer by Behavior, Not by Familiarity
A practical rule is:
- choose
CountDownTimerfor countdown displays - choose
Handler.postDelayedfor short one-shot UI delays - choose coroutines when the app already uses them
- choose
WorkManagerorAlarmManagerfor delayed background behavior
The old instinct to reach for Timer or TimerTask everywhere usually leads to code that is harder to tie back to the Android lifecycle.
Common Pitfalls
The biggest mistake is starting a timer in an Activity or Fragment and forgetting to cancel it. That can leak work or try to update views that no longer exist.
Another common problem is using a UI timer for background scheduling. What works on a foreground test device often fails once the app is backgrounded or the process is reclaimed.
People also update views from the wrong thread when they use older timer APIs. Anything touching the UI should be on the main thread.
Finally, do not overcomplicate simple delays. If you only need one delayed action, a small delayed callback or coroutine is usually clearer than a repeating timer abstraction.
Summary
- Use
CountDownTimerfor visible countdowns. - Use a delayed callback or coroutine for short UI delays.
- Use
WorkManagerorAlarmManagerfor background timing needs. - Cancel timers when the owning lifecycle ends.
- Pick the timer API based on the behavior you need, not on habit.
Related reading
- How to set top-left alignment for UILabel for iOS application?
- How to set UICollectionViewCell Width and Height programmatically
- How to set UICollectionViewDelegateFlowLayout?
- How to set UITextField height?
- How to share an image on Instagram in iOS?
- How to show a dialog to confirm that the user wishes to exit an Android Activity?
- How to show an empty view with a RecyclerView?
- How to show Done button on iOS number pad keyboard?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.