Reputation: 991
I'm trying to use variable into the CountDownTimer constructor but the application doesn't work. Normally works with duration without a variable(like 10000). How Can I use a variable?
my code
private CountDownTimer countDownTimer = new CountDownTimer(duration, 1000) {
@Override
public void onTick(long l) {
txt_count.setText(String.valueOf(l / 1000));
progressBar.setProgress((int) l/ 1000);
}
@Override
public void onFinish() {
iscountering = false;
if(temp_question.getQuestions().size() !=(temp_position+1))
Do_Next_Question(temp_position,temp_question);
else
Toast.makeText(ObserverRoomActivity.this, "پایان آزمون", Toast.LENGTH_SHORT).show()
}
};
private void StartTimer(){
if(!iscountering){
iscountering = true;
countDownTimer.start();
}else{
iscountering = false;
countDownTimer.cancel();
}
}
Upvotes: 1
Views: 398
Reputation: 324
I think you need to use timer like this:
private CountDownTimer countDownTimer;
private void createTimer(int duration) {
countDownTimer = new CountDownTimer(duration, 1000) {
@Override
public void onTick(long l) {
txt_count.setText(String.valueOf(l / 1000));
progressBar.setProgress((int) l/ 1000);
}
@Override
public void onFinish() {
iscountering = false;
if(temp_question.getQuestions().size() !=(temp_position+1))
Do_Next_Question(temp_position,temp_question);
else
Toast.makeText(ObserverRoomActivity.this, "پایان آزمون", Toast.LENGTH_SHORT).show()
}
};
}
private void StartTimer(){
if(!iscountering){
iscountering = true;
countDownTimer.start();
} else {
iscountering = false;
countDownTimer.cancel();
}
}
// Call this method to pass your duration for the timer
private void createAndStartTimer(int duration) {
createTimer(duration);
startTimer();
}
Upvotes: 1