Reputation: 105
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!
Upvotes: 2
Views: 67
Reputation: 572
webView.setWebViewClient(new WebViewClient() {
public void onPageFinished(WebView view, String url) {
// do your stuff here
if (progressCircle != null) {
progressCircle.dismiss();
progressCircle = null;
}
}
});
This full code for you task:
boolean loadingFinished = true;
boolean redirect = false;
mWebView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String urlNewString) {
if (!loadingFinished) {
redirect = true;
}
loadingFinished = false;
webView.loadUrl(urlNewString);
return true;
}
@Override
public void onPageStarted(WebView view, String url) {
loadingFinished = false;
//SHOW LOADING IF IT ISNT ALREADY VISIBLE
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);
}
}
}
@Override
public void onPageFinished(WebView view, String url) {
if (progressCircle != null) {
progressCircle.dismiss();
progressCircle = null;
}
}
});
Enjoy it!
Upvotes: 2