Android-Javascript Bridge

Posted on 10 Feb, 2015 in android

Last post I wrote about Interfacing Javascript from Webview to native Android was about one way communication from Javascript to Android activity.

Later I tried a duplex communication between Activity and Javascript. That means -

  • Call Javascript methods from Android activity
  • Call Android activity methods from Javascript

are possible.

Android to Javascript


String data = "Hello!";
webview.loadUrl("javascript:setData(\"" + data + "\");");

In index.html

function setData(data) {

Javascript to Android


public class JSInterface {
    JSInterface() {}
    public String getData() {
        return "Hello";

webview.addJavascriptInterface(new JSInterface(), "JSInterface");

In index.html

var data = JSInterface.getData();

Here is a preview of my setup where a javacript function is called when a button is clicked, and the javascrip call activity's function for current date string.


Check my repository Android-Javascript-Bridge for full project source.