Code Example

Kotlin Android CountDownTimer Snippets

Android step by step CountDownTimer examples.

1. alexfu/CountDownView

A simple countdown View for the Android framework.

A simple view that does a temporal count down with fields - Hours, Minutes and Seconds.

CountDownView Example Tutorial

Step 1: Installation

Pull in CountDownView from Jitpack.

allprojects {
  repositories {
    maven { url 'https://jitpack.io' }
  }
}

dependencies {
  implementation 'com.github.alexfu:CountDownView:0.1.0'
}

Step 2: Usage

Include in your layout.

<com.alexfu.countdownview.CountDownView
    android:id="@+id/count_down"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="@style/TextAppearance.AppCompat.Display1"
    app:startDuration="60000"/>

Then, in your Activity/Fragment, obtain the view and call start().

CountDownView countDownView = findViewById(R.id.count_down);
countDownView.start();

Full Example

Let us look at a full Example below.

Step 1. Design Layouts

We need to design our XML layouts as follows:

(a). activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    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:gravity="center"
    android:orientation="vertical"
    tools:context="com.alexfu.countdownview.sample.MainActivity">

    <com.alexfu.countdownview.CountDownView
        android:id="@+id/view_count_down"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="@style/TextAppearance.AppCompat.Display1"
        app:startDuration="60000"/>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <android.support.v7.widget.AppCompatButton
            android:id="@+id/button_start"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Start"
            app:backgroundTint="#C8E6C9"/>

        <android.support.v7.widget.AppCompatButton
            android:id="@+id/button_reset"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Reset"
            app:backgroundTint="#FFF9C4"/>

        <android.support.v7.widget.AppCompatButton
            android:id="@+id/button_stop"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Stop"
            app:backgroundTint="#FFCDD2"/>
    </LinearLayout>

</LinearLayout>

Step 2. Write Code

Finally we need to write our code as follows:

(a). MainActivity.java

package com.alexfu.countdownview.sample;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import com.alexfu.countdownview.CountDownView;
import com.alexfu.countdownview.CountDownListener;

public class MainActivity extends AppCompatActivity implements CountDownListener {
    private CountDownView countDownView;
    private Button startButton;
    private Button resetButton;
    private Button stopButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        countDownView = findViewById(R.id.view_count_down);
        startButton = findViewById(R.id.button_start);
        resetButton = findViewById(R.id.button_reset);
        stopButton = findViewById(R.id.button_stop);

        startButton.setOnClickListener(new View.OnClickListener() {
            @Override public void onClick(View view) {
                countDownView.start();
            }
        });

        resetButton.setOnClickListener(new View.OnClickListener() {
            @Override public void onClick(View view) {
                countDownView.reset();
            }
        });

        stopButton.setOnClickListener(new View.OnClickListener() {
            @Override public void onClick(View view) {
                countDownView.stop();
            }
        });
        countDownView.setListener(this);
    }

    @Override
    public void onFinishCountDown() {
        Toast.makeText(this, "Time up!!!", Toast.LENGTH_LONG).show();
    }
}

Reference

You can DOWNLOAD FULL CODE.
You can also browse code or read more here.
Follow code author here.


Read More.