How to get return value from javascript in WebView of Android?

Cuong Ta picture Cuong Ta · Jul 21, 2010 · Viewed 97.2k times · Source

I want to get a return value from Javascript in Android. I can do it with the iPhone, but I can't with Android. I used loadUrl, but it returned void instead of an object. Can anybody help me?

Answer

Kirill Kulakov picture Kirill Kulakov · Sep 29, 2013

Same as Keith but shorter answer

webView.addJavascriptInterface(this, "android");
webView.loadUrl("javascript:android.onData(functionThatReturnsSomething)");

And implement the function

@JavascriptInterface
public void onData(String value) {
   //.. do something with the data
}

Don't forget to remove the onData from proguard list (if you have enabled proguard)