CountDownTimer Example

Countdowntimer is helpful for calculate time in seconds.

actvity_main.xml

This xml shows one textview and button in this layout. On Running Countdowntimer, The timer shows in textview.




    

MainActivity.java

This activity gets countdowntimer for counts and display using textview. If button gets click , the countdowntimer starts.

package com.example.icowboysradio.myapplication;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity{

    public int counter;
    Button button;
    TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button= (Button) findViewById(R.id.btn_timer);
        textView= (TextView) findViewById(R.id.textView);
        button.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v) {
                new CountDownTimer(100000, 1000){
                    public void onTick(long millisUntilFinished){
                        textView.setText(String.valueOf(counter));
                        counter++;
                    }
                    public  void onFinish(){
                        textView.setText("Finish 1 minute");
                    }
                }.start();
            }
        });
    }
}