Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Tom Coomer
1,331 PointsEndless countdown timer
I would like to create a timer that will continue into minus figures.
new CountDownTimer(1000, 10) {
public void onTick(long millisUntilFinished) {
timerGame1.setText("" + millisUntilFinished / 10);
}
public void onFinish() {
//timerGame1.setText("done!");
}
}.start();
3 Answers
Jack Hill
1,168 PointsDid you get your problem solved? Because thats a neat thought and if you would share the code I would like to see it.
Barrie Freyre
23,301 PointsThis is rustic code but I hope it can help you to get a better solution
Is a simple activity with three buttons which you can select the following:
- From 20 to 10 ( mBtn20to10 )
- From 10 to -10 ( mBtn10minus10 )
- From 20 to 0 ( mBtn20to0 )
The trick to manage a range is using an offset variable, see the code below,
Regards, Barrie
package com.thecatdesign.counterdownexample;
import android.app.Activity; import android.os.Bundle; import android.os.CountDownTimer; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.TextView;
public class MainActivity extends Activity {
protected TextView mCounterDownMessage;
protected Button mBtn20to0;
protected Button mBtn10minus10;
protected Button mBtn20to10;
protected CountDownTimer mCountTimer;
protected long offset;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mCounterDownMessage = (TextView) findViewById(R.id.txtCounterDownMessage);
mBtn20to0 = (Button) findViewById(R.id.btn20to0);
mBtn10minus10 = (Button) findViewById(R.id.btn10minus10);
mBtn20to10 = (Button) findViewById(R.id.btn20to10);
mBtn20to0.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
executeTimerDown(20000, 0, 200);
}
});
mBtn20to10.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
executeTimerDown(20000, 10000, 200);
}
});
mBtn10minus10.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
executeTimerDown(10000, -10000, 200);
}
});
} // OnCreate
private void executeTimerDown(long millisInFuture, long toMillisInFuture, long countDownInterval ) {
offset = toMillisInFuture;
Log.i("check", "offset" + offset);
Log.i("check", "millisInFuture" + millisInFuture);
Log.i("check", "toMillisInFuture" + toMillisInFuture);
if (toMillisInFuture < 0 ) {
millisInFuture += Math.abs(toMillisInFuture);
}
else if (toMillisInFuture > 0 ) {
millisInFuture -= Math.abs(toMillisInFuture);
}
Log.i("check", "millisInFuture" + millisInFuture);
Log.i("check", "toMillisInFuture" + toMillisInFuture);
mCountTimer = new CountDownTimer(millisInFuture, countDownInterval) {
@Override
public void onTick(long millisUntilFinished) {
long secondsToShow = millisUntilFinished;
if (offset > 0)
secondsToShow += Math.abs(offset);
else if (offset < 0)
secondsToShow -= Math.abs(offset);
Log.i("check", "secondsToShow: " + secondsToShow );
Log.i("check", "millisUntilFinished: " + millisUntilFinished );
mCounterDownMessage.setText("seconds remaining: " + secondsToShow / 1000);
}
@Override
public void onFinish() {
mCounterDownMessage.setText("done!");
}
}.start();
} // executeTimerDown
} // MainActivity
Tom Coomer
1,331 PointsThanks!
Barrie Freyre
23,301 Points-
Tom Coomer
1,331 PointsTom Coomer
1,331 PointsNot yet. Sorry.