Create CountDown timer in android example tutorial

How to make a count down timer in android app and show message after finish count down.

In this tutorial we are creating count down timer clock using textview and CountDownTimer class. This class help us to set automatic schedule inside android application, So application developer can show live count down time into TextView. So here is the complete step by step tutorial for Create CountDown timer in android example tutorial.

android-project-download-code-button

How to Create CountDown timer in android example tutorial.

Code for MainActivity.java file.

 package com.countdowntimer_android_examples.com;

import android.app.Activity;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {

 Button button;
 TextView textview;
 CountDownTimer countdowntimer;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 button = (Button)findViewById(R.id.button1);
 textview = (TextView)findViewById(R.id.textView1);
 
 button.setOnClickListener(new View.OnClickListener() {
 
 @Override
 public void onClick(View v) {
 // TODO Auto-generated method stub
 
 countdowntimer = new CountDownTimerClass(10000, 1000);
 
 countdowntimer.start();
 
 }
 });
 }

 
 public class CountDownTimerClass extends CountDownTimer {
 
 public CountDownTimerClass(long millisInFuture, long countDownInterval) {
 
 super(millisInFuture, countDownInterval);
 
 }
 
 @Override
 public void onTick(long millisUntilFinished) {
 
 int progress = (int) (millisUntilFinished/1000);

 textview.setText(Integer.toString(progress));
 
 }
 
 @Override
 public void onFinish() {
 
 textview.setText(" Count Down Finish ");
 
 }
 }
 
 
}

Code for activity_main.xml layout file.

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:paddingBottom="@dimen/activity_vertical_margin"
 android:paddingLeft="@dimen/activity_horizontal_margin"
 android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin"
 tools:context="com.countdowntimer_android_examples.com.MainActivity" >

 <TextView
 android:id="@+id/textView1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_centerHorizontal="true"
 android:layout_centerVertical="true"
 android:textAppearance="?android:attr/textAppearanceLarge" />

 <Button
 android:id="@+id/button1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_below="@+id/textView1"
 android:layout_centerHorizontal="true"
 android:layout_marginTop="36dp"
 android:text="Start Count Down Timer" />

</RelativeLayout>

Screenshots:

CountDowntimer

 

Create CountDown timer in android example tutorial

 

CountDowntimer-2

Click here to download Create CountDown timer in android example tutorial project with source code.

Leave a Reply

Your email address will not be published. Required fields are marked *