android - Adding loading spinner on Action Bar while the WebView loads? -
i developing android app. using webview render html page stays blank while page loads. want add loading spinner in action bar user knows loading , not blank.
please can guide me how that.
my existing code -
package com.pranavsethi.dpsrkp; import android.annotation.targetapi; import android.app.actionbar; import android.app.activity; import android.content.intent; import android.net.uri; import android.os.build; import android.os.bundle; import android.view.keyevent; import android.view.menu; import android.view.menuinflater; import android.view.menuitem; import android.webkit.downloadlistener; import android.webkit.webview; import android.webkit.webviewclient; public class achievements extends activity{ private webview mwebview; @targetapi(build.version_codes.honeycomb) @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.webvew_client_layout); actionbar actionbar = getactionbar(); actionbar.setdisplayhomeasupenabled(true); //for web view mwebview = (webview) findviewbyid(r.id.webview); mwebview.getsettings().setjavascriptenabled(true); //enable javascript mwebview.getsettings().setbuiltinzoomcontrols(true); //enable zoom mwebview.getsettings().setdisplayzoomcontrols(false); mwebview.getsettings().setloadwithoverviewmode(true); mwebview.getsettings().setusewideviewport(true); mwebview.setscrollbarstyle(webview.scrollbars_outside_overlay); mwebview.setscrollbarfadingenabled(true); mwebview.setdownloadlistener(new downloadlistener() { @override public void ondownloadstart(string url, string useragent, string contentdisposition, string mimetype, long contentlength) { // handle download, here use brower download, can try other approach. uri uri = uri.parse(url); intent intent = new intent(intent.action_view, uri); startactivity(intent); } }); mwebview.loadurl("http://pranavsethi.tk"); // name of website load mwebview.setwebviewclient(new hellowebviewclient()); //removing loading progressbar } private class hellowebviewclient extends webviewclient{ //our web client @override public void onreceivederror(webview view, int errorcode, //code checking internet connection , return error if fails string description, string failingurl) { intent intent = new intent(achievements.this, mainactivity.class); intent.addflags(intent.flag_activity_clear_top); startactivity(intent); return; } @override public boolean shouldoverrideurlloading(webview webview, string url) { webview.loadurl(url); return true; } } @override public boolean onkeydown(int keycode, keyevent event) { if ((keycode)== keyevent.keycode_back && mwebview.cangoback()) { mwebview.goback(); return true; } return super.onkeydown(keycode, event); } @override public boolean oncreateoptionsmenu(menu menu) { return true; } @override public boolean onoptionsitemselected(menuitem item) { switch (item.getitemid()) { case android.r.id.home: intent intent = new intent(this, mainactivity.class); intent.addflags(intent.flag_activity_clear_top); startactivity(intent); overridependingtransition (r.anim.right_slide_in, r.anim.fade_out); break; default: return super.onoptionsitemselected(item); } return true; } @override public void onbackpressed() { this.finish(); overridependingtransition (0, r.anim.right_slide_out); return; } }
solution
add following -
requestwindowfeature(window.feature_indeterminate_progress); setprogressbarindeterminatevisibility(true);
after -
super.oncreate(savedinstancestate);
and in webviewclient
private class hellowebviewclient extends webviewclient{ @override public boolean shouldoverrideurlloading(webview webview, string url){ webview.loadurl(url); setprogressbarindeterminatevisibility(true); return true; } @override public void onpagefinished(webview webview, string url){ super.onpagefinished(webview, url); setprogressbarindeterminatevisibility(false); } }
Comments
Post a Comment