CountDownTimer Tutorial With Example In Android Studio

CountDownTimer in Android is used to set a countdown based on interval set by you and it will stop when the time has come in future.  You can use this Count Down Timer for creating any countdown for event.

CountDownTimer Basic Code:

CountDownTimer (long millisecInFuture, 
                long countDownInterval)

Here millisecInFuture is the time you set in millisecond when you want CountDownTimer to stop and countDownInterval is the interval time in millisecond you set after which number will increment in CountDownTimer.

Methods of CountDownTimer in Android:

Let’s discuss some common methods of a CountDownTimer, which are used to configure a CountDownTimer in our application.

1. onTick(long millisUntilFinished )
It callback fired on regular interval and millisUntilFinished is the number of millis in the future from the call until the countdown is done.

2. onFinish()
It fires then the countdown timer finishes i.e time is up. User can add toast to show that time is up or can set text in textView.

public  void onFinish()

3. start()
It simply starts the countdown timer.

4. cancel()
It cancels the countdown timer.

CountDownTimer Example in Android Studio:

Example: In the below example of countdown timer we will show you the use of countdown timer in our application. For that we display a textview and a button in our xml file. In java class we used the countdown timer methods and add message when timer is over. Here we had set the time and till that specified time the timer will run and then stop. Below is the final output, download project code and step by step explanation:

Download Project Code(Password: abhiandroid) ?

CountDownTimer In Android Studio

Step 1: Create a new project and name it CountDownTimer

Step 2: Open res -> layout -> activity_main.xml (or) main.xml and add following code:

In this step we open an xml file and add a button and textview, on clicking button we will call the countdown timer to run and textview will display the time.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android=""

        android:textStyle="bold|italic" />

        android:textSize="40sp" />


Step 3: Open  app -> package ->
In this step we open MainActivity where we add the code to initiate the countdown timer and a text view to display time and then user can display the message that time is over by using a Toast and also displayed in the textview.

package com.example.countdowntimer;

import android.os.CountDownTimer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity  {
    public int counter;
    Button button;
    TextView textView;

    protected void onCreate(Bundle savedInstanceState) {
        button= (Button) findViewById(;
        textView= (TextView) findViewById(;
        button.setOnClickListener(new View.OnClickListener()
            public void onClick(View v) {
                new CountDownTimer(30000, 1000){
                    public void onTick(long millisUntilFinished){
                    public  void onFinish(){


Now run the App in AVD and you will see that on clicking the button will start the timer till the defined time.

Bonus Tip: You can also run the countdown in decreasing order. For that you just need to set the end value in a variable(counter) and then set it as counter. It will start the countdown from end value.

Recommended Read – How To Create CountDown Timer App In Android Studio: Step By Step Guide

  • Is this article helps you understand?
  • YES   NO

Leave a Reply

Your email address will not be published. Required fields are marked *

Also Read:

Subscribe and get access to the latest android tutorials, freebies, and lots more!

Android Developer Money eBook
One More Step...

Enter your email and I'll send you a FREE copy of Android Dev Dollaring eBook:
Send Me This eBook >>
We hate SPAM and promise to keep your email address safe.

Download Your Free Copy

Download this free eBook and Learn:
1. Different approach developer use to earn through App business
2. Learn how Android Developer can generate passive income
3. Job is not the only option and you can choose freelancing
Send Me This eBook >>
One More Step...

Enter your email and I'll send you a FREE copy of Android Dev Dollaring eBook:
Send Me This eBook >>
We hate SPAM and promise to keep your email address safe.