I'm trying to implement a Timer counter, but all that I see is CountDown and I don't want to count down time, I just want start from 00:00 and every second sum one second until 60:00, I've been trying to implement this :
1.-Android timer? How? - Answer
2.- Chronometer
And they didn't help me at all.. can you guide me how to start time from 00:00 and end in 60:00?
Answer here :
As Usama Zafar put his answer, I updated the code to control this CountDownTimer in case you'll need it...
I created
CountDownTimer cdtTimer;
Then the method I have to startTimer() that starts to count the time it's like this :
private void StartTimer(){
final long EndTime = 3600;
cdtTimer = new CountDownTimer(EndTime*1000, 1000) {
public void onTick(long millisUntilFinished) {
long secondUntilFinished = (long) (millisUntilFinished/1000);
long secondsPassed = (EndTime - secondUntilFinished);
long minutesPassed = (long) (secondsPassed/60);
secondsPassed = secondsPassed%60;
tvCounterTimer.setText(String.format("%02d", minutesPassed) + ":" + String.format("%02d", secondsPassed));
}
public void onFinish() {
tvCounterTimer.setText("done!");
}
}.start();
}
Then wherever I want to cancel this CountDownTimer I simply do
cdtTimer.cancel();
The simplest way to implement Timer is to obviously use CountDown as stated on Android's official guide:
new CountDownTimer(30000, 1000) {
public void onTick(long millisUntilFinished) {
mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
}
public void onFinish() {
mTextField.setText("done!");
}
}.start();
But as per your requirement you don't want to count down rather you wish to count up until you reach 60min mark. How can we do it is indeed an interesting question. You can try implementing a Runnable and get system time in milliseconds on each tick using: System.currentTimeMillis().
It tends to get complex right? So why not play with the CountDown Timer and make it work to our needs? How can we do it? Observe the below code:
long StartTime = 0; //Starting from 00:00
long EndTime = 3600; //End at min:sec converted to seconds
new CountDownTimer(EndTime*1000, 1000) {
public void onTick(long millisUntilFinished) {
long secondUntilFinished = (long) (millisUntilFinished/1000);
long secondsPassed = (EndTime - secondUntilFinished);
long minutesPassed = (long) (secondsPassed/60);
secondsPassed = secondsPassed%60;
// So now at this point your time will be: minutesPassed:secondsPassed
mytextView.setText(String.format("%02d", minutesPassed) + ":" + String.format("%02d", secondsPassed));
}
public void onFinish() {
mTextField.setText("done!");
}
}.start();
The code is very easy to understand. If you still have any queries comment them and I will address them. Hope this is helpful.
Related
I have to show countdown in my app,
i getting two timestamp
1. startTime
2. endTime
please help to show countdown timer.
Thank you
You can use CountdownTimer like this. It uses milliseconds, for example this uses 1 second interval(1000 milliseconds) as it counts down.
int input = Integer.valueOf(String.valueOf(mCount_Entry.getText())) * 1000;
CountDownTimer countDownTimer = new CountDownTimer(input, 1000) {
#Override
public void onTick(long millisUntilFinished) {
mDisplay.setText("seconds remaining: " + millisUntilFinished / 1000);
}
#Override
public void onFinish() {
mDisplay.setText("Countdown is over, boom*****");
}
}.start();
So i have here Quiz App and have timer. So what i want to happen for example i have set the timer for 15 seconds and if the user answer the question in 5 seconds i want the 10 seconds ramaining become 10 points and it will add to previous score plus the score of you will get upon answering the questions. so for now i have this ...
if(savedInstanceState!=null){
//saved instance state data
int exScore = savedInstanceState.getInt("score");
scoreText.setText("Score: "+exScore);
}
Timer = new CountDownTimer(15000, 1000) {
#Override
public void onTick(long millisUntilFinished) {
tv_time.setText("" + millisUntilFinished / 1000);
int progress = (int) (millisUntilFinished / 150);
progressBar.setProgress(progress);
}
#Override
public void onFinish() {
progressBar.setProgress(0);
timeUp(context);
}
}.start();
and here is the one for onclick. if the user answer correctly it will add 10points automatically
public void onClick(View view) {
Button clicked = (Button) view;
int exScore = getScore();
if (clicked.getText().toString().equals(this.active_question.getAnswer()) ) {
if (this.questions.size() > 0) {
setQuestion(questions.poll());
scoreText.setText("Score: " + (exScore + 10))
} else {
CustomGameOver cdd = new CustomGameOver(PlayQuizActivity.this);
cdd.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
cdd.show();
setHighScore();
Timer.cancel();
}
}
I dont have any idea on how to get the remaianing time on CountdownTimer and add it as a score when the answer is correct. could anyone please help me please.
Just use millisUntilFinished from onTick of the CountDownTimer
And the bonus will be millisUntilFinished/1000
P.S I think you better use a lower interval than 1000, so the ProgressBar will seem smoother.
All you need to do is declare a long variable timeleft in your MainActivity.
long timeleft;
Then, when you create a new Timer, set the "onTick" override to update the timeleft variable each "onTick" (which in the following example is 1000 milliseconds )
timer = new CountDownTimer(time, 1000) {
#Override
public void onTick(long millisecondsUntilFinished) {
timeleft = millisecondsUntilFinished;
}
}
Your app can access then the variable timeleft every time you need to check how much time is left.
score = score + timeleft / 1000; // divide by 1000 to get seconds
Have in mind that if you need to update the timer, you have to cancel it and create a new timer with the updated time left (and the same override);
timeleft = timeleft + bonustime; // (if you want to add bonus time, remember has to be in milliseconds)
if( timer != null){ timer.cancel();} // better check first if the timer exists
timer = new CountDownTimer(timeleft, 1000) {
#Override
public void onTick(long millisecondsUntilFinished) {
timeleft = millisecondsUntilFinished;
}
I have an edit text for entering a time in minutes, a button to start a countdown timer using the minutes entered in the edit text which updates a text view, and a button which I want to cancel the countdown timer before it finishes.
The problems I'm having are...the variable the countdown timer needs (the milliseconds) can't get assigned until I click the start timer button because it's coming from an edit text. So I created the countdown timer inside of the onClick for the start timer button. However...I want another button to cancel the timer...and it doesn't have access to the countdown timer. So that didn't solve anything and I'm confused at how to do this. Any advice would help.
Edit: I just realized I could move the stop timer button with the listener to the start timer button onClick also...under the countdown timer. I'm just confused about how the countdown timer could find the milliseconds variable if I have countdown timer outside of the start timer onClick. I guess moving the stop timer under countdown timer works...but still feels like I'm missing something.
Edit2: I tried moving countDownTimer into onCreate and making milliseconds global...but I have to make countDownTimer final in order for the buttons to be able to use it, and countDownTimer just skips to onFinish.
mTimerTextView = (TextView)findViewById(R.id.timer_text_view);
mTimerEditText = (EditText)findViewById(R.id.timer_edit_text);
mStartTimerButton = (Button)findViewById(R.id.btn_start_timer);
mStartTimerButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Getting time in minutes from edit text
int timeEntered = Integer.parseInt(mTimerEditText.getText().toString());
long milliseconds = timeEntered * 60000;
CountDownTimer countDownTimer = new CountDownTimer(milliseconds, 1000) {
#Override
public void onTick(long millisUntilFinished) {
int seconds = (int) (millisUntilFinished / 1000) % 60;
int minutes = (int) ((millisUntilFinished / (1000*60)) % 60);
int hours = (int) ((millisUntilFinished / (1000*60*60)) % 24);
mTimerTextView.setText(String.format("%02d:%02d:%02d", hours, minutes, seconds));
}
#Override
public void onFinish() {
new AlertDialog.Builder(DailyGoalActivity.this)
.setTitle("Time's Up")
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
}).show();
}
};
countDownTimer.start();
}
});
mStopTimerButton = (Button)findViewById(R.id.btn_stop_timer);
mStopTimerButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// countDownTimer.cancel(); <- How could I do this?
}
});
there are two possible reason for your problem first one is that you need to create countdown timer in onCreate and second one is that you are stopping null countdown timer.
solution-1 you can use a boolean variable to check that timer has start or not and then start timer accordingly on button click.Apart from this initialize timer in on create and start in on click follow this code please follow this link
solution-2 to stop the timer use this code
if(countDownTimer != null) {
countDownTimer .cancel();
countDownTimer = null;
}
Isn't the simple solution to declare CountDownTimer variable globally?
Move this code to onCreate
CountDownTimer countDownTimer = new CountDownTimer(milliseconds, 1000) {
#Override
public void onTick(long millisUntilFinished) {
int seconds = (int) (millisUntilFinished / 1000) % 60;
int minutes = (int) ((millisUntilFinished / (1000*60)) % 60);
int hours = (int) ((millisUntilFinished / (1000*60*60)) % 24);
mTimerTextView.setText(String.format("%02d:%02d:%02d", hours, minutes, seconds));
}
}
Then start timer in Start button's onClick and cancel in Stop button's onClick.
Hope it helps.
I have one CountDownTimer and i want to do an action for every two wasted seconds.
countdowntimer = new CountDownTimer(10000, 1) {
#Override
public void onTick(long millisUntilFinished) {
}
#Override
public void onFinish() {
}
}.start();
For example when it starts time remaining = 10 seconds
When time remaining = 8 seconds I want to do an action
When time remaining = 6 seconds I want to do an action
and so on......
Just check if it is divisible by 2.
#Override
public void onTick(long millisUntilFinished) {
long seconds = millisUntilFinished/1000;
if ((seconds % 2) == 0)
{ // even number--do some action }
}
This is assuming you are calling the onTick() every second like
countdowntimer = new CountDownTimer(10000, 1000) {
This is probably preferable for you
Setting it up to call every 2 seconds should also work and better but leaving the original answer in case someone needs to call onTick() more often
countdowntimer = new CountDownTimer(10000, 2000) {
then you could just do the action with every call to onTick()
CountDownTimer Docs
i m creating an online examination app.
this app require to show elapsed time from 1h 30min.
what should i take timer or what?
elapsetime = (TextView)findViewById(R.id.txtElapsedTime);
elapsetime.setText(" "+hour+" h "+min+" m");
any suggestions?
Use CountDownTimer.
Use this : Example of showing a 30 second countdown
new CountDownTimer(30000, 1000) {
public void onTick(long millisUntilFinished) {
mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
}
public void onFinish() {
mTextField.setText("done!");
}
}.start();
stop the Counter we use the countDownTimer.cancel()
if you want to Pause and Resume the timer refer below link
http://example.javamonday.com/Open-Source/Android/Timer/multitimer-android/com/cycleindex/multitimer/CountDownTimerWithPause.java.htm
Refference : http://developer.android.com/reference/android/os/CountDownTimer.html