Skip to content Skip to sidebar Skip to footer

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 valu

Solution 1:

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.


Solution 2:

This worked perfectly for me:

String ABC="50";

myWebView.loadUrl("javascript: (function(){document.getElementById('countLiveSteps').value ='" + ABC + "';})();");

Solution 3:

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


Solution 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


Post a Comment for "In Android, How Can I Set The Value Of A Edit Box In WebView Using Javascript"