0

how can I wait to show the webView until it loads all the web data? i have created this asynktask sketch:

@Override
protected Void doInBackground(String... params) {


    webView.setWebChromeClient(new WebChromeClient()); 
    webView.setWebViewClient(new WebViewClient()); 
    webView.getSettings().setJavaScriptEnabled(true);
    String url = params[0];
    webView.loadUrl(url);



    return null;
}

@Override
protected void onPreExecute() {
//progress = ProgressDialog.show(MainActivity.this,"wait","downloading URL");
    progressBar.setVisibility(View.VISIBLE);
}

@Override
protected void onPostExecute(Void result) {


            webView.setVisibility(View.VISIBLE);
            progressBar.setVisibility(View.GONE);



}

}

it works but a the end of this code, after onPostExecute i'm still seeing the blank page of webview loading

Gabrio
  • 388
  • 1
  • 4
  • 17

1 Answers1

1

loadUrl is already doing everything asynchronously, so onPostExecute is called right away.

If you want to display a loading indicator, you need to use that :

mWebView.setWebViewClient(new WebViewClient() {

    public void onPageStarted (WebView view, String url, Bitmap favicon) {
        view.setVisibility(View.GONE);
        progressBar.setVisibility(View.VISIBLE);
    }

    public void onPageFinished(WebView view, String url) {
        view.setVisibility(View.VISIBLE);
        progressBar.setVisibility(View.GONE);
    }
});
webView.loadUrl(url);

and of course remove you AsyncTask.

More : here

Community
  • 1
  • 1
Stephane Mathis
  • 6,542
  • 6
  • 43
  • 69
  • please can u show me a loading indicator example? for example with the progressBar that i am using. can I set the webview visibility 'Visible' only when the loading is finished? – Gabrio Oct 17 '14 at 12:38
  • I've updated my answer. If you page has iframe and stuff like that, use another example in the link provided in my answer. – Stephane Mathis Oct 17 '14 at 12:59