How to create a Countdown timer in Android
In this post, I will show you how to create a Countdown Timer in your Android App.
Countdown Timer is used when we create a Stopwatch app or when we perform some tasks after the timer finished its countdown.
We use a Button and Edittext in it.
Get value from Edittext and then click Button to start the Countdown timer.
Firstly we create a text view, a Button and Edittext in our XML File.
MainActivity.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=".MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start"
android:id="@+id/Button"
app:layout_constraintTop_toBottomOf="@+id/Edittext"
></Button>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/Edittext"
app:layout_constraintBottom_toBottomOf="@+id/Button"
></EditText>
<TextView
android:id="@+id/Textview1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Here, we create an Edittext to get timer value, a Button to start our Countdown timer and a TextView to show our Countdown timer.
Then in our java file, we call Countdown timer and create an instance of it.
MainActivity.java
public class MainActivity extends AppCompatActivity {
CountDownTimer ctimer;
TextView cview;
Button btn;
EditText et;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
cview = findViewById(R.id.Textview1);
btn = findViewById(R.id.Button);
et = findViewById(R.id.Edittext);
Here, we call Countdown timer and name it as ctimer. Also, we find ids of our Button, Edittext, and Textview.
Then we create a Button click listener
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
timer(Long.parseLong(et.getText().toString()));
}
});
Here, we call a method and parse the value of Edittext in it.
Get the value of Edittext then parse it in String and then parse it in Long.
Then we create a Countdown timer and set value to Textview
public void timer(Long milli)
{
ctimer = new CountDownTimer(milli*1000,1000) {
@Override
public void onTick(long l) {
String value = String.valueOf(l/1000);
cview.setText(value);
}
@Override
public void onFinish() {
Toast.makeText(MainActivity.this, "CountDown Finished", Toast.LENGTH_SHORT).show();
}
};
ctimer.start();
}
Here, milli is our Long variable.The countdown timer is working in milliseconds. So, we multiply it with 1000 to convert it in seconds. Second 1000 is countdown Interval.
In our onTick method, we parse the value to String and set the value to Textview.
Then we show a Toast message in our onFinish method.
Remember to start the timer at the end.
And we create our Coundown timer successfully.
Follow us for more posts like this,
Subscribe Harpreet studio on Youtube
Like Harpreet Studio on Facebook
No comments