title: Android webview: inject a function call to the onclick event of a checkbox
body: <p>I have the following html code loaded in a webview:</p>
  
  <pre><code>&lt;form&gt;
      &lt;input checked="true" id="myCheckBox" onchange=
      "location.href='/doSomething.html'" type=
      "checkbox"&gt;My Checkbox description
  &lt;/form&gt;
  </code></pre>
  
  <p>I can not modify this the html code because I'm not the owner of the web site. What I want to do is to receive in my Android app if the user clicks on the checkbox, and the value of the checkbox (checked or not).</p>
  
  <p>I need to manipulate the state of the check box too, but this was easy.</p>
  
  <p>I'm using the following code:</p>
  
  <pre><code>    webView.getSettings().setJavaScriptEnabled(true);
      webView.addJavascriptInterface(new JsObject(), "injection");
  
      webView.setWebViewClient(new WebViewClient() {
  
          @Override
          public void onPageFinished(WebView view, String url) {
              super.onPageFinished(view, url);
  
              webView.loadUrl(
                      "javascript:(function() { " +
                              "var ch=document.getElementById('myCheckBox');" +
                              "ch.checked=false; " + //This works!!!
                              "injection.reportCheckboxState('111111');" + //This works too
                              "function myFunction() {" +
                              "  injection.reportCheckboxState('222222'); " +
                              "}"+
                              "ch.onClick=myFunction;"+
                              //"ch.addEventListener(\"onClick\", myFunction);"+
                              "})()"
              );
          }
  
  
      @Override
          public boolean shouldOverrideUrlLoading(WebView view, String url) {
              boolean returnVal = false;
              if (url.equalsIgnoreCase("/doSomething.htm")) {
                  //I can intercept the onClick event on this checkbox, but I don't know how to read the value of the checkbox here
  
  
              } else {
                  returnVal = true;
              }
              return returnVal;
          }
      });
  
  
      private class JsObject {
      @JavascriptInterface
      public void reportCheckboxState(String string) {
          Log.d(TAG, "" + string);
      }
  }
  </code></pre>
  
  <p>What I'm trying to do is to inject some Javascript into this HTML form. The first lines of the code are working right:</p>
  
  <pre><code>"var ch=document.getElementById('myCheckBox');" +
                              "ch.checked=false; " +
                              "injection.reportCheckboxState('111111');"
  </code></pre>
  
  <p>I receive in my android log console the string "111111". And the ch.cheched=false works too.</p>
  
  <p>But I'm unable to instruct the checkbox that it should call the injection.reportCheckboxState method on "onClick" event (when the user touches the checkbox)".</p>
  
  <p>Can anybody help me? Is it possible to do this?</p>
  
  <p>Thanks ind advance.</p>