Reputation: 71
In below code showing progress dialog with 5 second delay. After 5 second want to dismiss the dialog, but not happening in the below.
Can any one please help me where I made the mistake?
new CountDownTimer(5000, 1000)
{
public void onTick(long millisUntilFinished)
{
// You don't need anything here
dialog= ProgressDialog.show(SchedulerActivity.this,"Delay", "Please wait...."+ millisUntilFinished/1000 + " Second(s)",true);
}
public void onFinish()
{
dialog.dismiss();
}
}.start();
Upvotes: 0
Views: 432
Reputation: 853
You are calling
dialog= ProgressDialog.show(SchedulerActivity.this,"Delay", "Please wait...."+ millisUntilFinished/1000 + " Second(s)",true);
in side onTick()
method.
it will create new progress dialog instance at every tick. It might cause issue in your function and your dialog will not dismiss in this case.
Please try with below code
final ProgressDialog dialog = new ProgressDialog(DynamicView.this);
dialog.setTitle("Delay");
dialog.setMessage("Please wait...." + 5000 / 1000 + " Second(s)");
dialog.setIndeterminate(true);
new CountDownTimer(5000, 1000) {
public void onTick(long millisUntilFinished) {
// You don't need anything here
dialog.setMessage("Please wait...." + millisUntilFinished / 1000 + " Second(s)");
if (!dialog.isShowing())
dialog.show();
}
public void onFinish() {
if (dialog.isShowing())
dialog.dismiss();
}
}.start();
This code will works same as you need.
Upvotes: 1
Reputation: 279
on tick is calling multiple time that's why your dialog is not closing
implement this
new CountDownTimer(5000, 1000) {
public void onTick(long millisUntilFinished) {
// You don't need anything here
if(!dialog.isShowing())
{
dialog= ProgressDialog.show(mContext,"Delay", "Please wait...."+ millisUntilFinished/1000 + " Second(s)",true);
}
}
public void onFinish() {
dialog.dismiss();
}
}.start();
Upvotes: 0
Reputation: 56925
Instead of assign value of Progress Dialog to dialog variable. Create Progress Dialog object and set title to it as below.
ProgressDialog pd = new ProgressDialog(yourActivity.this);
pd.setMessage("what ever message you like");
pd.show();
in onFinish()
please call pd.dismiss();
Please set the message in onTick
method and show progress dialog in this method if progress dialog is not already shown.
ProgressDialog pd = new ProgressDialog(yourActivity.this);
new CountDownTimer(5000, 1000)
{
public void onTick(long millisUntilFinished)
{
// You don't need anything here
pd.setMessage("what ever message you like");
if (!pd.isShowing())
pd.show();
}
public void onFinish()
{
pd.dismiss();
}
}.start();
Upvotes: 0