I am trying to create a countdown timer that will increases its speed after a button press I uses the counter also to adjust a progress bar.
Right now I am adjusting the speed (increasing) after the button is pressed but it does not start from the beginning. For example, when I start my program the timer start from the beginning and decreases progressively, which is fine. However, when I press the button the counter does not start from the beginning like this:
I want just to make it run faster after each button press, not to decrease the length.
this is my code:
mTrueBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//is the user right? if pressing True button
if(isMathProblemTrue == 1){
//user is correct
Toast.makeText(MainActivity.this,"Correct!",Toast.LENGTH_SHORT).show();
generateMathProblem();
timer_length *= 0.8;
timer_interval *= 0.8;
Log.d(TAG,"time length:"+timer_length);
Log.d(TAG,"time interval:"+timer_interval);
mCountDownTimer.cancel();
createNStartTimer();
//restartTimer();
}else{
//user is incorrect
transferUserToStartScreen();
//reset the timer
mCountDownTimer.cancel(); // cancel
}
}
});
private void createNStartTimer() {
mCountDownTimer = new CountDownTimer(timer_length,timer_interval) {
#Override
public void onTick(long millisUntilFinished) {
Log.d(TAG,"Mil until finish:"+millisUntilFinished);
int progress = (int) (millisUntilFinished/100);
mProgressBar.setProgress(progress);
}
#Override
public void onFinish() {
mProgressBar.setProgress(0);
transferUserToStartScreen();
}
}.start();
}
create 2 global constants outside the functions
int totalMillisUntilFinished = 0;
bool firstTime = true;
we initialize the totalMillisUntilFinished when onTick is called for the time, so update your your onTick function:
private void createNStartTimer() {
firstTime = true;
mCountDownTimer = new CountDownTimer(timer_length,timer_interval) {
#Override
public void onTick(long millisUntilFinished) {
if(firstTime){totalMillisUntilFinished = millisUntilFinished; firstTime = false;}
Log.d(TAG,"Mil until finish:"+millisUntilFinished);
int progress = (int) (millisUntilFinished*100/totalMillisUntilFinished);
mProgressBar.setProgress(progress);
}
Personally, I use Handlers and Runnables, which I would definitely suggest looking into instead.
Related
How can I pause the countdownTimer if a dialog need to appear first. What can I do so that the timer will not start until I click the ok button.
countDownTimer = new CountDownTimer(320000, 1200) {
#Override
public void onTick(long l) {
timeText.setText(String.valueOf(timeValue));
timeValue = timeValue - 1;
if (timeValue == -1){
FLAG = 3;
playAudio.setAudioforEvent(FLAG);
timerDialog.timerDialog();
countDownTimer.cancel();
dbHandler.addScore2(tag,Integer.parseInt(txtCorrect.getText().toString()));
}
}
#Override
public void onFinish() {
// timerDialog.timerDialog();
}
}.start();
This is the dialog that will appear first.
public void fillDialog(){
fillDialog = new Dialog(mnContext);
fillDialog.setContentView(R.layout.activity_instruction_fill);
final Button btdialog = (Button) fillDialog.findViewById(R.id.ok_btn_fill);
btdialog.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
fillDialog.dismiss();
}
});
You can use the countdownTimer.cancel() method to pause the countdown timer. When the dialog appears, call this method to pause the timer. When the user clicks the "OK" button on the dialog, you can then call the countdownTimer.start() method to resume the timer from where it left off.
Alternatively, you can also use countdownTimer.pause() and countdownTimer.resume() to pause and resume the timer.
It would be good to keep in mind that countdownTimer is a android class and you need to call the above methods from your activity class
I have a widget with a button that users must press within a given interval. The button works fine and resets the interval but I want the button to change colour green -> amber -> red depending on time left. I have no problem changing the text on the button using remote views with code like this:
RemoteViews views =new RemoteViews(context.getPackageName(), R.layout.example_widget);
views.setCharSequence(R.id.example_widget_button, "setText", buttonText);
But I can not make any sort of code change the button colour. I've tried several things along the lines of:
views.setCharSequence(R.id.example_widget_button, "setBackgroundTint", "#039be5");
I have also tried using a drawable background and changing that. I'm missing something really obvious -it must be possible- I just can't find an example that works in my context.
Can anyone point me?
You can do this:
views.setInt(R.id.example_widget_button, "setBackgroundColor", android.graphics.Color.BLACK)`;
and you change the color to be what you want at that time.
You can either use Handler or CountDownTimer.
If you want to use Handler, here is the example:
long totalTime = 10000;
long warningTime = 6000;
long alertTime = 30000;
Runnable warningColorChangeRunnable = new Runnable() {
#Override
public void run() {
button.setBackgroundColor(getResources().getColor(R.color.colorWarning));
}
};
Runnable alertColorChangeRunnable = new Runnable() {
#Override
public void run() {
button.setBackgroundColor(getResources().getColor(R.color.colorAlert));
}
};
final Handler handler = new Handler();
handler.postDelayed(warningColorChangeRunnable, totalTime - warningTime);
handler.postDelayed(alertColorChangeRunnable, totalTime - alertTime);
button.setOnClickListener(new OnClickListener() {
#Override public void onClick(View view) {
handler.removeCallbacks(warningColorChangeRunnable);
handler.removeCallbacks(alertColorChangeRunnable);
}
});
If you want to use CountDownTimer, here is the example:
long totalTime = 10000;
long warningTime = 6000;
long alertTime = 30000;
long interval = 1000;
CountDownTimer timer = new CountDownTimer(totalTime, 1000) {
public void onTick(long millisUntilFinished) {
if (millisUntilFinished <= warningTime && millisUntilFinished > warningTime - interval) {
button.setBackgroundColor(getResources().getColor(R.color.colorWarning));
}
if (millisUntilFinished <= alertTime && millisUntilFinished > alertTime - interval) {
button.setBackgroundColor(getResources().getColor(R.color.colorAlert));
}
}
public void onFinish() {
// Maybe show a failure dialog
}
}.start();
button.setOnClickListener(new OnClickListener() {
#Override public void onClick(View view) {
timer.cancel();
}
});
So I have this "hack" to have a toast duration a little bit longer:
// Toast...
zanimivosti = new Toast(getApplicationContext());
zanimivosti.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
zanimivosti.setView(layout);
new CountDownTimer(4000, 1000) {
public void onTick(long millisUntilFinished) {
zanimivosti.show();
}
public void onFinish() {
zanimivosti.show();
}
}.start();
PROBLEM: When a user go to another intent, it can happen that the toast reaper again in new intent.
In my case CountDownTimer.cancel(); won't work
ALTERNATIVE:
In my toast I am displaying news every time the user lunch the app. I would also take in consideration a better solution whit a toast that when a user click on it disappear or when new intent is called also disappear.
Should I use a pop up dialog? Can I make it disappear when user clicks on it?
At the end I did it like this:
Toast zanimivosti;
int cas = 4000;
int perioda = 1000;
boolean boolean_toast = true;
CountDownTimer timer;
timer = new CountDownTimer(time, perioda) {
public void onTick(long millisUntilFinished) {
zanimivosti.show();
}
public void onFinish() {
if (boolean_toast == true) {
zanimivosti.show();
} else {
}
}
}.start();
And stoping the timer with stopping/cancelling all the possible things:
boolean_toast = false;
time = 0;
perioda = 0;
timer.cancel();
zanimivosti.cancel();
Similar query: wait until all threads finish their work in java
Hello,
I'm developing android application that contains some kind of count down timer. My problem is, that I have multiple count down timers, that sets text of TextView and have to work separately (first count down timer has to wait until the second one is finished etc.)
See code:
MyCountDownTimer.java
public class MyCountdownTimer extends CountDownTimer {
TextView tv;
// default constructor
public MyCountdownTimer (long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
// constructor
public MyCountdownTimer(int hours, int mins, int secs,
long countDownInterval, TextView tv) {
super(3600000 * hours + 60000 * mins + secs * 1000, countDownInterval);
this.tv = tv;
// all other settings
}
// when finished, set text of textview to done
#Override
public void onFinish() {
tv.setText("done!");
}
// when working periodically update text of text view to value of time left
#Override
public void onTick(long millisUntilFinished) {
tv.setText(/*function setting text of TextView based on time left*/);
}
}
Timer.java
public class Timer extends Fragment {
Button bStart;
Button bStop;
TextView tvShowTime;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.timer, container, false);
}
public void onStart() {
super.onStart();
bStart = (Button) getView().findViewById(R.id.bTimerStart);
bStop = (Button) getView().findViewById(R.id.bTimerStop);
tvShowTime = (TextView) getView().findViewById(R.id.showTime);
// setting on button start click
bStart.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
timerStart();
}
});
// setting on button stop click
bStop.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
timerStop();
}
});
}
private void timerStart() {
bStart.setClickable(false);
int repeat = 2;
int hour, mins, secs;
for (int i = 0; i < 2 * repeat; i++) {
if (i % 2 == 0) {
// setting working count down timer values
mins = 1;
} else {
// setting resting count down timer values
secs = 30;
}
timerCount = new MyCountdownTimer(hours, mins, secs, REFRESH_RATE,
tvShowTime);
timerCount.start();
//////////////////////////////////////////////////
// HERE I WANT TO WAIT UNTIL COUNTDOWN IS DONE //
// ATM SECOND, THIRD AND FOURTH COUNT DOWN //
// TIMER IS STARTED //
//////////////////////////////////////////////////
}
}
-- EDIT --
At the end I did 2 CountDownTimers where first one calls in onFinish() second one and second one calls the first one until repeat = 0;
In final it was the best sotution for me. Anyway thanks for help, #Triode's answer helped me a lot
Start your SECOND, THIRD AND FOURTH COUNT DOWN TIMER in onFinish() of Your MyCountdownTimer
I think you have to put down the timerStop() method implementation and do not try to hide it,you need answer but you don't want others to benifit :s
Okay, so I have a countdown timer, and my app requires the user to tap a button numerous times, however the timer starts ON that button tap. My problem is:
I have a 10 second countdown timer that starts at the press of the button, but instead of just continuing down to 0, it restarts at 10 everytime the user taps the button. How do I make it so when the user taps it the first time, it keeps counting down?
My code:
private Button tapBtn;
TextView cm;
tapBtn = (Button) findViewById(R.id.Tap);
cm = (TextView) findViewById(R.id.Timer);
final CountDownTimer aCounter = new CountDownTimer(10000, 1000) {
public void onTick(long millisUntilFinished) {
cm.setText("Time Left: " + millisUntilFinished / 1000);
}
public void onFinish() {
cm.setText("Time's Up!");
}
};
aCounter.cancel();
tapBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
scr = scr - 1;
TextView Score = (TextView) findViewById(R.id.Score);
Score.setText(String.valueOf(scr));
aCounter.start();
}
});
}
Are you trying to make it so that if the user has already started the timer, subsequent button presses don't restart it from the first tap? If so, all you should have to do is put an if statement in your onclick that checks to see if the timer is still counting down, i.e. check and see if the current time is greater than 0 on the counter.
Edit: here's code
final CountDownTimer aCounter = new CountDownTimer(10000, 1000) {
private long timeLeft;
public long getTimeLeft() {
return timeLeft;
}
public void onTick(long millisUntilFinished) {
timeLeft = millisUntilFinished / 1000;
cm.setText("Time Left: " + timeLeft);
}
public void onFinish() {
cm.setText("Time's Up!");
}
};
aCounter.cancel();
tapBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (aCounter.getTimeLeft() == 0) {
scr = scr - 1;
TextView Score = (TextView) findViewById(R.id.Score);
Score.setText(String.valueOf(scr));
aCounter.start();
}
}
});
}
one way to do it is to create a flag that gets set on the first tap and have the onclick event flip the flag on the first click, and put the timer start inside of an if statement that only occurs if the flag hasn't been set.