Basically, I have a count down timer right? And I need to display the time left for the timer in text. I have done that, but the timer's time is a bit off, as it needs to convert the seconds left into hours and minutes, like on a regular digital watch where it says 00:05:00 and it counts down to 00:04:59. I've done a lot of things today and my head is hurting quite a bit at the moment, so I can't exactly think about it. So my guess is that I need to use multiples of 60. Help?
Code:
int timeinminutes=1;
new CountDownTimer(timeinminutes*100000, 1000) {
TextView mTextField = (TextView) findViewById(R.id.textView1);
public void onTick(long millisUntilFinished) {
long hrs=0;
long mnts=0;
long scnds=0;
scnds=(millisUntilFinished/1000);
if (scnds>59) {
mnts=(scnds/60);
if (mnts!=Math.floor(mnts)) {
mnts=0;
}
}
if (mnts>59) {
hrs=(mnts/60);
if (hrs!=Math.floor(hrs)) {
hrs=0;
}
}
mTextField.setText(hrs + ":" + mnts + ":" + scnds);
}
public void onFinish() {
mTextField.setText("00:00:00");
}
}.start();
A lot of this has already been done. If you read about SimpleDateFormat it should help you get on your way to solving this.
You can find more information here as well.
Check out java.util.Timer here.
These should solve any of your problems.
You should have a separate class that does the count-down and keeps the remaining time in milliseconds as a field. The class could have a method that launches a Thread which sleeps 1000 milliseconds, then subtracts 1000 from the original time in milliseconds until the remaining time is 0.
You then could use another Thread which updates the TextView every 1000 milliseconds and a SimpleDateFormat to get the remaining time from the countdowntimer object and format it.
Related
I am trying to make a stopwatch and I have two questions.
First problem: I want to make it with plus and minus buttons, where minutes and seconds will look like this: [0:00]. What should I do, if I want to update the TextView after tapping the "add 5s button"? It should change to [0:05], not to [5].
Second problem: What should I do after setting the stopwatch to 60 seconds? Currently, TextView looks like this: [0:60] but I want to make it look like this:[1:00].
public void decrease3Integer(View view) {
czas=czas-5;
display2(czas);
}
public void increase3Integer(View view) {
czas=czas+5;
display2(czas);
}
private void display2(int time){
TextView display2Integer=(TextView) findViewById(R.id.textView21);
display2Integer.setText(""+time);
}
Try this in display2():
String formattedTime = String.format("%d:%02d", time / 60, time % 60);
display2Integer.setText(formattedTime);
Edit: how to make sure that seconds don't fall bellow zero?
public void decrease3Integer(View view) {
if (czas >= 5) {
czas=czas-5;
display2(czas);
}
}
First problem :
You can use 2 variables int minutes and int seconds , the right side of the clock will be linked to the variable seconds and left one to the variable minutes
When you click on add it is the variable seconds that will increase by 5
Second problem :
When you increment the variable seconds , make sure to do it this way seconds = ( seconds + added_seconds ) % 60 so it will never go beyond 60 , but in the other side , each time the seconds variable reachs 0 , the minute should increment by one , you can increment the seconds with a method like this :
public void increment_seconds(int added_seconds) {
seconds = seconds + added_seconds ;
if ( seconds >= 60 ) { minutes++ ; } // check if we're gone beyond 60
seconds = seconds % 60 ; // making seconds on their normal value
}
So I´m writing a little egg timer for learning purpose. And actually everything works fine without that the onTick method in the CountDownTimer is only calling once at the begining and I don´t get why. Actually in the documentation it´s stated that if the action called in the method takes too long ticks can be skiped but i dont think that these calculations take longer than 60 min which is the max time usable. So the timer is ticking once like if input 30 min the display widget will show 29:59.
new CountDownTimer(timer,timer){
#Override
public void onTick(long l) {
long help = (l - (l%60000))/60000;
String help2 = "" + help;
Log.i("Minuten übrig", help2);
long help3 = (l % 60000)/1000;
String help4 = "" + help3;
Log.i("Sekunden übrig", help4);
String prov = help + ":" + help3;
display.setText(prov);
}
#Override
public void onFinish() {
button.setText("Start");
}
}.start();
Try replacing this :
new CountDownTimer(timer,timer)
with this:
new CountDownTimer(timer,1000)
You can put other value in place of 1000 but make sure it is less than timer value, else onTick will get called only once.
Also, check out Chronometer if your task is suited to this.
If I use a one minute interval as shown below, the speech function doesn't work the second time it's called. If I use a shorter, ie 30 second interval, then it works as intended. I think this has something to do with the tts engine not being ready. Any ideas? I've tested this with longer examples and the problem seems to be somewhere in the if statement. For example, I've tried 4 minute examples and it will say "4 minutes remain, 3 minutes remain, 2 minutes remain, ..., Countdown Complete".
new CountDownTimer(120000, **60000**) {
public void onTick(long millisUntilFinished) {
long minutesLeft = (((millisUntilFinished + 60000) / 1000) / 60);
if (minutesLeft==1){
speech(minutesLeft + " minute remains");
} else{
speech(minutesLeft + "minutes remain");
}
}
public void onFinish() {
speech("Countdown Complete");
}
}.start();
private void speech(String toSpeak) {
float pitch, speed;
pitch=1;
speed=1;
engine.setPitch(pitch);
engine.setSpeechRate(speed);
engine.speak(toSpeak, TextToSpeech.QUEUE_FLUSH, null, null);
}
It turns out that all I had to do was add an extra half second to my total time. So changing 120000 to 120500 fixed the problem.
I am trying to implement a CountDownTimer to post a visual text count-down until I get to 0, then send a specific message. Everything works fine until I get to 3 seconds, it calls the my done() method twice; once with 3 seconds left and once when the counter is done.
I am certain that the only time done() is called is when onFinish() in this CountDownTimer() is called. I have no clue why it is calling twice, and at consistently the same time.
My Java Code:
mainCounter = new CountDownTimer(30000, 1000){
#Override
public void onTick(long millisUntilDone){
int hours = (int) (millisUntilDone/3600000);
int mins = 0, secs = 0;
long timeLeft = millisUntilDone % 3600000;
if((int)timeLeft != 0){
mins = (int) (timeLeft/60000);
}
long timeLeftStill = timeLeft % 60000;
if((int)timeLeftStill != 0){
secs = (int) (timeLeftStill/1000);
}
((TextView)findViewById(R.id.timer_label)).setText("You have " + hours + " hours, " + mins + " mins, " + secs + " secs left");
}
#Override
public void onFinish() {
// Send Message, Out of Time
((TextView)findViewById(R.id.timer_label)).setText("MESSAGE!");
num++;
done(); //My Own Method
this.cancel();
}
}.start();
I have another counter that works perfectly, I have commented it out to ensure it doesn't affect this counter, and like I say this one works fine until I get to 3 second left.
For you problem concerning the call of onFinish() method when 3 seconds are left, you should read the answer here. Actually you should read all the answers on Android: CountDownTimer skips last onTick()! question. It will hep you understand the reason. Talking abou that thread, usually people have had problems where finish gets called approx. 1 second before the estimated time. For them it can be understood ( from the answer referenced) that at the start of every tick, before onTick() is called, the remaining time until the end of the countdown is calculated. If this time is smaller than the countdown time interval, onTick is not called anymore. But the answer there also states that there may be other processes in the background that delay the thread running CountDownTimer plus that Android itself will probably create a small delay when calling the message handler of CountDownTimer, so your undesired delay might be becauase of all that.
Few workaround have been suggested in the answers. One is either decreaseing the interval time or increasing the maximum countDownTimer value. There is also one alternative of countDownTimer method. Hope this helps you understand the possible reason and will aid you to do the workaround.
You can do like this
private int secondsLeft = 0;
private int minutes;
private int seconds;
private String timeString;
counter = new CountDownTimer(30000, 100) {
public void onTick(long ms) {
if (Math.round((float) ms / 1000.0f) != secondsLeft) {
secondsLeft = Math.round((float) ms / 1000.0f);
minutes = (secondsLeft % 3600) / 60;
seconds = secondsLeft % 60;
timeString = String.format("%02d:%02d", minutes, seconds);
textViewTimer.setText(timeString);
}
}
public void onFinish() {
textViewTimer.setText("00:00");
}
}.start();
K So i am making a timer with CountDownTimer. it is my first app i am doing and i am already running into a problem. First my timer is skipping the number 14? no clue why. and second, I dont know how to set the proper format. "mm:ss" My biggest issue is setting format. I need to have it so when i get to 8 seconds left it read 00:08 etc. and when i have minute 15 left it read 1:15, and not 00:75 or 1:75. Its been a long day trying to figure this out. Thanks in advance.
private void walk() {
new CountDownTimer(15000, 1000) {
#Override
public void onFinish() {
lapCounter++;
lapNumber.setText("Lap Number: " + lapCounter);
run();
}
#Override
public void onTick(long millisUntilFinished) {
text.setText("Time left:" + millisUntilFinished/1000);
}
}.start();
you may want to use joda-time, see this post for details