android - Hide WebView until JavaScript is done -
i have webview
webview wv; wv = (webview)findviewbyid(r.id.webview1); wv.loadurl("http://example.com/");
simply said.
at:
onpagefinished
i have:
wv.loadurl("javascript:(function() { " + "document.getelementsbyclassname('centered leaderboard_container')[0].style.display = 'none'; " + "document.getelementsbyclassname('n')[0].style.display = 'none'; " + "document.getelementsbyclassname('paginator')[0].style.display = 'none'; " + "document.getelementsbytagname('ul')[0].style.display = 'none'; " + "document.getelementsbytagname('tr')[0].style.display = 'none'; " + "})()");
i've set webview visibility invisible
how can set visibility visible after javascript done?
now see whole page second , javascript done..
anyone?
ps. website not mine, 3rd party website
tested on api 17 emulator , works.
you can inject javascript java web.
and vice-versa, once url loaded call javascript function on our java code, , execute setvisibility()
. purpose going add js interface.
here code:
private final static string host = "stackoverflow.com"; private webview wb; @suppresslint("setjavascriptenabled") @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); requestwindowfeature(window.feature_no_title); setcontentview(r.layout.activity_home); wb = (webview) findviewbyid(r.id.home_webview); //make webview invisible wb.setvisibility(view.invisible); websettings websettings = wb.getsettings(); websettings.setjavascriptenabled(true); wb.setwebviewclient(new webviewclient(){ public void onpagefinished(webview view, string url){ //inject javascript code url given //not display element wb.loadurl("javascript:(function(){"+"document.getelementbyid('id').style.display ='none';"+"})()"); //call function defined on myjavascriptinterface wb.loadurl("javascript: window.calltoanandroidfunction.setvisible()"); } }); //add javascriptinterface, can make calls web java methods wb.addjavascriptinterface(new myjavascriptinterface(), "calltoanandroidfunction"); wb.loadurl("http://"+host); } public class myjavascriptinterface { @javascriptinterface public void setvisible(){ runonuithread(new runnable() { @override public void run() { wb.setvisibility(view.visible); } }); } }
this functionality going executed every page. once on 3rd party server have manage every request, webclient.shouldoverrideurlloading()
can you.
updated answer:
i reproduce commented, last version should do:
beginning in android 4.2, have explicitly annotate public methods @javascriptinterface in order make them accessible hosted javascript. note takes effect if have set app's minsdkversion or targetsdkversion 17 or higher.
i added , imported android.webkit.javascriptinterface
reference: javascriptinterface methods in webviews must annotated
Comments
Post a Comment