Android : How to add spinner loader (NOT PROGRESS BAR) before webview loads a webpage in Android?

on Tuesday, August 12, 2014


I have a webview inside Alert Dialog. How can i show loader spinner (NOT PROGRESS BAR) before webview loads and then hide once webpage is loaded ? Below is my code. Here i am not using any xml for webview, its dynamically generated simple webview :



WebView wv = new WebView(activity);
wv.loadUrl("https://www.yahoo.com");
wv.getSettings().setJavaScriptEnabled(true);
wv.getSettings().setBuiltInZoomControls(true);
wv.getSettings().setSupportZoom(true);
wv.getSettings().setUseWideViewPort(true);
wv.setClickable(true);
wv.setInitialScale(1);
wv.setWebViewClient(new WebViewClient() {

@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
//view.loadUrl(url);
//return true;
String cleanUrl = url;
if (url.contains("?")) {
// remove the query string
cleanUrl = url.substring(0,url.indexOf("?"));
}

if (cleanUrl.endsWith("pdf")) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse(url), "application/pdf");
try {
Uri uriUrl = Uri.parse(cleanUrl);
Intent intentUrl = new Intent(Intent.ACTION_VIEW, uriUrl);
view.getContext().startActivity(intentUrl);
return true;

} catch (Exception e) {
System.out.println(e);
Toast.makeText(context,"No PDF Viewer Installed", Toast.LENGTH_LONG).show();
}
} else {
view.loadUrl(url);
}
return true;
}

});

builder.setView(wv);
builder.show();

0 comments:

Post a Comment