Countdown Timer

Basics

Countdown is very useful way to make something happen later after determined time. It also can be used to make something happen at certain intervals. In the example below we build a countdown app that starts from 10 and goes down to zero one by one at one second intervals.

Layout file

First we need a layout file activity_main.xml. Later we will write the countdown numbers in this layout. Copy the code from below to your activity_main.xml.

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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"
    tools:context=".MainActivity">

<TextView
    android:id="@+id/countdownText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Countdown"
    android:textSize="100sp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

In the layout we have one textView in the center of the screen. We give it an id "countdownText”. Later we will refer to that id and make it show the numbers.

Countdown code

The actual code is in the MainActivity.java.

MainActivity.java
// Send and receive broadcast
package fi.sanoste.countdown;

import android.os.CountDownTimer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    CountDownTimer countDownTimer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Timer for polling and time out
        long totalTime = 10000;
        long interval = 1000;
        final TextView countdownText = findViewById(R.id.countdownText);
        countDownTimer = new CountDownTimer(totalTime, interval - 500) {
            public void onTick(long millisUntilFinished) {
                countdownText.setText(Long.toString((millisUntilFinished / 1000)+1));
            }
            public void onFinish() {
                countdownText.setText("Finish");
            }
        }.start();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        countDownTimer.cancel();
    }
}

In line 11, we define the countDownTimer.

In line 20, we define the total time to be 10000 millisecond that is 10 seconds.

In line 21, we set the interval to 1000 milliseconds that is one second.

In line 22, we define a TextView called countdownText and and make it point to counttdownText in the activity_main.xml.

The actual countdownTimer method starts in line 23. The method has two parameters: total time and interval. We can make the interval to be interval-500 to make it show correctly in the screen. The actual start of the timer is in line 30.

In line 24, the function onTick() runs at the determined intervals. Here it runs at one second intervals and shows the new number every time. MillisUntilFinished tells how much time we have before the end of the timer.

When we reach the total time, we run the function onFinish() in line 27. At that time we will show the text “Finish” on the screen.

In line 36, when we leave the activity, we need to cancel the countDownTimer. Otherwise it will keep running and may cause problems.