Issue
I want a loader to be displayed every time when I clicked on any link inside webview.
currently at application start
only it is showing me loader
.
used following code
some time for every load loader is coming sometimes not , not sure why it is happening.
// Force links and redirects to open in the WebView instead of in a browser
mWebView.setWebViewClient(new WebViewClient() {
ProgressDialog progressDialog = null;
//If you will not use this method url links are opeen in new brower not in webview
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (progressDialog == null) {
// in standard case YourActivity.this
progressDialog = new ProgressDialog(MainActivity.this);
progressDialog.setMessage("Loading...");
progressDialog.show();
}
view.loadUrl(url);
return true;
}
//Show loader on url load
public void onLoadResource (WebView view, String url) {
if (progressDialog == null) {
// in standard case YourActivity.this
progressDialog = new ProgressDialog(MainActivity.this);
progressDialog.setMessage("Loading...");
progressDialog.show();
}
}
public void onPageFinished(WebView view, String url) {
try{
if (progressDialog.isShowing()) {
progressDialog.dismiss();
progressDialog = null;
}
}
catch(Exception exception){
exception.printStackTrace();
}
}
//Show error page
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
mWebView.loadUrl("file:///android_asset/error.html");
}
});
// Javascript enabled on webview
mWebView.getSettings().setJavaScriptEnabled(true);
//Load url in webview
mWebView.loadUrl(url);
}
Solution
Try This:
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (progressDialog == null) {
// in standard case YourActivity.this
progressDialog = new ProgressDialog(MainActivity.this);
progressDialog.setMessage("Loading...");
progressDialog.show();
}
view.loadUrl(url);
return true;
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
try {
if (progressDialog .isShowing()) {
progressDialog .dismiss();
progressDialog = null;
}
} catch (Exception exception) {
exception.printStackTrace();
}
}
@Override
public void onLoadResource (WebView view, String url) {
}
public void onPageStarted(WebView webView, String url, Bitmap favicon) {
super.onPageStarted(webView, url, favicon);
if (progressDialog == null) {
// in standard case YourActivity.this
progressDialog = new ProgressDialog(MainActivity.this);
progressDialog.setMessage("Loading...");
progressDialog.show();
}
}
Answered By - Md. Ilyas Hasan Mamun
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.