P. Ilyin
P. Ilyin

Reputation: 791

ProgressDialog doesn't show a progress

I use a simple example of ProgressDialog usage. Author of this code sure that his code is right and works good.

    ProgressDialog barProgressDialog;
      Handler updateBarHandler;

    public void launchBarDialog() {
    barProgressDialog = new ProgressDialog(getActivity());

    barProgressDialog.setTitle("Downloading Image ...");
    barProgressDialog.setMessage("Download in progress ...");
    barProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
    barProgressDialog.setProgress(0);
    barProgressDialog.setMax(20);
    barProgressDialog.show();

    new Thread(new Runnable() {
      @Override
      public void run() {
        try {
          // Here you should write your time consuming task...
          while (barProgressDialog.getProgress() <= barProgressDialog.getMax()) {
            Thread.sleep(2000);
            updateBarHandler.post(new Runnable() {
              public void run() {
                barProgressDialog.incrementProgressBy(2);
              }
            });
            if (barProgressDialog.getProgress() == barProgressDialog.getMax()) {
              barProgressDialog.dismiss();
            }
          }
        } catch (Exception e) {
        }
      }
    }).start();
  }

But when i run this code in my project, I see that ProgressDialog is always show a 0 value as a progress. What do i wrong?

Upvotes: 0

Views: 457

Answers (1)

Shaishav
Shaishav

Reputation: 5312

Try the following code:

private ProgressDialog barProgressDialog;

public void launchBarDialog() {
        barProgressDialog = new ProgressDialog(this);

        barProgressDialog.setTitle("Downloading Image ...");
        barProgressDialog.setMessage("Download in progress ...");
        barProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        barProgressDialog.setProgress(0);
        barProgressDialog.setMax(20);
        barProgressDialog.show();

        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    // Here you should write your time consuming task...
                    while (barProgressDialog.getProgress() <= barProgressDialog.getMax()) {
                        Thread.sleep(2000);
                        barProgressDialog.incrementProgressBy(2);

                        if (barProgressDialog.getProgress() == barProgressDialog.getMax()) {
                            barProgressDialog.dismiss();
                        }
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();

        Log.d("", BuildConfig.VERSION_NAME);
    }

Upvotes: 1

Related Questions