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();
Related
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.
can someone help me on how to minus or subtract the time in my countdown timer? for example the countdown timer is running and when i click a button it will be diminished by 3, but it will still running. please help me, thank a lot in advance.
here is what i have ...
new CountDownTimer(30000, 1000) {
public void onTick(long millisUntilFinished) {
mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
}
public void onFinish() {
mTextField.setText("done!");
}
}.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;
}
Can anyone demonstrate how to display only the seconds part of the digital clock widget.
Schedule a countdown until a time in the future, with regular notifications on intervals along the way. Example of showing a 30 second countdown in a text field:
new CountdownTimer(30000, 1000) {
public void onTick(long millisUntilFinished) {
mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
}
public void onFinish() {
mTextField.setText("done!");
}
}.start();
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