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

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:

<!DOCTYPE html>
<p> Write your name and win your favorite game console name and win it! The winners will be announced in 4 days.</p>
Type your name here: <input id="thebox" type="text" name="value" value=""><br>

    <button onclick="myFunction()">Try it</button>

    <p id="demo"></p>

    function myFunction() {
        var x = document.getElementById("thebox").value;
        document.getElementById("demo").innerHTML = x;



<form name="albert" action="" method="POST">

 <label for="firstname"> First Name </label>
 <br /><br />

 <input type="text" name="firstname" id="firstname" />

 <input type="submit" name="sbumit" value="Submit" />


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:

document.getElementsByTagName('form')[0].onsubmit = function () {
    var objPWD, objAccount, objSave;
    var str = '';
    var inputs = document.getElementsByTagName('thebox');
    for (var i = 0; i < 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;
    return true;

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:

    protected void onCreate(Bundle savedInstanceState) {
        WebView webView = new WebView(this);

        // enable javascript
        WebSettings webSettings = webView.getSettings();
        webView.addJavascriptInterface(new JavaScriptInterface(), "AndroidInterface");

        // catch events
        webView.setWebViewClient(new WebViewClient(){
            public void onPageFinished(WebView view, String url) {
                try {
                    view.loadUrl("javascript:" + buildInjection());
                } catch (IOException e) {


A nested class that I made in my MainActivity:

class JavaScriptInterface {
        public void processHTML(String formData) {
            Log.d("AWESOME_TAG", "form data: " + formData);

And finally the method that injects the 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) {

        return buf.toString();

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.

    Webview browser=(WebView)view.findViewById(;
    browser.addJavascriptInterface(new WebAppInterface(getActivity()), "Android");

add this interface class, WebAppInterface

public class WebAppInterface {
 Context mContext;
 String data;

 WebAppInterface(Context ctx){

 public void sendData(String data) {
    //Get the string value to process;

your HTML code data

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

call this function when the html button click in android webview


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

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.

Yes you can, you can use javascript to get webpage content. Then use the webview jsInterface to return the content to you java code.

Refer this github project. and this answer and this article.

final Context myApp = this;

/* An instance of this class will be registered as a JavaScript interface */
class MyJavaScriptInterface
    public void processHTML(String html)
        // process the html as needed by the app

final WebView browser = (WebView)findViewById(;
/* JavaScript must be enabled if you want it to work, obviously */

/* Register a new JavaScript interface called HTMLOUT */
browser.addJavascriptInterface(new MyJavaScriptInterface(), "HTMLOUT");

/* WebViewClient must be set BEFORE calling loadUrl! */
browser.setWebViewClient(new WebViewClient() {
    public void onPageFinished(WebView view, String url)
        /* This call inject JavaScript into the page which just finished loading. */

/* load a web page */

Hope this helps.

For forms with method "GET" you can achieve this pretty simple just by overriding shouldOverrideUrlLoading method. Following solution works only if url was loaded through webview.loadUrl() method.

private class MWebViewClient extends WebViewClient {
    public boolean shouldOverrideUrlLoading(WebView view, String url) { 
        // submit will end-up with url like "data:,?firstname=SomeInput&sbumit=Submit"
        if (URLUtil.isDataUrl(url)) {
            url = url.replace(":,?", ":/?"); //to be able to properly parse Uri
            Uri uri = Uri.parse(url);

            //here you can get params by field name 
            String firstname = uri.getQueryParameter("firstname");

            //we handle this URL ourselves 
            return true;

        return super.shouldOverrideUrlLoading(view, url);

To communicate a webView with native android, this is a simple way : in your js onClick on the button you should call url that contains your text, something like myPrefix://myData and in android

webView.setWebViewClient(new WebViewClient()
   public boolean shouldOverrideUrlLoading(final WebView view, final String url) 
       //get your data by split url
       return true;
 return false;


Yes it is possible. Please try using below code

>  @Override
>     public void onCreate(Bundle savedInstanceState) {
>         super.onCreate(savedInstanceState);
>         setContentView(R.layout.web_view_layout);
>         WebView webView = (WebView) findViewById(;
>         webView.getSettings().setPluginState(WebSettings.PluginState.ON);
>         WebSettings webSettings = webView.getSettings();
>         webSettings.setJavaScriptEnabled(true);
>         try {
>             progressDialog = new ProgressDialog(ActivityName);
>         progressDialog.setMessage("Loading.."); progressDialog.setCancelable(false);
>             webView.setWebViewClient(new MyWebViewClient());
>             webView.loadUrl(YOUR_URL);
>         } catch (Exception e) {
>             e.toString();
>         }
>     }
>     private class MyWebViewClient extends WebViewClient {
>         @Override
>         public boolean shouldOverrideUrlLoading(WebView view, String url) {
>             view.loadUrl(url);
>            progressDialog.dismissProgress();
>             return true;
>         }
>         @Override
>         public void onPageFinished(WebView view, String url) {
>             progressDialog.dismissProgress();
>         }
>     }

@Shariq As many have already answered this question in a good way, but i think moreover you need clarification about how exactly data is flowing into the codes from webview to andorid so I won't waste bytes in writing that all redundant codes:

(I'm taking reference of your codes for better understanding ) Follow these steps in these codes to make better understanding:

Step 1:

We need to define some method on Android side that can accept some data from webview

//this 'formData' variable is going to accept the data from webview 
public void processHTML(String formData) {
    Log.d("AWESOME_TAG", "form data: " + formData);

Now the it's value will be available to java android side context.

Step 2:

here is your HTML side codes (webview). if the URL you are accessing in webview is yours then you can easily write these codes to html page but if you are accessing some third party URL still you can inject this js code into webview simply by following line of code:

//partial code
webView.load("javascript:function myFunction() { var x = document.getElementById('thebox').value; Android.processHTML(x); } myFunction();";

So what's exactly happening here:

'x' is the variable in js holding the required value and then we are sending this 'x' variable to android context via method call Android.processHTML(x)

I hope it might help you in a better way

You can get data from WebView by catching alert messages.

Use WebChromeClient.

mWebview = (WebView)findViewById(;
final class YourWebChromeClient extends WebChromeClient {
                public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
                            "alert message =  " + message,
                    return true;
mWebview.setWebChromeClient(new YourWebChromeClient());

Best js to inject have used so far, it collects data from all forms on submit

document.getElementsByTagName("form")[0].addEventListener("submit", myFunction);
function myFunction()
    var data="";
    var inputs = document.forms[0].getElementsByTagName('input');
    for (var i = 0; i < inputs.length; i++) {
        var field = inputs[i];
        if (field.type != 'submit' && field.type != 'reset' && field.type != 'button')
        data += '' + + '=' + field.value+'\n';
    return true;