Dhruv Verma
Dhruv Verma

Reputation: 431

Autofilling forms using WebView from android

I am using a WebView to try to fill a form using data that I am sending.

WebView webView;
String url = "http://139.59.34.30/quotation/";
String btn_ci, btn_co, n, a, k, r, m;
Intent in;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_web);
    webView = (WebView) findViewById(R.id.webview);
    in = getIntent();
    btn_ci = in.getStringExtra("checkin");
    btn_co = in.getStringExtra("checkout");
    n = in.getStringExtra("name");
    a = in.getStringExtra("adults");
    k = in.getStringExtra("kids");
    r = in.getStringExtra("rooms");
    m = in.getStringExtra("mail");
    webView.getSettings().setJavaScriptEnabled(true);
    webView.setWebViewClient(new WebViewClient() {
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            view.loadUrl("javascript:document.getElementsByName('name').value = '"+n+
                    "';document.getElementsByName('checkin').value='"+ btn_ci +
                    "';document.getElementsByName('checkout').value='"+ btn_co +
                    "';document.getElementsByName('no_of_pax').value='"+ a +
                    "';document.getElementsByName('no_of_kid').value='"+ k +
                    "';document.getElementsByName('no_of_rooms').value='"+ r +"';");
        }
    });
    webView.loadUrl(url);

But every time the activity executes the page loads for a while with no values filled in and then it just displays the last String I had passed.

Upvotes: 0

Views: 668

Answers (1)

Muthukrishnan Rajendran
Muthukrishnan Rajendran

Reputation: 11622

Instead of your code inside onPageFinished, you can use the follow code, it will work on all Android API versions,

        final String js = "javascript: " +
                "var nameDoc = document.getElementsByName('name');" +
                "nameDoc[0].value = '"+n+ "';" +
                "var checkOutDoc = document.getElementsByName('checkout');" +
                "checkOutDoc[0].value = '"+btn_co+ "';" +
                "var noOFPaxDoc = document.getElementsByName('no_of_pax');" +
                "noOFPaxDoc[0].value = '"+a+ "';" + // a should be int based on ur HTML
                "var noOFKidDoc = document.getElementsByName('no_of_kid');" +
                "noOFKidDoc[0].value = '"+k+ "';" + // a should be int based on ur HTML
                "var noOFRoomsDoc = document.getElementsByName('no_of_rooms');" +
                "noOFRoomsDoc[0].value = '"+r+ "';" + // a should be int based on ur HTML
                "var checkInDoc = document.getElementsByName('checkin');" +
                "checkInDoc[0].value = '"+btn_ci+ "';";

        if (Build.VERSION.SDK_INT >= 19) {
            view.evaluateJavascript(js, new ValueCallback<String>() {
                @Override
                public void onReceiveValue(String s) {

                }
            });
        } else {
            view.loadUrl(js);
        }

Upvotes: 1

Related Questions