0

Say I have two variables, x and y and I want to pass them from MyActivity to JavaScript. Here's my code:

public class myNewActivity extends Activity {
    double x;
    double y;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_new);

        // get extras passed from another activity
        Bundle extras = getIntent().getExtras();
        if (extras != null) {
            x = extras.getDouble("x");
            y = extras.getDouble("y");
        }

        WebView webView = (WebView)findViewById(R.id.webView);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.setWebChromeClient(new WebChromeClient());
        //webView.loadUrl("javascript:setX('"+x+"')");
        //webView.loadUrl("javascript:setY('"+y+"')");
        webView.loadUrl("file:///android_asset/index.html");    
    }

I was trying something with the two commented lines but was just crashing the app. This is the JavaScript:

var x, y;
    function init(){
        // ... here I need the values of X and Y

    }
    function setX(ex) {
        x = ex;
    }
    function setY(ey) {
        y = ey
    }
4
  • developer.android.com/guide/webapps/webview.html Commented May 9, 2014 at 4:53
  • You would run the loadUrl for your js after the page has loaded (in onPageCompleted()). Instead of implementing setX and setY, just provide a setXY -- one less call. Commented May 9, 2014 at 4:54
  • @323go But don't I need the values before if I want to display them on the page I'm loading? Is there some other way? Commented May 9, 2014 at 4:59
  • "But don't I need the values before if I want to display them on the page I'm loading?" I don't know, do you? It depends on how your html looks. If yes, then you might need to load the index.html first, modify it in memory and load it with loadData(). Commented May 9, 2014 at 5:04

1 Answer 1

2

You need to implement javascriptinteface in webview like this;

WebView webView = (WebView) findViewById(R.id.webView);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.setWebChromeClient(new WebChromeClient());

    Bundle extras = getIntent().getExtras();
    double x = 0, y = 0;
    if (extras != null) {
        x = extras.getDouble("x");
        y = extras.getDouble("y");
    }
    webView.addJavascriptInterface(new JavaScriptInterface(x, y), "JSInterface");
    webView.loadUrl("file:///android_asset/index.html");

Create This Javainteface in your activity

public class JavaScriptInterface {

        double x, y;

        JavaScriptInterface(double x, double y) {
            this.x = x;
            this.y = y;
        }

        public double getXValue() {
            return x;
        }

        public double getYValue() {
            return y;

        }
    }

In you index.html

var x, y;
    function init(){
       x = JSInterface.getXValue();
       y = JSInterface.getYValue();
    }
Sign up to request clarification or add additional context in comments.

3 Comments

the Bundle extra is always null !! what to do ?
@Suda.nese Answer given is in respect to asked question. It is example how to communicate with javascript. You need to pass from previous activity in bundle extras to read here.
Thanks for your reply. In fact I wasn't passing any extras and the bundle was null. Now you answer works for me. Thanks again for your solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.