Detect button clicked event and fill form in an android webview

934 views Asked by At

As per my title I would like in my WebView to detect a a click on a button and fill the username password fields automatically. Here is what I have:

@Override
public void onPageFinished(final WebView view, String url) {
    String user = "user";
    String pwd = "password";

    view.addJavascriptInterface(new Object() {
        @JavascriptInterface
        public void performClick() throws Exception {
            Log.d("LOGIN::", "Clicked from on finished");
            Toast.makeText(getApplicationContext(), "Login clicked", Toast.LENGTH_LONG).show();

            String js = "javascript:document.getElementById('login')[0].value = '" + user+ "';document.getElementById('login')[1].value='" + pwd+ "';";
            view.evaluateJavascript(js, new ValueCallback<String>() {
                @Override
                public void onReceiveValue(String s) {
                    Log.d("form filled javascript", s); // Prints the string 'null' NOT Java null
                }
            })
        }
    }, "fill");

I was able to detect the click on the button but not able to fill the form.

1

There are 1 answers

1
josedlujan On

Use a JavaScript interface from Android to JavaScript, check documentation.