PulseCountDown in Android with Example
PulseCountDown in Android is an alternative to CountDownTimer. A CountDownTimer is an accurate timer that can be used for a website or blog to display the count-down to any special event, such as a birthday or anniversary. It is very easy to implement PulseCountDown instead of CountDownTimer because PulseCountDown provides a default layout with some beautiful animations. By default, the start value of PulseCountDown is 10 and the end value is 0. Suppose there needs a quiz app, and in that add time limit to answer a question there PulseCountDown can be used.
Note: This library is no longer updated and won't work in newer versions of Android Studio.
Table of Attributes
XML Attribute | Method | Functionality |
pc_startValue | startValue(int value) | Starting value of the PulseCountDown (by default 10) |
pc_endValue | endValue(int value) | Value before which the countdown will stop(by default 0) |
- | start(callback: () -> Unit = {}) | Start the countdown |
Step by Step Implementation
Step 1: Create a New Project
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio.
Note: Minimum SDK version must be more than API 22: Android 5.1 (Lollipop) to implement this dependency. So choose the appropriate SDK while creating the new project.
Step 2: Add dependency
Navigate to the Gradle Scripts > build.gradle(Module:app) and add the below dependency in the dependencies section.
implementation ("com.gusakov:pulse-countdown:1.1.0-rc2")
Step 3: Working with the activity_main.xml file
Navigate to the app > res > layout > activity_main.xml and add the below code to that file. In this file add the PulseCountDown view and a start button to the layout. Below is the code for the activity_main.xml file.
activity_main.xml:
<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:background="@color/white"
tools:context=".MainActivity">
<com.gusakov.library.PulseCountDown
android:id="@+id/pulseCountDown"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="5"
android:textSize="100sp"
app:pc_startValue="5"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="188dp"
android:text="Start Countdown"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Step 4: Working with the MainActivity.kt file
Go to the MainActivity file and refer to the following code. In this file add the onClickListener() method to the button to start the PulseCountDownView. Below is the code for the MainActivity file. Comments are added inside the code to understand the code in more detail.
MainActivity.kt:
package org.geeksforgeeks.demo
import android.os.Bundle
import android.widget.CalendarView
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// when the user tap on the button this
// function get invoked automatically.
button.setOnClickListener(View.OnClickListener {
pulseCountDown.start {
// when countdown ends a toast
// will be shown to user
Toast.makeText(this,
"Course Alert!", Toast.LENGTH_LONG).show()
}
})
}
}