0

Is there a way to use progress bar with handler? I have the code below and I would like to include a progress bar that would "count down" the delay until the next loop. (in this case 2000ms).

private boolean started = false;
private Handler handler = new Handler();

private Runnable runnable = new Runnable() {        
    @Override
    public void run() {
        final Random random = new Random();
        int i = random.nextInt(2 - 0 + 1) + 0;
        random_note.setImageResource(image[i]);
        if(started) {
            start();
        }
    }
};

public void stop() {
    started = false;
    handler.removeCallbacks(runnable);
}

public void start() {
    started = true;
    handler.postDelayed(runnable, 2000);        
}
Damijan
  • 183
  • 1
  • 2
  • 7

3 Answers3

0

Class level:

ProgressDialog asycdialog;

Definition (in onCreate()):

asycdialog = new ProgressDialog(YourActivity.this);
asycdialog.setMessage("Working");
asycdialog.getWindow().setGravity(Gravity.CENTER_VERTICAL);
asycdialog.getWindow().setGravity(Gravity.CENTER_HORIZONTAL);
asycdialog.setCancelable(false);

in your start method:

asycdialog.show();

in your stop method:

asycdialog.dismiss();
Skynet
  • 7,820
  • 5
  • 44
  • 80
  • Thanks for this, but I think I have to do this with a progress bar. To be more clear: My code (above) is randomly showing the pictures every 2 seconds. I would like a progress bar showing (countdown) the remaining time until the next picture would show up. – Damijan Feb 16 '14 at 11:04
  • You can use `Asycdialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);` with `Asycdialog.setMax(someInt);` and `Asycdialog.incrementProgressBy(1);` also `.setProgress();` can come handy. – Skynet Feb 16 '14 at 11:07
0

i dont really get what you mean but you can use a handler this way to upgrade a progressBar from the backGroud thread

private ProgressBar mProgressBar
    private Handler handle = new Handler(){

    @Override
    public  void handleMessage(Message msg){

        super.handleMessage(msg);
        mProgressBar.setProgress(msg.arg1);

    }



};

and to send update from another thread

Message msg = handle.obtainMessage(null,intWhat, mProgressionValur, 0);
                      handle.sendMessage(msg);
murielK
  • 1,000
  • 1
  • 10
  • 21
0

Show progress and dismiss after some time :

 mProgressDialog = new ProgressDialog(getActivity());
 mProgressDialog.setMessage("loading info...");
 mProgressDialog.setCancelable(false);
 mProgressDialog.setCanceledOnTouchOutside(false);
 mProgressDialog.setIndeterminate(true);
 mProgressDialog.setProgressStyle(android.R.attr.progressBarStyleSmall);
 mProgressDialog.show();

   new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                mProgressDialog.dismiss();
            }
        }, 3000);
Faheem
  • 930
  • 10
  • 7