Reputation: 49
I researched some code for countdown on AlertDialog but when I tried the code its not counting down its just show the time tha when its clicked but when it is clicked again it is ticked but I wanted it to be inside the dialog box when its counting down. Well here is my code.
fabTimer.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
final AlertDialog.Builder builder = new AlertDialog.Builder(downPaymentActivity.this);
builder.setTitle("Time left");
try {
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
TimeZone tz = TimeZone.getTimeZone("Asia/Manila");
sdf1.setTimeZone(tz);
String currentDateandTime = sdf1.format(new Date());
String givenDateString = tvDueDate.getText().toString();
Date date1 = sdf1.parse(currentDateandTime);
Date date2 = sdf1.parse(givenDateString);
long restDatesinMillis = date2.getTime() - date1.getTime();
final String hms = String.format("%02d:%02d:%02d",
TimeUnit.MILLISECONDS.toHours(restDatesinMillis),
TimeUnit.MILLISECONDS.toMinutes(restDatesinMillis) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(restDatesinMillis)),
TimeUnit.MILLISECONDS.toSeconds(restDatesinMillis) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(restDatesinMillis)));
builder.setMessage(hms);
new CountDownTimer(10000, 1000) {
@Override
public void onTick(long l) {
builder.setMessage(hms + (l/1000));
}
@Override
public void onFinish() {
}
}.start();
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.cancel();
}
});
builder.show();
} catch (ParseException e) {
e.printStackTrace();
}
}
});
Upvotes: 2
Views: 2234
Reputation: 1390
Please use the below code for count down time in your alert dialog box its working
new CountDownTimer(30000, 1000) {
public void onTick(long millisUntilFinished) {
mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
}
public void onFinish() {
mTextField.setText("done!");
}
}.start();
Upvotes: 0
Reputation: 17820
The builder and the dialog are two separate entities. Your timer is setting the message for the builder after the dialog has already been built.
You should keep a reference to what's returned by:
builder.show();
And then call setMessage()
on that.
Also, don't forget to cancel the timer when your dialog is dismissed.
Upvotes: 0
Reputation: 61009
You can create AlertDialog
like this
final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Time left");
builder.setMessage("start time");
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.cancel();
}
});
final AlertDialog alert = builder.create();
alert.show();
new CountDownTimer(10000, 1000) {
@Override
public void onTick(long l) {
alert.setMessage("left: "+l);
}
@Override
public void onFinish() {
alert.setMessage("end");
}
}.start();
Upvotes: 4