Is it possible to get data from HTML forms into android while using webView?

    Webview browser=(WebView)view.findViewById(R.id.webChart);
    browser.getSettings().setJavaScriptEnabled(true);
    browser.addJavascriptInterface(new WebAppInterface(getActivity()), "Android");
    browser.loadUrl("file:///android_asset/yourHtmlFileName.html");

add this interface class, WebAppInterface

public class WebAppInterface {
 Context mContext;
 String data;

 WebAppInterface(Context ctx){
    this.mContext=ctx;
 } 


 @JavascriptInterface
 public void sendData(String data) {
    //Get the string value to process
      this.data=data;
 }
}

your HTML code data

 function loadChartData() {
  var x = document.getElementById("thebox").value;
   Android.sendData(x);
 }

call this function when the html button click in android webview

UPDATE

1) By default javascript is disabled in webview . to enable it, get the settings of the webview and call the setJavaScriptEnabled(true); to true.

2) to create the interface between your Javascript code and your android code, you need to create Javacript interface class.

3) bind the interface between your javascript code to android code, you need to pass the reference of the interface class and an interface name that your javaScript can call to access the class.

4) pass the html file path to load into the webview(browser).

5) create the interface class like below(WebAppInterface).

see this link for more details
https://developer.android.com/guide/webapps/webview.html

6) in HTML file, create the button and add the click listener to that button and call the sendData(“your value”) function with interface name(Here Android).

Thats all. you can pass the value from html to your android code.

Leave a Comment