How To Call A Javascript Function Directly From An Activity In Android?
I wanted to know if there is any way that I can call a JavaScript function from native Android Activity. I came across: webView.loadUrl('javascript:hello()'); But it did not wo
Solution 1:
Android can only call the javascript method if an html page is currently loaded in webView
webView.loadUrl("javascript:hello()");
will call hello method writen in the html page only if the page containing this method is currently loaded in the webview control
first call
webview.loadUrl("Your html page url");
then call
webView.loadUrl("javascript:hello()");
Solution 2:
In case anyone has any issues with this like I did, you need to call your javascript function after the page has finished loading otherwise the javascript won't be called:
webviewer.loadUrl("file:///android_asset/mypage.html");
webviewer.setWebViewClient(newWebViewClient() {
publicvoidonPageFinished(WebView view, String url)
{
webviewer.loadUrl("javascript:hello()");
}
});
Post a Comment for "How To Call A Javascript Function Directly From An Activity In Android?"