The countdown timer app is about setting a time that moves in reverse order as it shows the time left in the upcoming event. A countdown timer is an accurate timer that can be used for a website or blog to display the countdown 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-by-Step Implementation
Step 1: Create a New Project in Android Studio
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. The code for that has been given in both Java and Kotlin Programming Language for Android.
Step 2: Working with the XML files
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.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".MainActivity">
<!-- Toolbar for the activity -->
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:title="GFG App"
app:titleTextColor="@color/white"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
<LinearLayout
android:layout_width="match_parent"
android:orientation="vertical"
android:gravity="center"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_gravity="center"
android:textSize="20dp"
android:textStyle="bold"
android:layout_height="wrap_content"
android:textColor="?attr/colorPrimary"/>
</LinearLayout>
</LinearLayout>
Step 3: Working with the MainActivity file
Now In the MainActivity 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 at 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 file is given below.
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 {
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initialize the TextView
textView = findViewById(R.id.textView);
// Create and start the countdown timer
new CountDownTimer(50000, 1000) {
@Override
public void onTick(long millisUntilFinished) {
// Used for formatting digits 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));
}
@Override
public void onFinish() {
// When the task is over it will print 00:00:00
textView.setText("00:00:00");
}
}.start();
}
}
Output:
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 that adds a time limit to answer a question there PulseCountDown can be used. To implement PulseCountDown please refer PulseCountDown in Android with an Example.