nicvaldy
nicvaldy

Reputation: 21

How to change message of ProgressDialog every 2 seconds?

I have ProgressDialog, I want to change the message every 2 seconds. In this code, I set the progress time to 10 seconds. so I want to make it have 5 messages.

private void showProgressDialog() {

    progressDialog = new ProgressDialog(this, getProgressDailogStyle());
    progressDialog.setMessage("Processing ...");
    progressDialog.show();
    progressDialog.setCancelable(false);

    Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
        public void run() {
            progressDialog.dismiss();
            mInterstitialAd.show();
        }
    }, 10000);
}

Upvotes: 2

Views: 1429

Answers (5)

Sharath kumar
Sharath kumar

Reputation: 4132

Since you are already running a handler its easy for you to display message to UI thread i.e ProgressDialog.Instead of running the handler for 10 sec delay run it in a loop for 5 interval,i.e 2 sec delay like this.

private int count = 1;
Runnable runnable = null;
private void showProgressDialog() {

    progressDialog = new ProgressDialog(this, getProgressDailogStyle());
    progressDialog.setMessage("Processing ...");
    progressDialog.show();
    progressDialog.setCancelable(false);

    final Handler handler = new Handler();
    runnable = new Runnable() {
        public void run() {
            Log.i("message",""+counter);
            if (counter == 5) {
                handler.removeCallbacks(runnable);
                progressDialog.dismiss();
               // mInterstitialAd.show();
            }else {
                progressDialog.setMessage("your message");
                handler.postDelayed(runnable, 2000);
                counter++;
            }


        }
    };
    handler.post(runnable);
}

Upvotes: 0

Zainal Fahrudin
Zainal Fahrudin

Reputation: 608

You can use CountDownTimer

    count = 0;
    progressDialog = new ProgressDialog(this, getProgressDailogStyle());
    progressDialog.setMessage("Processing ...");
    progressDialog.show();
    progressDialog.setCancelable(false);

    new CountDownTimer(10000, 2000) {

        public void onTick(long millisUntilFinished) {
            //here you can have your logic to set message
            count=count+1;
            if (count==1){
                progressDialog.setMessage("Processing 1");
            }else if (count==2){
                progressDialog.setMessage("Processing 2");
            } 
              // until the count = 5 


        }

        public void onFinish() {
            //the progress is finish
            count = 0;
            progressDialog.dismiss();

        }

    }.start();

Upvotes: 4

KeLiuyue
KeLiuyue

Reputation: 8237

Use Timer in your code .And set the time of schedule method.

In schedule method .

  • First param is TimerTask
  • Second param is delay time
  • Third param is period time

Try this .

private void showProgressDialog() {

    progressDialog = new ProgressDialog(this, getProgressDailogStyle());
    progressDialog.setMessage("Processing ...");
    progressDialog.show();
    progressDialog.setCancelable(false);

    Timer timer = new Timer();
    timer.schedule(new TimerTask() {

        @Override
        public void run() {
            // do something
            Message message=new Message();  
            message.what=0;  
            mHandler.sendMessage(message);  
        }
    }, 0, 2000);
}

Update

private Handler mHandler = new Handler(){  
    @Override  
    public void handleMessage(Message msg) {  

        // TODO Auto-generated method stub  
        if(msg.what == 0){  
            //change message
        }  
    }     
};  

Upvotes: 0

Ishaan Kumar
Ishaan Kumar

Reputation: 985

Runnable r = new Runnable() {
    public void run({
        progressDialog.setMessage('...your msg...');
        handler.postDelayed(r,2000)
    })
};
handler.postDelayed(r,2000);

Upvotes: 0

Jenish
Jenish

Reputation: 234

Try below code, and setMessage must be in runOnUiThread,

public void setMessage(String message) {
    if (dialog != null && dialog.isShowing()) {
        ((Activity) context).runOnUiThread(new Runnable() {
            @Override
            public void run() {
                dialog.setMessage(message);
            }
        });
    }
}

Upvotes: 0

Related Questions