0
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {

        webView.evaluateJavascript("alert('hi'); var elem = 
        document.querySelector('.price__container__discount__sales_price').firstChild.innerHTML;" 
        ,null);

        btn.setText(elem);
}

i want use elem variable that declare in js into java code in setText methode

1 Answer 1

1

If you want to get a value from your javascript code loaded into your webview, I would expose a method with the @JavascriptInterface annotation that would gather that value from your javascript.

An example of how to do that is below:

NATIVE

//Make sure to enable these settings
  webviewSettings.setJavaScriptEnabled(true);
  addJavascriptInterface(YOUR_INTERFACE_CLASS, YOUR_INTERFACE_NAME);

 @JavascriptInterface
    public void getSalesPrice(String value) {
        //Here you will get the value from your javascript
    }

JAVASCRIPT

function sendDataToNative(data) {
        YOUR_INTERFACE_NAME.getSalesPrice(data);
    }
Sign up to request clarification or add additional context in comments.

Comments

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.