Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Android, how can I set the value of a edit box in WebView using Javascript

I understand how to set the value of a edit box in WebView, and in a browser on PC with Javascript.

It is basically first find out the ID of the edit box (text), then set the value using: On PC:

document.getElementById('xxxxxxxx').value = 'test'

In Android:

mWebView.loadUrl("javascript: document.getElementById('xxxxxxxx').value = 'test'");

This script works in PC, but when I call the script in WebView.loadUrl, it just override my entire page to "test".

It is weird to me because the following android code works on the radio button, but when it comes to the edit box, it just fails.

mWebView.loadUrl("javascript: document.getElementById('xxxxxxxx').checked = true");

Could you guys help me analyze what could be the possible reason? Thank you so much.

To make it clear, I am running the javascript code in WebViewClient.onPageFinished(WebView view, String url). So the WebView should be fully loaded. That also makes sense because I can check the radio button.

like image 306
wwnigel Avatar asked Jul 15 '14 22:07

wwnigel


People also ask

Does WebView support JavaScript?

JavaScript is disabled in a WebView by default. You can enable it through the WebSettings attached to your WebView . You can retrieve WebSettings with getSettings() , then enable JavaScript with setJavaScriptEnabled() . WebView myWebView = (WebView) findViewById(R.

How do I display HTML content in WebView?

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml. In the above code, we have taken web view to show html content.

How do I send a post request in WebView?

webView. setWebViewClient(new WebViewClient(){ public void onPageStarted(WebView view, String url, Bitmap favicon) { super. onPageStarted(view, url, favicon); } public boolean shouldOverrideUrlLoading(WebView view, String url) { webView. postUrl(Base_Url, postData.

Which method is used to load HTML content in WebView?

The loadUrl() and loadData() methods of Android WebView class are used to load and display web page.


2 Answers

The problem is solved. It was because of the way I call javascript in java code.

code like

mWebView.loadUrl("javascript: [your javascript code]");

sometimes work, but it doesn't work in all the case. At least it doesn't work for the following case when I tried to set the textbox in a webview:

mWebView.loadUrl("javascript: document.getElementById('xxxxxxxx').value= 'test'");

but it should work if you call the javascript as a function:

mWebView.loadUrl("javascript: (function() {document.getElementById('xxxxxxxx').value= 'test';}) ();" );

That fixed my problem. Hope it is helpful.

like image 126
wwnigel Avatar answered Sep 28 '22 08:09

wwnigel


This worked perfectly for me:

String ABC="50";

myWebView.loadUrl("javascript: (function(){document.getElementById('countLiveSteps').value ='" + ABC + "';})();");
like image 30
Indika Ruwan Senavirathne Avatar answered Sep 28 '22 07:09

Indika Ruwan Senavirathne