Android: How can i show progress bar while loading data into WebView?

yital9 picture yital9 · Feb 21, 2012 · Viewed 13.7k times · Source

How can I show the progress bar while loading data into my webview? My code :

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    this.requestWindowFeature(Window.FEATURE_PROGRESS);
    getWindow().setFeatureInt( Window.FEATURE_PROGRESS, Window.PROGRESS_VISIBILITY_ON); 
    this.setProgressBarVisibility(true);
    setContentView(R.layout.layout_article);
    WebView webview = (WebView) findViewById(R.id.webView);
    final Activity activity = this;
    webview.setWebChromeClient(new WebChromeClient() {
        public void onProgressChanged(WebView view, int progress) {
            activity.setProgress(progress * 100);
        }
    });

    webView.loadUrl("http://google.com");
}

Answer

Paresh Mayani picture Paresh Mayani · Feb 21, 2012

Before calling loadData() function, just try to show ProgressDialog or put ProgressBar inside layout.

And inside the WebViewClient, just dismiss() the progressDialog or make the ProgressBar invisible.

for example:

// when finish loading page
public void onPageFinished(WebView view, String url) {
       if(mProgress.isShowing()) {
             mProgress.dismiss();
       }
}

FYI, call loadData() or loadURL() only after you are done with web client setting.

check this example: Load WebView with ProgressDialog