title: Is it possible to get data from HTML forms into android while using webView?
body: <p>I'm making a very simple form in HTML which is viewed in android using the webview which takes in your name using a textbox and when you click on the button, it displays it into a paragraph and it's made using both html and javascript.
  This is my html code:</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
  &lt;body&gt;
  &lt;p&gt; Write your name and win your favorite game console name and win it! The winners will be announced in 4 days.&lt;/p&gt;
  Type your name here: &lt;input id="thebox" type="text" name="value" value=""&gt;&lt;br&gt;
  
      &lt;button onclick="myFunction()"&gt;Try it&lt;/button&gt;
  
      &lt;p id="demo"&gt;&lt;/p&gt;
  
      &lt;script&gt;
      function myFunction() {
          var x = document.getElementById("thebox").value;
          document.getElementById("demo").innerHTML = x;
      }
      &lt;/script&gt;
  
      &lt;/body&gt;
      &lt;/html&gt;
  </code></pre>
  
  <p><strong>NEW EDITED FORM</strong></p>
  
  <pre><code>&lt;form name="albert" action="" method="POST"&gt;
  
   &lt;label for="firstname"&gt; First Name &lt;/label&gt;
   &lt;br /&gt;&lt;br /&gt;
  
   &lt;input type="text" name="firstname" id="firstname" /&gt;
  
   &lt;input type="submit" name="sbumit" value="Submit" /&gt;
  
  
  &lt;/form&gt;
  </code></pre>
  
  <p>I want to get the value from the input box called "thebox" in a variable in android on the button click and I tried lots of stuff before and I followed a method where you inject a JS file but since I know nothing about JS so I did fail trying that and here is the file that I put in my project and the file is called inject.js:</p>
  
  <pre><code>document.getElementsByTagName('form')[0].onsubmit = function () {
      var objPWD, objAccount, objSave;
      var str = '';
      var inputs = document.getElementsByTagName('thebox');
      for (var i = 0; i &lt; inputs.length; i++) {
          if (inputs[i].name.toLowerCase() === 'thebox') {
              objAccount = inputs[i];
          }
      }
      if(objAccount != null) {
          str += objAccount.value;
      }
      if(objPWD != null) {
          str += ' , ' + objPWD.value;
      }
      if(objSave != null) {
          str += ' , ' + objSave.value;
      }
      window.AndroidInterface.processHTML(str);
      return true;
  };
  </code></pre>
  
  <p>And later as I followed that article it said that I need to put some stuff in my MainActivity but since I'm using webview for the first time, I couldn't understand much and heres the code I put into my MainActivity:</p>
  
  <pre><code> @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          WebView webView = new WebView(this);
          this.setContentView(webView);
  
          // enable javascript
          WebSettings webSettings = webView.getSettings();
          webSettings.setJavaScriptEnabled(true);
          webView.addJavascriptInterface(new JavaScriptInterface(), "AndroidInterface");
  
          // catch events
          webView.setWebViewClient(new WebViewClient(){
              @Override
              public void onPageFinished(WebView view, String url) {
                  try {
                      view.loadUrl("javascript:" + buildInjection());
                  } catch (IOException e) {
                      e.printStackTrace();
                  }
              }
          });
  
          webView.loadUrl("http://someurl.com");
      }
  </code></pre>
  
  <p>A nested class that I made in my MainActivity:</p>
  
  <pre><code>class JavaScriptInterface {
          @JavascriptInterface
          public void processHTML(String formData) {
              Log.d("AWESOME_TAG", "form data: " + formData);
          }
      }
  </code></pre>
  
  <p>And finally the method that injects the code:</p>
  
  <pre><code>private String buildInjection() throws IOException {
          StringBuilder buf = new StringBuilder();
          InputStream inject = getAssets().open("inject.js");// file from assets
          BufferedReader in = new BufferedReader(new InputStreamReader(inject, "UTF-8"));
          String str;
          while ((str = in.readLine()) != null) {
              buf.append(str);
          }
          in.close();
  
          return buf.toString();
      }
  </code></pre>
  
  <p>I want to get value from the html form(the-input-box) that I show in a webview in Android and is it really possible to do that and if yes how and please explain? Thanks and also please tell in what variable will I get the value.</p>