3

I have defined a chronometer;

protected Chronometer chrono;
protected int baseTime;
protected int stopTime;
protected long elapsedTime;

My program asks questions to the user and i want to set a timer based on the user's input. I want a timer which starts at 10 to 0. How can i do that?

Also, I want to show the remaining time on the screen.

serv-inc
  • 35,772
  • 9
  • 166
  • 188
temre
  • 189
  • 1
  • 3
  • 15

1 Answers1

8

Use CountDownTImer instead,

 new CountDownTimer(10000, 1000) { //Sets 10 second remaining

 public void onTick(long millisUntilFinished) {
     mTextView.setText("seconds remaining: " + millisUntilFinished / 1000);
 }

 public void onFinish() {
     mTextView.setText("done!");
 }
}.start();

Note : It's not possible to start Chronometer reversely because the Chronometer widget only counts up.

Edit: From API level 24, it is possible to perform count down using chronometer via help of Chronometer#setCountDown(true) method.

jack jay
  • 2,493
  • 1
  • 14
  • 27
Shree Krishna
  • 8,474
  • 6
  • 40
  • 68
  • Thank you sir! So, can i show it on the screen and set a position, size for CountDownTimer? – temre Jan 18 '16 at 15:33
  • @EmreŞen you'll be showing It to a `TextView`. Position easily wherever you like.. And you are welcome ! – – Shree Krishna Jan 18 '16 at 17:33
  • well you need to use chronometer in notifications unless you want to update push notifications every second yourself :/ and then you rely on android +24 – Kibotu Feb 19 '20 at 15:11