how to create a countdown timer in android studio
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:background="@color/primary"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="200dp"
android:gravity="center"
android:orientation="horizontal">
<TextView
android:id="@+id/hour"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="@drawable/round_back_black10_10"
android:gravity="center"
android:text="00"
android:textColor="#FFFFFF"
android:textSize="30sp"
android:textStyle="bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:text=":"
android:textColor="#FFFFFF"
android:textSize="30sp"
android:textStyle="bold" />
<TextView
android:id="@+id/min"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="@drawable/round_back_black10_10"
android:gravity="center"
android:text="00"
android:textColor="#FFFFFF"
android:textSize="30sp"
android:textStyle="bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:text=":"
android:textColor="#FFFFFF"
android:textSize="30sp"
android:textStyle="bold" />
<TextView
android:id="@+id/second"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="@drawable/round_back_black10_10"
android:gravity="center"
android:text="00"
android:textColor="#FFFFFF"
android:textSize="30sp"
android:textStyle="bold" />
</LinearLayout>
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/startBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="100dp"
android:background="@color/secondary"
android:text="Start"
android:textAllCaps="false"
android:textColor="#FFFFFF" />
</RelativeLayout>
drawable/round_back_black10_10.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#1AFFFFFF"/>
<corners android:radius="10dp"/>
</shape>
MainActivity.java
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatButton;
import java.util.Locale;
import java.util.concurrent.TimeUnit;
public class MainActivity extends AppCompatActivity {
// set end time in seconds
private int duration = 120;
private boolean timerRunning = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView hour = findViewById(R.id.hour);
final TextView min = findViewById(R.id.min);
final TextView seconds = findViewById(R.id.second);
final AppCompatButton startBtn = findViewById(R.id.startBtn);
startBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// check if countdown timer is already running or not
if (!timerRunning) {
timerRunning = true;
new CountDownTimer(duration * 1000, 1000) {
@Override
public void onTick(long millisUntilFinished) {
runOnUiThread(new Runnable() {
@Override
public void run() {
// getting time format on HH:MM:SS
String time = String.format(Locale.getDefault(), "%02d:%02d:%02d", TimeUnit.MILLISECONDS.toHours(millisUntilFinished),
TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished) -
TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millisUntilFinished)),
TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished)));
final String[] hourMinSec = time.split(":");
hour.setText(hourMinSec[0]);
min.setText(hourMinSec[1]);
seconds.setText(hourMinSec[2]);
}
});
}
@Override
public void onFinish() {
// reset timer duration
duration = 120;
timerRunning = false;
}
}.start();
} else {
Toast.makeText(MainActivity.this, "Timer is already running", Toast.LENGTH_SHORT).show();
}
}
});
}
}
Output
![](https://learnoset.com/Learnoset/CodeOutput/count_down_timer.gif)
Projects with Source Code + Video Tutorials
You can download our Java and Android Studio Projects with Source Code and Video Tutorials.
![Login-and-Register-Activity-Material-UI-design-in-Android-Studio](https://learnoset.com/Learnoset/Projects/LoginAndRegister05/login_register_05.png)
Login and Register Activity Material UI design in Android Studio
![Simple-Calculator-App-for-Android------------------
----------](https://learnoset.com/Learnoset/Projects/Calculater/screenshot1.jpg)
Simple Calculator App for Android
![Login-and-Register-screen-with-OTP-Verification-Material-UI-design](https://learnoset.com/Learnoset/Projects/LoginAndRegister04/login-and-register-screen-with-otp-verification-in-android-studio.jpg)
Login and Register screen with OTP Verification Material UI design
If you have any Questions or Queries
You can mail us at info.learnoset@gmail.com
Follow us to learn Coding and get in touch with new Technologies.