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
Related
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.
Bit of a question i dont really know how to formulate. Making an android app in android studio using java and I want to display a double taken from a textview above (the result of a calculation done with user input) instead of the amount of minutes left on the countdown while still using the same amount of time counting it down.
Say for example the User puts in 4 hours and the value from the double is 12, I want 12 to gradually decrease over the course of 4 hours.
Here is the code
InsulinButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String text = InsuEditText.getText().toString();
if (!text.equalsIgnoreCase("")) {
final int hours = Integer.valueOf(text);
final CountDownTimer TotalTid = new CountDownTimer(hours * 3600000, 60000)
{
#Override
public void onTick(long millisUntilFinished) {
long start_time = System.currentTimeMillis();
double TimePassed = System.currentTimeMillis() - start_time;
The line below is where the issue is. I need this to display insulindose being gradually decreased over x amounts of hours I know i should do insulindose -(insulindose /countdowntime * timepassed) but it doesn't work, just tells me "Operator cannot be applied to 'java.lang.String', 'long'
AktivInsulinTextView.setText("Aktiv insulin: " + insulindose - ;
}
#Override
public void onFinish() {
AktivInsulinTextView.setText("Ingen aktiv insulin");
}
}.start();
}
}
});
Because the first thing you pass into the operation is a String ("Aktiv insulin: "), java assumes that it should do string concatenation and the - operator doesn't make sense in a string concatenation. Simply put your math in parenthesis and it should work:
AktivInsulinTextView.setText("Aktiv insulin: " + (insulindose -(insulindose /countdowntime * timepassed)));
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.
please i am a beginner at android and i'm trying to add a CountDownTimer to my quizz , well the CountDownTimer works well and when i answer all questions in the time limit i get my results like this :
Corrects Answers : 14
Wrong Answers : 16
Score : 14/30
In case i don't catch time i get something like this :
Corrects Answers : 14
Wrong Answers : 0
Score : 14/30
Which is correct in my opinions because in that case answers are not wrong but unanswered
But if it happens that i answer all questions before the time finishes .I get the results as i want but the Count doesn't stop and keeps on running until 0 and on 0 it shows that afterward :
Corrects Answers : 0
Wrong Answers : 0
Score : 0/30
Here is the code for the timer :
mCountDownTimer = new CountDownTimer(90000, 1000) {
public void onTick(long millisUntilFinished) {
textViewtimer.setText("Seconds remaining: " + millisUntilFinished / 1000);
}
public void onFinish() {
marks = (correct+wrong)-wrong;
Intent resultintent = new Intent(getApplicationContext(),
ResultsActivity.class);
startActivity(resultintent);
mCountDownTimer.cancel();
}
}.start();
Though i'm calling the Intent part somewhere else too to lead me to the score page
Please can anyone give me a hint or help?
I think your logic on implementing onFinish is wrong. onFinish getting called after your timer has been done or you called cancel.
You should move the mCountDownTimer.cancel(); to the last question (probably), and it will call onFinish automatically.
In your current code, your timer will be canceled only if the timer has been done, because you called cancel in the onFinish.
please use following code.
// Global variable. Put
private boolean isRunning = true;
while(isRunning)
{
textViewtimer.setText("Seconds remaining: " + millisUntilFinished / 1000);
if((millisUntilFinished / 1000) <= 0)
isRunning = false;
}
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.