How to start chronometer in reverse in android?-ThrowExceptions

Exception or error:
cmtr.setText(finalTime);
cmtr.setBase(SystemClock.elapsedRealtime());

timetest = SystemClock.elapsedRealtime();
Log.d("SETTIME: ", ""+timetest);
cmtr.start();

eltime = SystemClock.elapsedRealtime();
Log.d("ELapsed: ", ""+eltime);

Note: i want to start my chronometer in reverse order. like i set chronometer 10 seconds. now, i want to start from 10 to 0 seconds in reverse order. so can anyone help to get this solution.? Thank you so much in advance.

How to solve:

You can’t, the Chronometer widget only counts up, that’s the specific purpose it was made for. If you want to count down, use the CountDownTimer (the Android SDK page contains a specific example where a TextView is updated), or roll your own solution.

These classes are trivial wrappers to save you some typing. You really shouldn’t feel uncomfortable writing an alternative implementation if they donĀ“t fit your exact needs.

[Update]

As Ronaldo Bahia added in the remarks, since API 24 the Chronometer actually offers this functionally through the setCountDown method.

###

You can’t chronometer widget for counts down. use CountDownTimer example below
here my count down start from 01:45 mins 1sec = 1000

 CountDownTimer cT =  new CountDownTimer(100000, 1000) {

         public void onTick(long millisUntilFinished) {


                 String v = String.format("%02d", millisUntilFinished/60000);
                 int va = (int)( (millisUntilFinished%60000)/1000);
                 textView.setText("seconds remaining: " +v+":"+String.format("%02d",va));
         }

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

###

Choronometer only counts up. but we can do reverse with this small trick.

chrono.setOnChronometerTickListener(new Chronometer.OnChronometerTickListener() {
            @Override
            public void onChronometerTick(Chronometer arg0) {
                countUp = (SystemClock.elapsedRealtime() - startTime) / 1000;

                if (countUp % 2 == 0) {
                    chronoRecordingImage.setVisibility(View.VISIBLE);
                } else {
                    chronoRecordingImage.setVisibility(View.INVISIBLE);
                }
                int min = (int) (countUp/60);
                int sec = (int) (countUp%60);

                Utility.printMessage(min+":"+sec);
                int newCount = MAX_TIME - (int) countUp;  // MAIN LOGIC
                String asText = String.format("%02d",(newCount / 60)) + ":"
                        + String.format("%02d", (newCount % 60));

                chrono.setText(asText);
                if (countUp > MAX_TIME) {
                    //time complete
                }
            }
        });

###

For those who are still looking for other options and a View, I suggest using the Chronometer widget and setting setCountDown (boolean) to true.

As an example, in your activity or fragment:

  view_timer.base = SystemClock.elapsedRealtime() + 10000    
  view_timer.start()

For the widget in the layout .xml:

<Chronometer
            android:id="@+id/view_timer"
            android:countDown="true"
            tools:targetApi="24"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>

###

Also possible to use setCountDown(true);
https://developer.android.com/reference/android/widget/Chronometer.html

###

private static int sec = 60*2;  // this is for 2 for min TODO replace your sec

    public void startTimer(final TextView textView ,int sec) {

    CountDownTimer downTimer= new CountDownTimer(1000 * sec, 1000) {

        public void onTick(long millisUntilFinished) {


            String v = String.format("%02d", millisUntilFinished / 60000);
            int va = (int) ((millisUntilFinished % 60000) / 1000);
            textView.setText(v + ":" + String.format("%02d", va));
        }

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

}

and call this :

startTimer(tvClock,sec);

Leave a Reply

Your email address will not be published. Required fields are marked *