We can manipulate Webview content or get any input value by injecting javascript. We need some knowledge of DOM element and javascript to manipulate Webview content.
In this blog, We will inject javascript function to get the value of the form field and manipulate DOM properties.
1. Enable javascript in Webview
1 |
webview.getSettings().setJavaScriptEnabled(true); |
2. Set WebViewClient and override onPageFinished method. We have injected javascript to get DOM element value.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
webView.setWebViewClient(new WebViewClient() { @Override public void onPageFinished(WebView view, String url) { view.evaluateJavascript("(function(){return document.getElementsByName(\"PaRes\")[0].value;})();", paRes -> { if (paRes != null && !paRes.isEmpty() && !paRes.equals("null")) { // TODO: You will use get value } }); super.onPageFinished(view, url); } } ); |
You can manipulate DOM properties as mentioned below.
1 2 3 4 5 6 7 8 9 |
webView.setWebViewClient(new WebViewClient() { @Override public void onPageFinished(WebView view, String url) { String js = "javascript:document.body.style.background = " + "#FF3F60DA" + ";"; view.loadUrl(js); } } ); |
Hope, you have got the idea to get the value of the form field and manipulate DOM properties.