简体   繁体   中英

How to show Progress Dialog Button dynamically after n seconds delay

How to show the ProgressDialog button after n seconds (fixed) delay that the ProgressDialog is shown?

To be more clear, the ProgressDialog starts normally. How can I show a button within it after n seconds?

LF

EDIT

Using the answers of Segi and android_beginner (thank you very much) I'm posting the solution of my problem:

        pDialog = new ProgressDialog(mContext);
        pDialog.setTitle(Reso.getString(mContext, R.string.waiting));
        pDialog.setMessage(Reso.getString(mContext, R.string.waiting));
        pDialog.setIndeterminate(true);
        pDialog.setCancelable(false);
        pDialog.setButton(DialogInterface.BUTTON_NEGATIVE, 
                Reso.getString(mContext, R.string.annulla), 
                new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                // Stuff
            }
        });
        pDialog.show();
        pDialog.getButton(DialogInterface.BUTTON_NEGATIVE).setVisibility(View.INVISIBLE);

        int progressDialogCancelButtonDelay = 2500;

        new CountDownTimer(progressDialogCancelButtonDelay, progressDialogCancelButtonDelay + 1) {

            @Override public void onTick(long millisUntilFinished) { }

            @Override public void onFinish() {
                pDialog.getButton(ProgressDialog.BUTTON_NEGATIVE).setVisibility(View.VISIBLE); 
            }
        }.start();

Just extend this class instead of AsyncTask and be sure to call super() when appropriate:

public abstract class AsyncTaskWithDelayedIndeterminateProgress
  <Params, Progress, Result> extends AsyncTask<Params, Progress, Result> {
private static final int MIN_DELAY = 250;
private final ProgressDialog progressDialog;
private final CountDownTimer countDownTimer;

protected AsyncTaskWithDelayedIndeterminateProgress(Activity activity) {
  progressDialog = createProgressDialog(activity);
  countDownTimer = createCountDownTimer();
}

@Override protected void onPreExecute() {
  countDownTimer.start();
}

@Override protected void onPostExecute(Result children) {
  countDownTimer.cancel();
  if(progressDialog.isShowing())
     progressDialog.dismiss();
}

private ProgressDialog createProgressDialog(Activity activity) {
  final ProgressDialog progressDialog = new ProgressDialog(activity);
  progressDialog.setIndeterminate(true);
  return progressDialog;
}

private CountDownTimer createCountDownTimer() {
  return new CountDownTimer(MIN_DELAY, MIN_DELAY + 1) {
     @Override public void onTick(long millisUntilFinished) { }

     @Override public void onFinish() {
        progressDialog.show();
     }
  };
}

Try Something Like Below Code:

 ProgressDialog loadingDialog = ProgressDialog.show(activity.this, "", "Loading...",
                true, false);

        new Thread(new Runnable() {

            @Override
            public void run() {
                try {

                    Thread.sleep(2500);
                    runOnUiThread(new Runnable() {
                        public void run() {



                                loadingDialog.dismiss();

                               loadingDialog.setButton(ProgressDialog.BUTTON_NEUTRAL, 
                                                 "Close", 
                               new DialogInterface.OnClickListener()                 {                   
                              @Override
                               public void onClick(DialogInterface dialog, int which) {
                            //button click stuff here
                          }
                     });

                    loadingDialog.show(); 
                   loadingDialog.getButton(ProgressDialog.BUTTON_NEUTRAL).setVisibility(View.INVISIBLE); 

                            }
                        }
                    });
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }
        }).start();

make a layout with your ProgressDialog and an invisible button on it. When you show the dialog activate a timer, that set the button visible after n seconds.

I dont know if i understand your question right, but i hope so. =)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM