androidwebviewprogress-barwebviewclientwebchromeclient

Android progressbar on webview appearing twice


I'm trying to create a simple app for android with progress bar. Everything works fine. But, here two issues

1) When the application is launched i can see progress loading twice.

2) How can i disable the progress bar after the initial page is finished loading. I dont want to show the progressbar on each click..

Here is my code

package com.mycom.jquery;
import android.app.Activity;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class HelloWebView extends Activity {

WebView webview;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.getWindow().requestFeature(Window.FEATURE_PROGRESS);

    setContentView(R.layout.main);
    getWindow().setFeatureInt(Window.FEATURE_PROGRESS,
            Window.PROGRESS_VISIBILITY_ON);

    webview = (WebView) findViewById(R.id.webview);
    webview.getSettings().setJavaScriptEnabled(true);

    webview.setWebViewClient(new WebViewClient() {

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }

    });
    final Activity activity = this;

    final ProgressDialog progressDialog = new ProgressDialog(activity);
    progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
    progressDialog.setMessage("Loading...Jquery.com");
    progressDialog.setCancelable(true);

    webview.loadUrl("http://www.jquery.com");

    // WebChromeClient give progress etc info
    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();
        }
    });

}

}

Solution

  • Set a flag when progress becomes 100, and then if that flag is true immediately return in your onProgressChanged implementation.