Skip to content
Related Articles

Related Articles

PulseCountDown in Android with Example
  • Difficulty Level : Medium
  • Last Updated : 30 Apr, 2021

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.  

pulse countdown

Table of Attributes 

XML Attribute

Method

Functionality



pc_startValuestartValue(int value)Starting value of the PulseCountDown (by default 10)
pc_endValueendValue(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-rc1’  

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. 

XML




<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
 
    <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 file

Go to the MainActivity file and refer to the following code. In this file add the onClickListner() 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.

Kotlin




import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.Toast
import com.gusakov.library.start
import kotlinx.android.synthetic.main.activity_main.*
 
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()
            }
        })
    }
}

Java




import androidx.appcompat.app.AppCompatActivity;
 
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
 
import com.gusakov.library.PulseCountDown;
import com.gusakov.library.java.interfaces.OnCountdownCompleted;
 
public class MainActivity extends AppCompatActivity {
 
    // creating object of Button and PulseCountDown
    private Button buttonStart;
    private PulseCountDown pulseCountDown;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        // initialising objects
        buttonStart=(Button)findViewById(R.id.buttonStart);
        pulseCountDown=(PulseCountDown)findViewById(R.id.pulseCountDown);
 
        // button OnClickListener
        buttonStart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                 
                // start countdown and add OnCountdownCompleted
                pulseCountDown.start(new OnCountdownCompleted() {
                    @Override
                    public void completed() {
                        // show simple toast when countdown completed
                        Toast.makeText(MainActivity.this, "Pulse Count Completed :)", Toast.LENGTH_SHORT).show();
                    }
                });
            }
        });
    }
}

Output:

Want a more fast-paced & competitive environment to learn the fundamentals of Android?
Click here to head to a guide uniquely curated by our experts with the aim to make you industry ready in no time!
My Personal Notes arrow_drop_up
Recommended Articles
Page :