Reputation: 2719
I have a basic working Quiz App. And I'm setting an alert dialog giving the user Explanation of the problem (whether be it right/wrong)
Problem: I couldn't understand, how to put the logic for: pausing my CountDownTimer on AlertDialog and Resume it automatically after the close dialog button is clicked.
FYI: I have found answers on how to pause and resume for CountDownTimer but I couldn't find any answer on setting it programmatically for AlertDialog.
This is my working code:
new CountDownTimer(40000, 1000) {
public void onTick(long millisUntilFinished) {
timer.setText("Sec: " + millisUntilFinished / 1000);
}
public void onFinish() {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(EasyLevel1.this);
alertDialogBuilder
.setTitle("Time's Up!")
.setMessage("Your Final Score is " + mScore + " points.")
.setCancelable(false)
.setPositiveButton("REPLAY",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int i) {
startActivity(new Intent(getApplicationContext(), EasyLevel1.class));
finish();
}
})
.setNegativeButton("EXIT",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int i) {
finish();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
}.start();
And this is my Answer logic:
answer1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(answer1.getText() == mAnswer){
mScore+=5;
score.setText("Score: " + mScore);
updateQuestion(rand.nextInt(mQuestionsLength));
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(EasyLevel1.this, R.style.quizAlert);
alertDialogBuilder
.setTitle("Bingo! Your answer is Correct.\n" + "Score: +5")
.setMessage("Correct Answer: " + mExplanation)
.setCancelable(true)
.setPositiveButton("Close", null);
//startActivity(new Intent(getApplicationContext(),MainActivity.class));
//finish();
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
} else {
mScore-=2;
score.setText("Score: " + mScore);
updateQuestion(rand.nextInt(mQuestionsLength));
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(EasyLevel1.this, R.style.quizAlert);
alertDialogBuilder
.setTitle("Oops! Your answer is Wrong.\n" + "Score: -2")
.setMessage("Correct Answer: " + mExplanation)
.setCancelable(true)
.setPositiveButton("Close", null);
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
}
});
Upvotes: 1
Views: 291
Reputation: 62841
You know how to pause/restart the timer, but you are missing where to put the code that will execute when the alert dialog is closed.
You already have the basis for doing this by using setPositiveButton("Close", null)
. Instead of null
you will want to use a listener that will restart the timer. Something like this:
alertDialogBuilder.setPositiveButton("Close", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// Do timer stuff.
}
});
See the documentation here for details.
Upvotes: 1