Android Calling JavaScript Functions in Webview

Calling JavaScript from Android

According to CW answer,

You need to wait until your page is loaded

private void helloJs(){
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
webView.evaluateJavascript("javascript:hello();", null);
} else {
webView.loadUrl("javascript:hello();");
}
}

webView.setWebViewClient(new WebViewClient() {
public void onPageFinished(WebView view, String url) {
helloJs();
}
});

Run javascript code in Webview

From kitkat onwards use evaluateJavascript method instead loadUrl to call the javascript functions like below

    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
webView.evaluateJavascript("var FunctionOne = function () {"
+ " try{document.getElementsByClassName('test')[0].style.color='red';}catch(e){}"
+ "};", null);
} else {
webView.loadUrl("javascript:"
+ "var FunctionOne = function () {"
+ " try{document.getElementsByClassName('test')[0].style.color='red';}catch(e){}"
+ "};");
}

Enable Javascript for your webview by adding the following line

wb.getSettings().setJavaScriptEnabled(true);

It is possible to call a javascript function in android?

I believe you can use

WebView webView = (WebView) findViewById(R.id.webview);
webView.setWebChromeClient(new MyCustomChromeClient(this));
webView.getSettings().setJavaScriptEnabled(true);
webView.evaluateJavascript("globalJavascriptFunction();", null);

https://developer.android.com/reference/android/webkit/WebView#evaluateJavascript(java.lang.String,%20android.webkit.ValueCallback%3Cjava.lang.String%3E)

The second argument is a callback.


[edit]

To get a value back from the javascript, you can do something like this:

webView.evaluateJavascript("globalJavascriptFunction();", new ValueCallback<String>() {
@Override
public void onReceiveValue(String result) {
// result is the return value of globalJavascriptFunction()
// you'll have to do some testing inside here to figure out exactly how result gets sent back for your usecase
}
});

How to inject whole javascript function into webview and call it from android

Use the below mentioned method to inject a js file from asset folder

public void injectScriptFile(WebView view, String scriptFile) {
InputStream input;
try {
input = getAssets().open(scriptFile);
byte[] buffer = new byte[input.available()];
input.read(buffer);
input.close();

// String-ify the script byte-array using BASE64 encoding !!!
String encoded = Base64.encodeToString(buffer, Base64.NO_WRAP);
view.loadUrl("javascript:(function() {" +
"var parent = document.getElementsByTagName('head').item(0);" +
"var script = document.createElement('script');" +
"script.type = 'text/javascript';" +
// Tell the browser to BASE64-decode the string into your script !!!
"script.innerHTML = window.atob('" + encoded + "');" +
"parent.appendChild(script)" +
"})()");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}


Related Topics



Leave a reply



Submit