pranavnairtech pranavnairtech - 9 months ago 22
Android Question

Close ProgressCircle after most of the page is loaded in WebView

So i'm loading a WebView and using a progressCircle to track the same. However, the progress circle doesn't close well until after all images in the WebView have been loaded. I want it to close right after the page loads, irrespective oh how many images have loaded. As of now, my code is:

webView.setWebChromeClient(new WebChromeClient() {
private ProgressDialog progressCircle;

@Override
public void onProgressChanged(WebView view, int progress) {
if (progressCircle == null) {
progressCircle = new ProgressDialog(view.getContext(), R.style.WebViewLoadingCircle);
progressCircle.setCancelable(true);
progressCircle.setProgressStyle(android.R.style.Widget_ProgressBar_Small);
try {
progressCircle.show();
} catch (Exception e) {
Log.e(TAG, "Exception while showing progress circle", e);
}
}

if (progress == 100) {
progressCircle.dismiss();
progressCircle = null;
}
}
});


How can i dismiss the progressCircle as soon as the webpage has loaded? (i've tried a lot of things, including playing with progress variable, but none of it has helped). Thanks!

Answer Source
webView.setWebViewClient(new WebViewClient() {

   public void onPageFinished(WebView view, String url) {
        // do your stuff here
                if (progressCircle != null) {
                    progressCircle.dismiss();
                    progressCircle = null;
                 }
     }
});

Enjoy it!