Reputation:
I have an android app with a timer. By default, it is displayed as 00:00:00 (Hours, Minutes, Seconds). The user can press the H M S and drag the scrubber around the circle to change the hour, minute, or second, depending on which one is clicked. I am trying to create a CountDownTimer by parsing the number from the changeable TextViews for hours, minutes, and seconds, convert it all into seconds, and then start the timer; however, the problem is that is displays the total minutes instead of keeping it 59 and below. For example if I countdown for 3 hours. It displays 2 Hours, 179 Minutes and 59 Seconds. When I want it to be 2 Hrs. 59 Minutes, 59 Seconds.
I am not sure why I am receiving this error. I am getting all the right values in my code.
Here is my code for the setActionListener for my CountDownTimer in my class, I made a comment for where I think the error might be.:
private void setActionListeners() {
number_text = (TextView) findViewById(R.id.hour_progress_number);
minute_text = (TextView) findViewById(R.id.minute_progress_number);
second_text = (TextView) findViewById(R.id.second_progress_number);
start_timer.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// textViewShowTime.setTextAppearance(getApplicationContext(), R.style.normalText);
hourint = Integer.valueOf(number_text.getText().toString());
minuteint = Integer.valueOf(minute_text.getText().toString());
secondint = Integer.valueOf(second_text.getText().toString());
Log.i("YourActivity", "Hours: " + hourint);
Log.i("YourActivity", "Minutes: " + minuteint);
Log.i("YourActivity", "Seconds: " + secondint);
totalTimeCountInMilliseconds = ((hourint*60*60) +(minuteint*60) + (secondint)) * 1000; // time count for 3 minutes = 180 seconds
timeBlinkInMilliseconds = totalTimeCountInMilliseconds/1000;
countDownTimer = new CountDownTimer(totalTimeCountInMilliseconds, 500) {
// 500 means, onTick function will be called at every 500 milliseconds
@Override
public void onTick(long leftTimeInMilliseconds) {
long seconds = leftTimeInMilliseconds / 1000;
mSeekArc.setVisibility(View.INVISIBLE);
if ( leftTimeInMilliseconds < timeBlinkInMilliseconds ) {
// textViewShowTime.setTextAppearance(getApplicationContext(), R.style.blinkText);
// change the style of the textview .. giving a red alert style
if ( blink ) {
number_text.setVisibility(View.VISIBLE);
minute_text.setVisibility(View.VISIBLE);
second_text.setVisibility(View.VISIBLE);
// if blink is true, textview will be visible
} else {
number_text.setVisibility(View.INVISIBLE);
minute_text.setVisibility(View.INVISIBLE);
second_text.setVisibility(View.INVISIBLE);
}
blink = !blink; // toggle the value of blink
}
second_text.setText(String.format("%02d", seconds % 60));
//THE ERROR MIGHT BE HERE FOR THE MINUTE
minute_text.setText(String.format("%02d", seconds / 60));
number_text.setText(String.format("%02d", seconds / 3600)); // format the textview to show the easily readable format
}
@Override
public void onFinish() {
// this function will be called when the timecount is finished
//textViewShowTime.setText("Time up!");
number_text.setVisibility(View.VISIBLE);
minute_text.setVisibility(View.VISIBLE);
second_text.setVisibility(View.VISIBLE);
mSeekArc.setVisibility(View.VISIBLE);
}
}.start();
}
});
I have tried tampering around with that value and finding its value for a while, but it is a really persistent problem and just won't display properly.
It seems like I am doing everything correctly:
Countdown timer in HH:MM:SS format in Android
What exactly is the problem and how can it be fixed?
Here is my full code for the class if needed:
Upvotes: 2
Views: 1761
Reputation: 39698
Your error, as you suspected, is here.
minute_text.setText(String.format("%02d", seconds / 60));
You need to subtract out the number of hours. You could also do the modulus operation on the remainder with your formatting, as follow
minute_text.setText(String.format("%02d", (seconds / 60) % 60));
A better way to do this would be to calculate the values ahead of time, and then just do the formatting on the occupied values.
int just_hours = seconds / 3600;
int just_minutes = (seconds/60) % 60;
int just_seconds = seconds % 60;
Upvotes: 2
Reputation: 1746
Use this code to get the right amout of minutes, seconds and hours:
//Count down one Minute
if (secondsLeft > 0) {
secondsLeft--;
}
// Count down one Minute
else if (secondsLeft == 0 && minutesLeft != 0) {
secondsLeft = 59;
minutesLeft--;
}
// Count down one Hour
else if (secondsLeft == 0 && minutesLeft == 0) {
hoursLeft = 59;
minutesLeft = 59;
secondsLeft--;
}
You just have to initialize secondsLeft, minutesLeft and hoursLeft to the selected Value of seconds, minutes and hours. The countDownTimer should tick every 1000 ms.
Upvotes: 0
Reputation: 95
minutes = seconds % 60;
% sign represents mod operation in java, it should be same in android.
Upvotes: 1