I am trying to use the CountDownTimer
in Android to get a TextView
to count down from 100 to zero. I would like this to happen as quickly as possible while remaining visible.
At the moment if the CountDownTimer tick interval is any less than 500ms (I think it was that, might've been a bit lower) then the updates just don't happen.
I have only tried this on the emulator.
Am I going about this in the right way? If I am, is this apparent slowness a limitation of the emulator or just something that I will have to live with? If this is not the right way, can someone recommend a different approach?
Kindly check the following sample code both on emulator & device
package com.sample;
import android.app.Activity;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.widget.TextView;
public class SampleTimer extends Activity {
TextView tv; // textview to display the countdown
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
tv = new TextView(this);
this.setContentView(tv);
// 10000 is the starting number (in milliseconds)
// 1000 is the number to count down each time (in milliseconds)
MyCount counter = new MyCount(10000, 1000);
counter.start();
}
// countdowntimer is an abstract class, so extend it and fill in methods
public class MyCount extends CountDownTimer {
public MyCount(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
@Override
public void onFinish() {
tv.setText("done!");
}
@Override
public void onTick(long millisUntilFinished) {
tv.setText("Left: " + millisUntilFinished / 1000);
}
}
}
How are you doing the update at the moment, is it on the UI thread?
If you were counting up rather than down, you could use the Chronometer UI widget. That's not flexible enough to go backwards but perhaps you could use its source as a starting point.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With