Tuesday 11 December 2012

Android Count Down Timer

Today I am going to explain CountDownTimer in android. Android provides CountDownTimer and mechods to start and cancel timer.

But there is not any methods to pause and resume timer. In this tutorial I have implemented how to pause and resume timer.

Here is layout file including button to start,pause and resume timer and textview to display time.

You can download project from Download

activity_timer.xml


   <LinearLayout 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:gravity="center"
        android:orientation="vertical"
        tools:context=".TimerActivity" >

        <TextView
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:padding="10dp"
              android:text="@string/description" />

        <TextView
             android:id="@+id/text"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:padding="10dp"
             android:textSize="25dp"
             android:textStyle="bold" />

        <Button
            android:id="@+id/btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Start" />

    </LinearLayout>


This is our activity class. I have declare one inner class MyTimer which extends CountDownTimer class.


TimerActivity.java

package com.ketan.countdowntimer;

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

public class TimerActivity extends Activity {
  
     Button btn_start;
     TextView txt_remaintime;
     MyTimer mTimer;
     long remainMilli = 0;
     boolean isRunning=false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
           super.onCreate(savedInstanceState);
           setContentView(R.layout.activity_timer);
   
          btn_start = (Button) findViewById(R.id.btn);
          txt_remaintime = (TextView) findViewById(R.id.text);

          btn_start.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
               
                if(isRunning){
                    //cancel (pause) timer when it is running
                    mTimer.cancel();
                    mTimer=null;
                    isRunning=false;
                    btn_start.setText("Resume");
                   
                }else{

                    if (remainMilli == 0) {
                        //start timer from initial time
                        mTimer = new MyTimer(5 * 1000 *60, 1000);
                    } else {
                        //resume timer from where it is paused
                        mTimer = new MyTimer(remainMilli, 1000);
                    }
                    btn_start.setText("Pause");
                    mTimer.start();   
                    isRunning=true;
                }
               
            }
        });
    }

   

    class MyTimer extends CountDownTimer {




         //constructor for timer class
        public MyTimer(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
           
        }


       // this method called when timer is finished
        @Override
        public void onFinish() {
            // reset all variables
            txt_remaintime.setText("Opps!! Time Up..");
            btn_start.setText("Start");
            isRunning=false;
            remainMilli=0;
        }

         //this method is called for every iteration of time interval
        @Override
        public void onTick(long millisUntilFinished) {
           
            remainMilli = millisUntilFinished;
           
            //calculate minutes and seconds from milliseconds
            String minute=""+(millisUntilFinished/1000)/60;
            String second=""+(millisUntilFinished/1000)%60;
           
            //apply style to minute and second
            if((millisUntilFinished/1000)/60<10)
                minute="0"+(millisUntilFinished/1000)/60;
            if((millisUntilFinished/1000)%60<10)
                second="0"+(millisUntilFinished/1000)%60;
           
            // update textview with remaining time
            txt_remaintime.setText(minute+":"+second);
        }

    }
}

Again full source code is available at Download

Njoy!!


3 comments:

  1. Helped to solve my problem. Thanks.

    ReplyDelete
  2. Thank u so much...nice tutorial....

    ReplyDelete
  3. I want to pause timer and resume from last pause time.....help me

    ReplyDelete