CountDownTimer in Android with Example

19 Feb, 2021
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.
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
<?xml version="1.0" encoding="utf-8"?><androidx.constraintlayout.widget.ConstraintLayout    xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:background="#fff"    tools:context=".MainActivity">      <!-- Here adding a TextView to show countdown -->    <TextView        android:id="@+id/textView"        android:layout_width="0dp"        android:layout_height="0dp"        android:background="?android:attr/detailsElementBackground"        android:gravity="center_horizontal|center_vertical"        android:text="TextView"        android:textColor="@color/colorPrimary"        android:textSize="36sp"        android:textStyle="bold"        app:layout_constraintBottom_toBottomOf="parent"        app:layout_constraintEnd_toEndOf="parent"        app:layout_constraintHorizontal_bias="0.0"        app:layout_constraintStart_toStartOf="parent"        app:layout_constraintTop_toTopOf="parent"        app:layout_constraintVertical_bias="0.0" />  </androidx.constraintlayout.widget.ConstraintLayout>
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
package org.gfg.countdowntimer;  import android.os.Bundle;import android.os.CountDownTimer;import android.widget.TextView;import androidx.appcompat.app.AppCompatActivity;import java.text.DecimalFormat;import java.text.NumberFormat;  public class MainActivity extends AppCompatActivity {    // Initializing my textView    TextView textView;      @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);          textView = findViewById(R.id.textView);        // Time is in millisecond so 50sec = 50000 I have used        // countdown Interveal is 1sec = 1000 I have used        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();    }}
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 Count Down
Android-Date-time
Android
Java
Java
Android
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.