6

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.

wwnigel
  • 497
  • 7
  • 14
  • Did you ever figure this out? I am having the same problem, but only if I try to insert a string. If the insertion value is a number, it works as expected. My problem is that the web page expects values that are zero padded. Using a number that is shorter than the full length causes the page (not under my control) to fail. So: – Thinman Oct 11 '14 at 02:56
  • continuing... `webView.loadUrl("javascript: document.form1.TextBox1.value = 012345678;");` inserts 12345678 (almost what I want) into the box, but `webView.loadUrl("javascript: document.form1.TextBox1.value = '012345678';");` fails as the OP described. – Thinman Oct 11 '14 at 03:10
  • Hi @Thinman, the problem was solved. I have added the solution as the answer. Hope it is helpful for you. – wwnigel Oct 12 '14 at 07:11

4 Answers4

25

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.

wwnigel
  • 497
  • 7
  • 14
  • 1
    U saved my day...this works on 4.4.4 >= ( Tested on moto g runnig on 4.4.4 ) if u want to set value in webview.otherwise whole page get refreshed by value...!!Weired..but tested. – TheFlash Jun 11 '15 at 05:56
  • You saved my 1 week and thousands of dollars, I wish I could give you 10000 points! Thank you again. – Fenix Apr 17 '17 at 02:04
  • Don't forget that set this line of code : webView.getSettings().setJavaScriptEnabled(true); in onCreate() before set those javascript codes . – Hamid Reza Ansari Sep 18 '19 at 18:24
3

This worked perfectly for me:

String ABC="50";

myWebView.loadUrl("javascript: (function(){document.getElementById('countLiveSteps').value ='" + ABC + "';})();");
Clíodhna
  • 818
  • 1
  • 15
  • 29
0

I would imagine you are running the command before the page is loaded or it cannot resolve document itself. You should write this code as a function included in your document and then have android do:

mWebView.loadUrl("javascript: myFunction('test')");

Or better still, use @JavascriptInterface

Nick Cardoso
  • 20,807
  • 14
  • 73
  • 124
  • Thanks, but I am running the script in WebViewClient.onPageFinished(WebView view, String url). So the webpage should be fully loaded. – wwnigel Jul 15 '14 at 23:26
  • For this problem, I don't need to use the javascript interface since the script is only one line, and I don't rely on any java code to execute it. – wwnigel Jul 15 '14 at 23:53
-4

Use the setText() function of TextView as in:

TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(message);

Where 'message' is a String variable, this will put the value of message into the textView box replacing anything that was in there previously. I got this example from the code at the bottom of this page https://developer.android.com/training/basics/firstapp/starting-activity.html

EditText uses the same setText() method

oniel
  • 21
  • 3
  • It looks like they are using webview, so its a web input, not an edit box so this answer isnt relevant – Nick Cardoso Jul 15 '14 at 23:16
  • Hi @user3838603, I am afraid I didn't make it very clear. The problem I have is with the WebView, how to automatically fill an editbox in a webpage, which is usually implemented in WebView. That's why I need to use javascript and I had some problem in using the javascript in WebView. – wwnigel Jul 15 '14 at 23:17