Android simple package get verification code countdown

We believe that the effect shown in the figure is not unfamiliar to everyone. We can use many methods to achieve this effect. Here we use CountDownTimer timer to simply encapsulate this effect, which is convenient for us to call at any time.

The first page defines the following attributes in attrs.xml:

    <declare-styleable name="CountDownButton">
        <attr name="millisinfuture" format="integer"/>
        <attr name="countdowninterva" format="integer"/>
        <attr name="normalColor" format="color"/>
        <attr name="countDownColor" format="color"/>

The following is the specific code of the implementation, a very simple way, easy to understand:

 * Created by xiaolong on 2018/1/12.
public class CountDownButton extends Button{

    //Total duration
    private long millisinfuture;

    //Interval length
    private long countdowninterva;

    //Default background color
    private int normalColor;

    //Countdown background color
    private int countDownColor;

    //End or not
    private boolean isFinish;

    private CountDownTimer countDownTimer;

    public CountDownButton(Context context) {

    public CountDownButton(Context context, AttributeSet attrs) {
        this(context, attrs,0);

    public CountDownButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray typedArray = context.getTheme().obtainStyledAttributes(attrs, R.styleable.CountDownButton,defStyleAttr,0);
        //Set default duration
        millisinfuture = (long) typedArray.getInt(R.styleable.CountDownButton_millisinfuture,60000);
        //Set default interval length
        countdowninterva = (long)typedArray.getInt(R.styleable.CountDownButton_countdowninterva,1000);
        //Set default background color
        normalColor = typedArray.getColor(R.styleable.CountDownButton_normalColor,android.R.color.holo_blue_light);
        //Set default countdown background color
        countDownColor = typedArray.getColor(R.styleable.CountDownButton_countDownColor,android.R.color.darker_gray);
        //Default to closed
        isFinish = true;
        //Font Center
        //Default text and background colors
        //set timer 
        countDownTimer = new CountDownTimer(millisinfuture, countdowninterva) {
            public void onTick(long millisUntilFinished) {
                //not finished
                isFinish = false;

                setText((Math.round((double) millisUntilFinished / 1000) - 1) + "second");


            public void onFinish() {
                isFinish = true;


    private void normalBackground(){
        setText("Get verification code");

    public boolean isFinish() {
        return isFinish;

    public void cancel(){

    public void start(){


A simple way to call:

public class MainActivity extends AppCompatActivity {

    private CountDownButton countDownButton;

    protected void onCreate(Bundle savedInstanceState) {
        countDownButton = ((CountDownButton) findViewById(;
        countDownButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                //Here, judge whether the countdown is over, and avoid repeated request interface caused by multiple clicks during the countdown
                if (countDownButton.isFinish()) {
                    //Call after sending the authentication code successfully.

    protected void onDestroy() {
        if (!countDownButton.isFinish()) {

Such a simple encapsulation is over, and the process is very simple. This is mainly an exercise on the use of CountDownTimer, which has not been touched before. By the way Source code Now!

Tags: Android xml

Posted on Fri, 01 May 2020 16:08:39 -0700 by thunder708