How to Pause CountDownTimer in Android when onClick - android

I have a timer running for my IQ activity, and I want to Pause that timer when user click on Answer Button.
Bellow is my running timer and I have cancel timer when onlick but it's doesn't work for me:
timer = (TextView) findviewbyid(r.id.time)
new CountDownTimer(30000, 1000) {
public void onTick(long millisUntilFinished) {
timer.setText("seconds remaining: " + millisUntilFinished / 1000);
}
public void onFinish() {
mTextField.setText("done!");
}
}.start();
banana.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
CountDownTimer.cancel();
}
}

You can create a variable 'total' for total time and just set: total = millisUntilFinished in the onTick method. Check this answer: https://stackoverflow.com/a/6469166/2126403

Related

How to stop the CountDownTimer?

where should I edit this code, that i can stop the CountDown ?
How to delete the old one before I start the next new CountDown ?
public void MyCounter1(){
new CountDownTimer(60000, 1000) {
public void onTick(long millisUntilFinished) {
txt_timer.setText("Left time : " + millisUntilFinished / 1000);
}
public void onFinish() {
txt_timer.setText("done");
}
}.start();
}
here is code:
CountDownTimer timer= null;
public void MyCounter1(){
timer =new CountDownTimer(60000, 1000) {
public void onTick(long millisUntilFinished) {
txt_timer.setText("Left time : " + millisUntilFinished / 1000);
}
public void onFinish() {
txt_timer.setText("done");
}
};
timer.start();
}
// To stop & start new timer check not null of timer instance first then cancel existing timer & start new one
if(timer != null){
timer.cancel();
MyCounter1();
}
also if you want to cancel first instance and start new one you can add above lines in onFinish method which will be get called when timer finish his time.
check this:
CountDownTimer timer= null;
public void MyCounter1(){
timer =new CountDownTimer(60000, 1000) {
public void onTick(long millisUntilFinished) {
txt_timer.setText("Left time : " + millisUntilFinished / 1000);
}
public void onFinish() {
txt_timer.setText("done");
if(timer != null){
timer.cancel();
MyCounter1();
}
}
};
timer.start();
}
I did these 2 lines. I works at the time fine:
public void MyCounter1(){
CountDownTimer countDownTimer = new CountDownTimer(60000, 1000) {
public void onTick(long millisUntilFinished) {
if(my_condition=1){ //1- just make my_condition<>1 to stop the counter
txt_timer.setText("Left time : " + millisUntilFinished / 1000); }
}
public void onFinish() {
countDownTimer.cacel(); // 2- delete old one
txt_timer.setText("done");
}
}.start();
}
private int time = 10; //example 10 min.
CountDownTimer(60000 * time, 1000)
#Override
public void onFinish() {
txt_timer.setText("done");
cancel();
time = 0;

CountDownTimer onfinish method run after change activity

I use CountDownTimer in my code and when i run app i have one problem that is when i go to next level ( go to next activity ) the timer should be stop and dont run the onfinish method but when the first level timer down it run the onfinish method and go to activity GameOver .
I use Intent to move between my activity and my CountDownTimer is :
new CountDownTimer(timeoflevel, 1000) {
public void onTick(long millisUntilFinished) {
txtclock2.setText("Time: " + millisUntilFinished / 1000);
//here you can have your logic to set text to edittext
}
public void onFinish() {
txtclock2.setText("..Finish..");
Intent intent = new Intent(Play.this , GameOver.class);
startActivity(intent);
}
}.start();
Sorry for my easy question and thanks for reading .
CountDownTimer countDownTimer;
countDownTimer = new CountDownTimer(timeoflevel, 1000) {
public void onTick(long millisUntilFinished) {
txtclock2.setText("Time: " + millisUntilFinished / 1000);
//here you can have your logic to set text to edittext
}
public void onFinish() {
txtclock2.setText("..Finish..");
Intent intent = new Intent(Play.this , GameOver.class);
startActivity(intent);
}
}.start();
// and in OnStop
public void onStop(){
countDownTimer.cancel();
}
You should call countDownTimer.cancel() before starting next level. It will cancel the counDownTimer and will not call onFinish.
public void onStop(){
//Cancel your timer here before going to next activity.
}
CountDownTimer countDownTimer;
//in on Resume()
countDownTimer = new CountDownTimer(timeoflevel, 1000) {
public void onTick(long millisUntilFinished) {
txtclock2.setText("Time: " + millisUntilFinished / 1000);
//TODO perform operation
}
public void onFinish() {
txtclock2.setText("..Finish..");
Intent intent = new Intent(Play.this , GameOver.class);
startActivity(intent);
}
}.start();
// Cancel Timer in onPause()
public void in onPause(){
countDownTimer.cancel();
}

Android Countdown timer with double speed

I would like to create a countdown timer that starts at 10 but only takes 5 seconds to count down to 0.
I have this code below from Google Source Code that counts down from 10:
new CountDownTimer(10000, 1000) {
public void onTick(long millisUntilFinished) {
timerText.setText("seconds remaining: " + millisUntilFinished / 1000);
}
public void onFinish() {
timerText.setText("done!");
}
}.start();
You should actually try doing it yourself first, but:
new CountDownTimer(5000, 500) {
public void onTick(long millisUntilFinished) {
timerText.setText("Half-seconds remaining: " + millisUntilFinished / 500);
}
public void onFinish() {
timerText.setText("done!");
}
}.start();
The CountDownTimer has two parameters in its constructor, one for the length of the timer as a whole (called millisInFuture, the first parameter), and one for how often the onTick function is called (which is countDownInterval). Both of these parameters are of the long variable type.
Please see CountDownTimer in the Android API.

Stop watch in android withtimer starting from the top

I want a stop watch for an android quiz application but it should run from the top.I mean it should set the time for example 2 hrs for the application and then should come down from 2 hrs to 0 as the quiz moves on..How can we implement this in android????
Try to use countdown timer.
Refer the link.
http://developer.android.com/reference/android/os/CountDownTimer.html
Hope following code helps you out :)
final TextView textView=(TextView)findViewById(R.id.textView1);
Button button=(Button)findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
new CountDownTimer(30000, 1000) {//CountDownTimer(edittext1.getText()+edittext2.getText()) also parse it to long
public void onTick(long millisUntilFinished) {
textView.setText("seconds remaining: " + millisUntilFinished / 1000);
//here you can have your logic to set text to edittext
}
public void onFinish() {
textView.setText("done!");
}
}
.start();
}
});

Android countdown timer display milliseconds?

I want a timer to pop up when I click a button that will count down from 3 seconds. And it does that fine but i want it to also show the milliseconds so when I click the button the text would go from 3.0 to 0.1. How would I add the milliseconds to the text view?
new CountDownTimer(1000, 3000) {
public void onTick(long millisUntilFinished) {
textViewTimer.setText("" + millisUntilFinished / 1000);
}
public void onFinish() {
textViewTimer.setVisibility(View.INVISIBLE);
textViewLevelGained.setVisibility(View.INVISIBLE);
}
}.start();
This is what I have
Other SO questions suggest CountDownTimer doesn't do sub 1-second granularity well. Look into a different class, like TimerTask.
Otherwise, the following would work.
new CountDownTimer(3000, 1) {
public void onTick(long millisUntilFinished) {
textViewTimer.setText("" + millisUntilFinished / 1000
+ "." + millisUntilFinished % 1000);
}
public void onFinish() {
textViewTimer.setVisibility(View.INVISIBLE);
textViewLevelGained.setVisibility(View.INVISIBLE);
}
}.start();

Categories

Resources