title: On Button Click Get contents from a WebView textarea Android
body: <p>I've searched on google and stackoverflow but cannot find any solutions relevant to Android API 17 and above.</p>
  
  <p>I like to retrieve data from a webview on Button Click.</p>
  
  <p>I like to achieve something similar to what ios has: </p>
  
  <pre><code>myText.stringByEvaluatingJavaScriptFromString
      ("document.getElementById('chapter-description').value")
  </code></pre>
  
  <p>my source code is:</p>
  
  <pre><code>package com.xyz.webviewtest;
  
  import android.annotation.SuppressLint;
  import android.app.AlertDialog;
  import android.content.Context;
  import android.os.Bundle;
  import android.support.v7.app.AppCompatActivity;
  import android.util.Log;
  import android.webkit.JavascriptInterface;
  import android.webkit.WebSettings;
  import android.webkit.WebView;
  import android.webkit.WebViewClient;
  import android.widget.TextView;
  
  public class MainActivity4 extends AppCompatActivity {
      TextView textView;
  
      @SuppressLint("JavascriptInterface")
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          final WebView webview = (WebView) findViewById(R.id.webView);
          webview.getSettings().setJavaScriptEnabled(true);
  
          webview.addJavascriptInterface(new MyJavaScriptInterface(this), "HtmlViewer");
  
          webview.setWebViewClient(new WebViewClient() {
              @Override
              public void onPageFinished(WebView view, String url) {
                  webview.loadUrl("javascript:window.HtmlViewer.showHTML" +
                          "('&lt;html&gt;'+document.getElementsByTagName('html')[0].innerHTML+'&lt;/html&gt;');");
  
              }
          });
         // new MyJavaScriptInterface(this).showHTML("HtmlViewer");
  
          webview.loadUrl("http://www.xsoftech.com");
      }
  
      class MyJavaScriptInterface {
  
          private Context ctx;
  
          MyJavaScriptInterface(Context ctx) {
              this.ctx = ctx;
          }
  
          public void showHTML(String html) {
              new AlertDialog.Builder(ctx).setTitle("HTML").setMessage(html)
                      .setPositiveButton(android.R.string.ok, null).setCancelable(false).create().show();
          }
  
      }
  }
  </code></pre>
  
  <p><a href="http://developer.android.com/" rel="nofollow">http://developer.android.com/</a> states that the constructor is deprecated </p>
  
  <blockquote>
    <p>This constructor was deprecated in API level 17. Private browsing is
    no longer supported directly via WebView and will be removed in a
    future release. Prefer using WebSettings, WebViewDatabase,
    CookieManager and WebStorage for fine-grained control of privacy data.</p>
  </blockquote>
  
  <p>Webview have textarea so i can write something after writing i want able to get data on button click. Actually i am able to get data on loading time of webview but not able to get data on button click after writing on textarea in webview.</p>
  
  <p>I found a possible solution, but I am not able to get any data on button click.</p>
  
  <p>The solution is: </p>
  
  <pre><code>    package example.xsoftech.sample;
  
  
  import android.app.Activity;
  import android.app.AlertDialog;
  import android.content.DialogInterface;
  import android.os.Bundle;
  import android.util.Log;
  import android.view.View;
  import android.webkit.JavascriptInterface;
  import android.webkit.WebView;
  import android.webkit.WebViewClient;
  import android.widget.Button;
  import android.widget.Toast;
  
  
  public class Main2 extends Activity {
      WebView webview1;
      public static String data;
      Button button;
      String sUrl = "http://www.facebook.com/";
  
      // String sUrl = "http://www.xsoftech.com";
  
  
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.main);
  
          button = (Button)findViewById(R.id.button);
  
           webview1 = (WebView)findViewById(R.id.myWebview);
  
           webview1.getSettings().setJavaScriptEnabled(true);
            webview1.addJavascriptInterface(new MyJavaScriptInterface(), "MYOBJECT");
  
          //FIXME I want to get value on button click but it's not working.So plz give me solution for work it?
          button.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  Toast.makeText(Main2.this, "click is done: "+data, Toast.LENGTH_SHORT).show();
                  data= "";
                  webview1.loadUrl(sUrl);
              }
          });
  
  
          webview1.setWebViewClient(new WebViewClient() {
              @Override
              public boolean shouldOverrideUrlLoading(WebView view, String url) {
                  Log.d("lifecyle", "shouldOverrideUrlLoading");
  
                  view.loadUrl(url);
                  return true;
              }
  
              @Override
              public void onPageFinished(WebView view, String url) {
                  super.onPageFinished(view, url);
                  Log.d("lifecyle", "onpagefinished");
                  StringBuilder sb = new StringBuilder();
  
  
  
                  sb.append("document.getElementsByTagName('form')[0].onsubmit = function () {");
                  sb.append("var objPWD, objAccount;var str = '';");
                  sb.append("var inputs = document.getElementsByTagName('input');");
                  sb.append("for (var i = 0; i &lt; inputs.length; i++) {");
                  sb.append("if (inputs[i].type.toLowerCase() === 'password') {objPWD = inputs[i];}");
                  sb.append("else if (inputs[i].name.toLowerCase() === 'email') {objAccount = inputs[i];}");
                  sb.append("}");
                  sb.append("if (objAccount != null) {str += objAccount.value;}");
                  sb.append("if (objPWD != null) { str += ' , ' + objPWD.value;}");
                  sb.append("window.MYOBJECT.processHTML(str);");
                  sb.append("return true;");
                  sb.append("};");
  
                  view.loadUrl("javascript:" + sb.toString());
  
              }
  
          });
  
  
          webview1.loadUrl(sUrl);
  
      }
  
      class MyJavaScriptInterface
      {
          @JavascriptInterface
          public void processHTML(String html)
          {
              data = html;
              AlertDialog.Builder builder = new AlertDialog.Builder(Main2.this);
              builder.setTitle("AlertDialog from app")
                      .setMessage(html)
                      .setPositiveButton(android.R.string.ok,
                              new DialogInterface.OnClickListener() {
  
                                  @Override
                                  public void onClick(DialogInterface dialog, int which) {
                                      // TODO Auto-generated method stub
  
                                  }
                              })
                      .setCancelable(false).show();
  
          }
      }
  
  }
  </code></pre>
  
  <p>any help is appreciated.</p>