0

I have a count down timer that is meant to reading a time and stop at a particular time. show use time and remaining time. I have codded a sample in my activity. But the function that is suppose to help me display in 00:00:00 format does not work well. It only displays it in that format when the timer as stoped.

    public class PracticeQuestionActivity extends SherlockActivity implements OnClickListener {

private long timeElapsed;
    private boolean timerHasStarted = false;

    private final long startTime = 50000;
    private final long interval = 1000;

    MyCountDownTimer countdown = null;
    TextView timerText = null, ElaspedTime = null;

    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.practicequestion);

        context = PracticeQuestionActivity.this;

        initializeComponents(context);
        countdown = new MyCountDownTimer(startTime,interval);
        this.controlTimer(); //This is going to start the timer
    }

    private void controlTimer(){

        if (!timerHasStarted)
        {
            countdown.start();
            timerHasStarted = true;            
        }
    else
        {
            countdown.cancel();
            timerHasStarted = false;            
        }
    }

    //This is going to format the time value from duration in second
    private String setTimeFormatFromSeconds(long durationSeconds){

        return String.format("%02d:%02d:%02d", durationSeconds / 3600, (durationSeconds % 3600) / 60, (durationSeconds % 60));
    }


        //This method is going to be used to initialize the components of the view
    private void initializeComponents(Context context){

        timerText = (TextView)findViewById(R.id.timer);
        ElaspedTime = (TextView)findViewById(R.id.timeElapsed);

    }

     // CountDownTimer class
      public class MyCountDownTimer extends CountDownTimer
          {

              public MyCountDownTimer(long startTime, long interval)
                  {
                      super(startTime, interval);
                  }

              @Override
              public void onFinish()
                  {
                      timerText.setText("Time's up!");
                      ElaspedTime.setText("Time Elapsed: " + setTimeFormatFromSeconds(startTime));
                  }

              @Override
              public void onTick(long millisUntilFinished)
                  {
                      timerText.setText("Time remain:" + millisUntilFinished);
                      timeElapsed = startTime - millisUntilFinished;
                      ElaspedTime.setText("Time Elapsed: " + String.valueOf(timeElapsed));
                  }
          }

    }
skliz4rel
  • 198
  • 2
  • 11

1 Answers1

0

SimpleDateFormat is your friend:

private SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");

@Override 
public void onFinish() {
     timerText.setText("Time's up!");
     ElaspedTime.setText("Time Elapsed: " + timeFormat.format(startTime);
}

@Override
public void onTick(long millisUntilFinished) {
    timerText.setText("Time remain:" + timeFormat.format(millisUntilFinished));
    timeElapsed = startTime - millisUntilFinished;
    ElaspedTime.setText("Time Elapsed: " + timeFormat.format(timeElapsed));
}
FunkTheMonk
  • 10,908
  • 1
  • 31
  • 37