Saturday, June 7, 2014

Restart CountDownTimer

Refer to my old exercise of "Count Down Timer", it is a one-shot CountDownTimer. In some case you want to reset/restart the CountDownTimer, you have to cancel the old one if exist, otherwise both old and new CountDownTimer will run together.

Example:


MainActivity.java
package com.example.androidcountdowntimer;

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

public class MainActivity extends Activity {

 TextView myCounter;
 Button btnStart;
 CountDownTimer countDownTimer;

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

  myCounter = (TextView) findViewById(R.id.mycounter);
  btnStart = (Button) findViewById(R.id.start);
  btnStart.setOnClickListener(new OnClickListener() {

   @Override
   public void onClick(View v) {

    //cancel the old countDownTimer 
    if(countDownTimer!=null){
     countDownTimer.cancel();
    }
    
    countDownTimer = new CountDownTimer(10000, 1000) {

     @Override
     public void onFinish() {
      myCounter.setText("Finished!");
     }

     @Override
     public void onTick(long millisUntilFinished) {
      myCounter.setText("Millisecond Until Finished: "
        + String.valueOf(millisUntilFinished));
     }

    };
    
    countDownTimer.start();
   }
  });

 }

}

<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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context="com.example.androidcountdowntimer.MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:autoLink="web"
        android:text="http://android-er.blogspot.com/"
        android:textStyle="bold" />

    <Button
        android:id="@+id/start"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Start CountDownTimer" />
    <TextView
        android:id="@+id/mycounter"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

Next:
- Use CountDownTimer to do something repeatly, updating custom view; handle CountDownTimer in custom view.
- Another approach to use CountDownTimer to do something repeatly; handle CountDownTimer in MainActivity, custom view handle display only.

5 comments:

Anonymous said...

Thanks! Clean and working solution.

Anonymous said...

Thanks, very nice example :)

waffel said...

if I close the app the counter still runs until finished, which is good. But if I reopen the app while counting the onClick method won't update the new instance of the textView. Is there a way to get to make this work?

jatin Singh said...

Thanks, Nice Article and Clean and working code.

Unknown said...

hi
i just use your code. but its not restarting timer.please help me