Creating a CountDownTimer | AndroidMonks

Creating a CountDownTimer | AndroidMonks

CountDownTimer unlike a Chronometer is used for one purpose only, to count down the time! Count Down Timer is a simple to implement Class that you can plug into any Activity.

Count Down Timer 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 Implementation

You can create a simple Count Down Timer by creating a Object for the Count Down Timer class. See the basic implement below.

new CountDownTimer(30000, 1000) {}.start()

The Count Down Timer constructor takes in 2 arguments millisecInFuture, countDownInterval. 

There are 4 important public calls that you can use with a count down timer. See the list below.

CountDownTimer – Public Methods

1. onTick(long millisUntilFinished )
Important method under Count Down Timer. The callback gets fired every time the clock ticks(milli second) until the final value. You can use that to set a TextView which will allow us to create a Timer like application.

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.

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

4. cancel()
It cancels the countdown timer.

CountDownTimer example in Android Studio

We will use Android Studio to create a sample Application using the Count Down Timer.

We will see a simple count_down.xml file below

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center">
    
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Count Down Timer"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/count_down_timer"
        android:textSize="35dp"
        android:textStyle="bold"
        />
</LinearLayout>

Create a CountDownTimerClass.java which will serve as our Activity,

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

public class CountDownTimerClass extends AppCompatActivity
{   int count = 1;
    TextView countDownTimer;
    Button startButton;
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.count_down);
        countDownTimer = findViewById(R.id.count_down_timer);
        new CountDownTimer(30000, 1000){
            public void onTick(long millisUntilFinished){
                countDownTimer.setText(String.valueOf(count));
                count++;
            }
            public  void onFinish(){
                countDownTimer.setText("FINISH!!");
            }
        }.start();
    }
}

Run this in your AVD, your resulting application is as below

"<yoastmark

Drop in any comments you have below.

“Learn and Be Curious”

 

Leave a Comment