saloua
saloua

Reputation: 329

Javascript not working on webview

I am developing an Android application and I have trouvble making javascript work. Here is my main activity code :

    protected void onCreate(Bundle savedInstanceState) {
        Log.i(TAG, "create LocalDialogActivity");

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_local_dialog);

        webView = (WebView)findViewById(R.id.local_dialog_webview);
        webView.setWebChromeClient(new WebChromeClient());

        webView.getSettings().setUseWideViewPort(true);
        webView.getSettings().setLoadWithOverviewMode(true);

        webView.getSettings().setSupportZoom(true);
        webView.getSettings().setBuiltInZoomControls(true);
        webView.getSettings().setDisplayZoomControls(false);

        webView.getSettings().setJavaScriptEnabled(true);
           webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);


       }
}

private class WebAppInterface {
           @JavascriptInterface
            public void validate() {
                  //do some action...
            }

        @JavascriptInterface
        public void cancel() {
            //do some actions...
        }
}

here is my html code :

<!DOCTYPE html>
<html>
    <head>
        <link rel="stylesheet" type="text/css" href="../css/style.css">
        <script src="../js/myjavascript.js"></script>
    </head>
    <body>


    <section id="header">   
    </section>


    <section id="buttons">
            <button class="button" id="no" onclick="cancel()">cancel</button>
            <button class="button" id="yes" onclick="validate()">validate</button>
    </section>

    <footer id="footer">
    </footer>
</body>

</html>

When I click on my buttons, the functions validate() and cancel() doesn't work.

here is my javascript code :

function validate() {
    Interface.validate();
}

function cancel() {
    Interface.cancel();
}

Interface is my javascriptInterface in my Android code.

Any ideas would be welcome.

Upvotes: 2

Views: 511

Answers (2)

Bonatti
Bonatti

Reputation: 2781

From my usage of Android webview, check if you are at "default" zoom level. For instance, if you zoom in/out and then press a button, you cannot press it, but instead pan the zoom at the position you clicked.

If that is not your issue, attemp the following:

Check if the script is being read correctly. Do this by adding the cancel()/validate() functions to the header of the HTML...

Check if the document is being fully loaded. Do this by adding something like functcion initialize(){alert("loaded");} at window.onload = initialize();

If it is still failling, then post your Manifest, and any logcat should an error happen, as well as the reference to the webview (the full class where the webview is used, or at least all variables that are used with it).

Upvotes: 0

IntelliJ Amiya
IntelliJ Amiya

Reputation: 75778

Just add

webView.getSettings().setDomStorageEnabled(true);

Upvotes: 2

Related Questions