Skip to content
Related Articles
Open in App
Not now

Related Articles

CountDownTimer in Android with Example

Improve Article
Save Article
  • Difficulty Level : Expert
  • Last Updated : 24 Aug, 2022
Improve Article
Save Article

CountDownTimer app is about setting a time that moves in reverse order as it shows the time left in the upcoming event. A CountDownTimer is an accurate timer that can be used for a website or blog to display the count down to any special event, such as a birthday or anniversary. Likewise, here let’s create an Android App to learn how to create a simple Countdown App. So let’s begin app creation step by step towards its completion.

countdowntimer

Approach

Step 1: Create a New Project

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Java as the programming language.

Step 2: Working with activity_main.xml file

In the activity_main.xml file add only a TextView to display the CountDownTimer. Below is the complete code for the activity_main.xml file.

activity_main.xml

 

Step 3: Working with MainActivity.java file

Now In MainActivity.java file, create an object of TextView and map the components(TextView) with their id. 

// Initializing thetextView

TextView textView;

textView = findViewById (R.id.textView);

Schedule a countdown until a time in the future, with regular notifications on intervals along the way. Example of showing a 50-second countdown in a text field:

new CountDownTimer(50000, 1000) {

           public void onTick(long millisUntilFinished) {

               // Used for formatting digit to be in 2 digits only

               NumberFormat f = new DecimalFormat(“00”);

               long hour = (millisUntilFinished / 3600000) % 24;

               long min = (millisUntilFinished / 60000) % 60;

               long sec = (millisUntilFinished / 1000) % 60;

               textView.setText(f.format(hour) + “:” + f.format(min) + “:” + f.format(sec));

           }

           // When the task is over it will print 00:00:00 there

           public void onFinish() {

               textView.setText(“00:00:00”);

           }

       }.start();

The complete code for the MainActivity.java file is given below.

MainActivity.java

 

Output: Run on Emulator

PulseCountDown

PulseCountDown in Android is an alternative to CountDownTimer. It is very easy to implement PulseCountDown instead of CountDownTimer because PulseCountDown provides a default layout with some beautiful animations. By default start value of PulseCountDown is 10 and the end value is 0. Suppose there needs a quiz app, and in that add time limit to answer a question there PulseCountDown can be used. To implement PulseCountDown please refer PulseCountDown in Android with Example.
 

pulse countdown

Pulse Count Down

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!