How to create a countdown timer in android

In this android programming source code example, we are going to create a countdown timer in android.

You can copy and adopt this source code example to your android project without reinventing the wheel.

Below is a step by step source code to create a countdown timer in android.

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"
    tools:context=".WorkManagerAndJobSchedule.WorkManagerAndJobScheduleActivity19">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        android:layout_marginStart="10dp"
        android:layout_marginEnd="10dp">

        <TextView
            android:id="@+id/tvtime"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:text="....."
            android:textColor="#000000"
            android:textSize="19sp"/>

        <Button
            android:id="@+id/timer"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/colorAccent"
            android:text="Start CountDown Timer"
            android:textAllCaps="false"
            android:textColor="#ffffff"
            android:textSize="19sp"
            android:layout_marginTop="10dp"/>

    </LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.java

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.bluapp.androidview2.R;

public class WorkManagerAndJobScheduleActivity19 extends AppCompatActivity {
    private TextView tvtime;
    private Button timer;
    private int counter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_work_manager_and_job_schedule19);
        tvtime = (TextView) findViewById(R.id.tvtime);
        timer = (Button) findViewById(R.id.timer);
        timer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startTimer();
            }
        });
    }
    private void startTimer(){
        new CountDownTimer(30000, 1000){
            @Override
            public void onTick(long millisUntilFinished) {
               tvtime.setText(String.valueOf(counter));
                counter++;
            }
            @Override
            public void onFinish() {
                tvtime.setText("Timer Finish");
            }
        }.start();
    }
}

If you have any question or suggestions kindly use the comment box or you can contact us directly through our contact page below.

Add a Comment