CountDownTimer in Android

The countdown timer software allows you to establish a timer that moves in reverse order as it displays the amount of time remaining till the next event. A countdown timer is a precise timer that can be used on a website or blog to display the countdown to any special occasion.

In this article, you will learn how to create your countdown Example of Bottom Sheet​ in Android. So let’s take example of countdown  activity codes  in Android of Mobile application.  

Example of countdown

                                                                                      

Step-by-Step Im​plementation
​Step 1: Create a New Project in Android ​studio (File >new  project).  


Step 2: Create an activity_main.xml file in your layout folder . path (res>layout)

Implement the same in​voke the following code inside activity_main.xml file.

 <?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"
android:orientation="vertical"
tools:context=".MainActivity"
tools:ignore="HardcodedText">

<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/txtcounter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="50sp"
android:layout_marginTop="50dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
Step 3: Create an MainActivity.kt file in your package folder .

Implement the same invoke the fol​low​ing code inside MainActivity.kt file.

 import android.annotation.SuppressLint
import android.os.Bundle
import android.os.CountDownTimer
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.AppCompatButton
import androidx.appcompat.widget.AppCompatTextView
import java.text.DecimalFormat
import java.text.NumberFormat


class MainActivity : AppCompatActivity() {
@SuppressLint("MissingInflatedId")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val txtcounter = findViewById<View>(R.id.txtcounter) as AppCompatTextView
object : CountDownTimer(50000, 1000) {
@SuppressLint("SetTextI18n")
override fun onTick(millisUntilFinished: Long) {
val f: NumberFormat = DecimalFormat("00")
val hour = (millisUntilFinished / 3600000) % 24
val min = (millisUntilFinished / 60000) % 60
val sec = (millisUntilFinished / 1000) % 60
txtcounter.setText(
(f.format(hour) + ":" + f.format(min)).toString() + ":" + f.format(
sec
)
)
}

override fun onFinish() {
txtcounter.setText("00:00:00")
}
}.start()

}
}

Step 4 : Output of above example.

                                                       

   Happy coding!                                                        

365Bloggy July 23, 2024
Share this post
Tags
SUBSCRIBE THIS FORM


Archive