3

I'm working with android studio to an app that have a countdown that starts from 10 seconds. I wrote the code and it works fine but it shows only the seconds remaining, now i want the countdown show the milliseconds too. Can you help me please? Here's the code

public class MainActivity extends Activity {
TextView txtCount;
Button btnCount;
int count = 0;


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

    txtCount = (TextView)findViewById(R.id.textView1);
    txtCount.setText(String.valueOf(count));
    btnCount = (Button)findViewById(R.id.button1);

    btnCount.setOnClickListener(new View.OnClickListener() {
        public void onClick(View arg0) {
            count++;
            txtCount.setText(String.valueOf(count));
        }
    });
    final TextView textic = (TextView) findViewById(R.id.textView2);

    CountDownTimer Count = new CountDownTimer(10000, 1000) {
        public void onTick(long millisUntilFinished) {
            int seconds = (int) ((millisUntilFinished / 1000));

            textic.setText(seconds + "seconds " + millisUntilFinished / 1000);

        }

        public void onFinish() {
            textic.setText("TEMPO SCADUTO");
        }
    };

    Count.start();



}
Rick
  • 3,943
  • 6
  • 33
  • 45
  • Similar question here: http://stackoverflow.com/questions/14669752/android-countdown-timer-display-milliseconds – GVillani82 Sep 27 '13 at 21:47

4 Answers4

4
//start button click
     CountDown timer = new CountDown(180000, 1000);
     timer.start();

//stop button click
timer.stop();   

    //countdown class
    public class CountDown extends CountDownTimer {

          public CountDown(long millisInFuture, long countDownInterval) {
             super(millisInFuture, countDownInterval);
          }

          @Override
          public void onTick(long millisUntilFinished) {
             long ms = millisUntilFinished;
             String text = String.format("%02d\' %02d\"",
                                         TimeUnit.MILLISECONDS.toMinutes(ms) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(ms)),
                                         TimeUnit.MILLISECONDS.toSeconds(ms) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(ms)));
             textViewTimer.setText(text);
          }

          @Override
          public void onFinish() {
             textViewTimer.setText("ffinish");
          }
       }
msevgi
  • 4,828
  • 2
  • 24
  • 30
3

I've implemented countdown with seconds and milliseconds considering also CountDownTimer's issue with skipping one second (https://stackoverflow.com/a/6811744/1225669). It works for me.

private static final long NUMBER_MILLIS = 20000;
private static final String MILLISECONDS_FORMAT = "%03d";
private int secondsLeft = 0;

//
new CountDownTimer(NUMBER_MILLIS, 1) {

        public void onTick(long millisUntilFinished) {                    

                if (Math.round((float)millisUntilFinished / 1000.0f) != secondsLeft)
                {
                    secondsLeft = Math.round((float)millisUntilFinished / 1000.0f);
                }
                long roundMillis = secondsLeft * 1000;
                if(roundMillis==NUMBER_MILLIS){
                    tvTimer.setText(secondsLeft
                            + "." + String.format(MILLISECONDS_FORMAT, 0));
                }else {
                    tvTimer.setText(secondsLeft
                            + "." + String.format(MILLISECONDS_FORMAT, millisUntilFinished % 1000));
                }
        }

        public void onFinish() {

            tvTimer.setText("done!");
        }
}.start();
Community
  • 1
  • 1
valerybodak
  • 4,195
  • 2
  • 42
  • 53
2

Use remainder operator,

millisUntilFinished % 1000

Write something on the lines of

textic.setText(seconds + "seconds " + millisUntilFinished / 1000 + " ms" + 
millisUntilFinished / 1000);

Although in your case, it will always return a 0 since your timer is ticking every 1000 milliseconds. To get a better picture, experiment with smaller ticker time values like 100 ms or 250 ms.

CountDownTimer Count = new CountDownTimer(10000, 100){...
Naeem A. Malik
  • 995
  • 4
  • 19
0

Below is a snippet of the countdowntimer class with the format display = "00:0" (seconds:milliseconds)

  mCountDownTimer = new CountDownTimer(mTimeLeftInMillis, 1)  

  private void updateCountDownText() {
          int seconds = (int) (mTimeLeftInMillis / 1000) % 60;
          int miliseconds = (int) (mTimeLeftInMillis / 100) % 10;
          String timeLeftFormatted = String.format(Locale.getDefault(), "%02d:%01d", seconds, miliseconds);
          TextView_Countdown.setText(timeLeftFormatted);