Android – javascript Bridge/Interface

Hey friends, Recently come across a requirement for communicating from the Web back to the app. Normally, if app will look for loading URL using shouldOverrideUrlLoading(). so app will perform certain operation specific to the url for example setting a title for particular url loaded in web content. But if the web decides which title string to be shown, then app has to listen to web using javascript interface or simply called as JSbridge.

To create a webview in your activity you need to add the webView widget to the layout.

<?xml version="1.0" encoding="utf-8"?>
<WebView  xmlns:android=""

To load a web page in the WebView, use loadUrl(). For example:

WebView myWebView = (WebView) findViewById(;

Note: Ensure you have added the internet permission in your androidManifest file.

To make the javascript enabled for the webview, enable the JS setting as true.


Binding JavaScript code to Android code

To bind a new interface between your JavaScript and Android code, call addJavascriptInterface(), passing it a class instance to bind to your JavaScript and an interface name that your JavaScript can call to access the class.

For example, you can include the following class in your Android application:

public class WebAppInterface {
    Context mContext;

    /** Instantiate the interface and set the context */
    WebAppInterface(Context c) {
        mContext = c;

    /** Show a toast from the web page */
    public void showToast(String title) {
        Toast.makeText(mContext, title, Toast.LENGTH_SHORT).show();

Caution: If you’ve set your targetSdkVersion to 17 or higher, you must add the @JavascriptInterface annotation to any method that you want available to your JavaScript (the method must also be public). If you do not provide the annotation, the method is not accessible by your web page when running on Android 4.2 or higher.

You can bind this class to the JavaScript that runs in your WebView with addJavascriptInterface() and name the interface Android. For example:

WebView webView = (WebView) findViewById(;
webView.addJavascriptInterface(new WebAppInterface(this), "Android");

On the Web side, Android Interface showToast is visible and all you need to call the function in the script tag.

<input type="button" value="Say hello" 
onClick="showAndroidToast('Hello Android!')" />

    function showAndroidTitle(title) {

IMPORTANT: only primitive data types are supported to passed 
across the JS and android app. if you try passing say for example 
JSONObject or Object, it will be null as these datatypes are not 

But workaround can be achieved by passing all the jsoninfo as strings 
and the app has to parse it to JSONObject.

JsonObject jsonObject = (JsonObject) jsonParser.parse(jsonstring);

All good. App will show the Title string info to be passed. Happy coding 🙂


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s