---
title: "Android - Using native features, through Javascript | Steve Edson"
description: "The next step is to allow us to use the native features on the device, through the webview and JavaScript."
url: https://steveedson.co.uk/articles/2014-01-22-creating-a-webview-android-app-part-2
generatedAt: 2026-12-01T16:48:36.208Z
---

# Android - Using native features, through Javascript

#### The next step is to allow us to use the native features on the device, through the webview and JavaScript.

* 22 January 2014

[#tutorial](https://steveedson.co.uk/tags/tutorial)[#android](https://steveedson.co.uk/tags/android)[#jsbridge](https://steveedson.co.uk/tags/jsbridge)[#javascriptbridge](https://steveedson.co.uk/tags/javascriptbridge)[#webview](https://steveedson.co.uk/tags/webview)[#java](https://steveedson.co.uk/tags/java)

This follows on from the [previous post](https://steveedson.co.uk/articles/2014-01-21-creating-a-webview-android-app).

### JavaScriptInterface

At this stage, we have a working app that can load a webpage, with Javascript enabled. The next step is to allow us to use the native features on the device, through the webview. Examples of this include:

* Geolocation
* Device information, such as battery levels, network status.
* Controlling hardware, such as the camera and vibration.
* Native sharing, using the apps installed on the device.
* Anything else that the device is capable of.

A new class needs to be created in the `MainActivity.java` file, after the `onCreate()` method, like so:

```
public class jsBridge {
    Context mContext;

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

    /** Show a toast from the web page */
    @JavascriptInterface
    public void showMessage(String message) {
        Toast.makeText(mContext, message, Toast.LENGTH_SHORT).show();
    }
}
```

_Note:_ If eclipse shows a load of errors for some of the variables, hover over the error, and select `import`\_\_\_.

We then assign this to our `webView`, like so

```
webView.addJavascriptInterface(new jsBridge(this), "Android");
```

Then, on our HTML page, we can add the Javascript that would natively show a message on the device:

```
<script type="text/javascript">
    Android.showMessage("Hello!");
</script>
```

There are hundreds more features that can be added to the `JavaScriptInterface`, for example, controlling the camera, recording audio, making the device vibrate, enabling WiFi, using the compass or getting information like the battery level.

For more examples of specific features, leave a comment.

![Steve Edson](https://steveedson.co.uk/_astro/steve-edson-2026-v2.DFDBX9RO_1OKeaR.webp)

Steve Edson

Full Stack Developer based in Sheffield

[Back to all articles](https://steveedson.co.uk/articles)
