Update the parent view when webView finishes on Android

390 views Asked by At

I have a web view which takes a bit of time to load, so I have a progress indicator which should show until the webView is ready. However, what is the best way to do this? I have:

        webView.setWebViewClient(new WebViewClient() {
            public void onPageFinished(WebView view, String url) {
                //mListener.finishedLoadingHtml();
                ProgressBar bar = (ProgressBar) view.findViewById(R.id.article_content_progressIndicator);
                bar.setVisibility(View.GONE);
            }
        });

but that doesn't work as bar = null. Is there an easy way to get access to the progressBar from within the callback? I am still struggling a bit with the Android view hierarchy etc.

2

There are 2 answers

1
MHP On BEST ANSWER

I do it by javaScript
at start I show progressBar on my webView and when page load completely I use a javaScript to notify java that should gone progressBar by this way:

myWebView.addJavascriptInterface(new JavaScriptInterface(context),
            "jsInterface");

ProgressBar bar = (ProgressBar) view.findViewById(R.id.article_content_progressIndicator);

public class JavaScriptInterface {
    Context mContext;

    /** Instantiate the interface and set the context */
    JavaScriptInterface(Context c) {
        mContext = c;
    }

    @JavascriptInterface
    public void pageLoaded() {
         if(bar!=null)
             bar.setVisibility(View.GONE);
    }

}  

and inside html page by javaScript call method like this:

function onPageLoad(){
    jsInterface.pageLoaded();
}
0
Prabhuraj On

This may help.

webview.setWebViewClient(new WebViewClient() {
        public void onProgressChanged(WebView view, int progress)   
        {
            //Make the bar disappear after URL is loaded, and changes string to Loading...
            setTitle("Loading...");
            setProgress(progress * 100); //Make the bar disappear after URL is loaded

            // Return the app name after finish loading
            if(progress == 100)
               setTitle(R.string.app_name);
            }
        });