views:

156

answers:

4

I have a webview that is loading a page from the Internet. I want to show a progressbar until the loading is complete.

How do I listen for the completion of page loading of a WebView?

A: 

Use setWebViewClient() and override onPageFinished()

BrennaSoft
+5  A: 

Just implement WebViewClient and extend onPageFinished() as follows:

mWebView.setWebViewClient(new WebViewClient() {

   public void onPageFinished(WebView view, String url) {
        // do your stuff here
    }
});
irlennard
A: 

You can trace the Progress Staus by the getProgress mehtod in webview class.

Initialize the progress status

private int mProgressStatus = 0;

then the AsyncTask for loading like this:

private class Task_News_ArticleView extends AsyncTask<Void, Void, Void> {
    private final ProgressDialog dialog = new ProgressDialog(
            your_class.this);

    // can use UI thread here
    protected void onPreExecute() {
        this.dialog.setMessage("Loading...");
        this.dialog.setCancelable(false);
        this.dialog.show();
    }

    @Override
    protected Void doInBackground(Void... params) {
        try {
            while (mProgressStatus < 100) {
                mProgressStatus = webview.getProgress();

            }
        } catch (Exception e) {

        }
        return null;

    }

    protected void onPostExecute(Void result) {
        if (this.dialog.isShowing()) {
            this.dialog.dismiss();
        }
    }
}
Praveen Chandrasekaran
A: 

If you want show a progress bar you need to listen for a progress change event, not just for the completion of page:

mWebView.setWebChromeClient(new WebChromeClient(){

            @Override
            public void onProgressChanged(WebView view, int newProgress) {

                //change your progress bar
            }


        });

BTW if you want display just an Indeterminate ProgressBar overriding the method onPageFinished is enough

Francesco