Countdown Timer

Android Count Down Timer With Toast Example:-


                                           In this sample application we will create a countdown timer in android using the CountDownTimer class .This is an example of showing a 15 second countdown in a TextView . We will update the TextView on every tick of the timer.

1)CountDownTimer.java

package com.Parthiv.CountdownTimer;


import java.util.Timer;
import java.util.TimerTask;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.widget.TextView;
import android.widget.Toast;
import android.app.Activity;

public class CountDownTimer extends Activity {

Timer timer;
TimerTask timerTask;
final Handler handler = new Handler();
MyCountDown countdown;
private final long startTime = 15000; // 1000 = 1 second
private final long interval = 1000;
int timeRemain;
int remain;
TextView result;

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

final MyCountDown countdown = new MyCountDown(startTime, interval);
countdown.start();

startTimer();

}

public void startTimer() {

timer = new Timer();
initializeTimerTask();
timer.schedule(timerTask, 15000, 15000); //

}

public void initializeTimerTask() {
timerTask = new TimerTask() {
public void run() {
handler.post(new Runnable() {
public void run() {

final MyCountDown countdown = new MyCountDown(
startTime, interval);
countdown.start();
// Toast.makeText(MainActivity.this, "No Internet",
// 1000).show();

class MyCountDown extends CountDownTimer {
public MyCountDown(long millisInFuture,
long countDownInterval) {
super(millisInFuture, countDownInterval);
// TODO Auto-generated constructor stub
}

@Override
public void onFinish() {
// TODO Auto-generated method stub
TextView result = (TextView) findViewById(R.id.txtResult);
result.setText("");
Toast.makeText(MainActivity.this, "Finished!",
Toast.LENGTH_SHORT).show();
}

@Override
public void onTick(long remain) {
// TODO Auto-generated method stub
TextView result = (TextView) findViewById(R.id.txtResult);
timeRemain = (int) (remain) / 1000;
result.setText(" Countdown : " + timeRemain);
}

}

}
});
}
};
}

public class MyCountDown extends CountDownTimer {
public MyCountDown(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
// TODO Auto-generated constructor stub
}

@Override
public void onFinish() {
// TODO Auto-generated method stub
TextView result = (TextView) findViewById(R.id.txtResult);
result.setText("");
Toast.makeText(MainActivity.this, "Finished!", Toast.LENGTH_SHORT)
.show();
}

@Override
public void onTick(long remain) {
// TODO Auto-generated method stub
TextView result = (TextView) findViewById(R.id.txtResult);
timeRemain = (int) (remain) / 1000;
result.setText(" Countdown : " + timeRemain);
}

}
}

2) activity_main.xml

<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:background="#7754a3" >

    <TextView
        android:id="@+id/txtResult"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/btnStop"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="98dp"
        android:text="countdown"
        android:textColor="#fff"
        android:textSize="20dp" />

</RelativeLayout>


Output:-

     



After coundown finished Toast:-

                                       




Keep Updating Every Day....




No comments:

Post a Comment

back to top