Search code examples
androidwebviewprogressdialogprogress

How to make a progress dialog for Android Webview app to disappear before page is fully loaded?


Recently, I added the following progress dialog to my Android webview app. It works perfectly but now I'd like to show the dialog for a certain amount of time, let's say in this example until the page is loaded for 89%.

final Activity activity = this;

    final ProgressDialog progressDialog = new ProgressDialog(activity);
    progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
    progressDialog.setCancelable(false);

    webView.setWebChromeClient(new WebChromeClient() {
        public void onProgressChanged(WebView view, int progress) {
            progressDialog.show();
            progressDialog.setProgress(0);
            activity.setProgress(progress * 1000);

            progressDialog.incrementProgressBy(progress);

            if(progress == 100 && progressDialog.isShowing())
                progressDialog.dismiss();
        }
    });

I tried to achieve this by changing the last part of the code to this:

if(progress == 89 && progressDialog.isShowing())
                progressDialog.dismiss();

The problem is that the dialog disappears when it reaches 89% just like I wanted, but then restarts and loads to 100% without disappearing afterwards.

What am I missing here?


Solution

  • I haven't run your code, but this, what I'm expecting, that:

    public void onProgressChanged(WebView view, int progress) {

    is called also after 89% of loading.

    To get ride of it, you can first change:

    if(progress == 89

    to:

    if(progress >= 89

    and it should be working, but it won't be elegant, as your code from start is not really elegant (in every progress change you are giving show on progress bar. To make it properly make show only once and not in onProgressChanged method.