In this tutorial, i’ll show you How to use CountDown Timer with a basic example. The program counts down from 10 minutes to 0.
In this project we used the abstract CountDown class for running per seconds.
CountDown used to Schedule a countdown until a time in the future, with regular notifications on intervals along the way.
Here is Android CountDown Example screen snap.
activity_main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | <?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=".MainActivity"> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="100dp" android:text="10:00" android:textSize="48sp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="64dp" android:text="Start" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/textView" /> </androidx.constraintlayout.widget.ConstraintLayout> |
MainActivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 | package com.example.countdownornek; 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; public class MainActivity extends AppCompatActivity { private TextView txtCount; private Button btnStart; private CountDownTimer countDownTimer; private long timeLeftInMillisecond=600000; private boolean timerRunning; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); txtCount=findViewById(R.id.textView); btnStart=findViewById(R.id.button); btnStart.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { startStop(); } }); } public void startStop(){ if(timerRunning){ stopTimer(); } else{ startTimer(); } } public void startTimer(){ countDownTimer=new CountDownTimer(timeLeftInMillisecond,1000) { @Override public void onTick(long millisUntilFinished) { timeLeftInMillisecond=millisUntilFinished; updateTimer(); } @Override public void onFinish() { } }.start(); btnStart.setText("Pause"); timerRunning=true; } public void stopTimer(){ countDownTimer.cancel(); btnStart.setText("Start"); timerRunning=false; } public void updateTimer(){ int minutes=(int)timeLeftInMillisecond/60000; int seconds=(int)timeLeftInMillisecond % 60000/1000; String timeLeftText=""; if(minutes<10){ timeLeftText+="0"+minutes; } else{ timeLeftText+=minutes; } timeLeftText+=":"; if(seconds<10){ timeLeftText+=0; } timeLeftText+=seconds; txtCount.setText(timeLeftText); } } |
Output: