Chevan Chevan - 1 year ago 140
Android Question

How to pass a value/variable from an android java class to javascript/.html file in android 4.4.2 kitkat

I need to pass a value/variable (e.g. integer, string) from my java class to the .html file inside android assets. Let's say it is an integer value based on the user's click. I need to pass this integer to the .html file(scripts) so as to determine which ajax call is to be made (via an if-else loop). I googled everywhere but I couldn't find an answer to this.

I need to achieve this on android 4.4.2. Thanks in advance.

Answer Source

Have you seen this You are interested in Binding JavaScript code to Android code section. First declare interface, then implement the method that will return your integer. Then inside your JS code call this method to get the value.


Interface for working with JS:

public class WebAppInterface {
    Context mContext;

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

    /** Get the value */
    public int getValue() {
        return value

Add this interface to your WebView:

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

Inside your JS code:

var value = Android.getValue();
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download