Android Question WebView and Javascript (in the WebView) interactions

Erick Kalugdan

Member
Licensed User
How do I...

1. Have my [app] execute and pass parameters to a [Javascript function inside the HTML inside a WebView] object?

2. Have a [Javascript inside the HTML inside a WebView] execute and pass parameters to a [Sub in my app]?
 

Erick Kalugdan

Member
Licensed User
I think it's the one but am a newbie in B4A. When I downloaded the zip file WebViewExtras_v1_42.zip and extracted it, it only contained a JAR and an XML file. I don't know how to open it in B4A.

Do you have a sample app that utilizes WebViewExtras_v1_42.zip?
 
Upvote 0

Roycefer

Well-Known Member
Licensed User
Longtime User
Copy those files into your Extra Libraries folder. Then right-click and hit Refresh in the Libraries tab in the IDE.
 
Upvote 0

Erick Kalugdan

Member
Licensed User
Done. It now showed up in the Libraries tab. Thanks!

I selected the library. Now... how do we use it in the code?

Is it like:

B4X:
Private WebView1 As WebView
executeJavascript(webView1, "sayHello()")
 
Upvote 0

Erick Kalugdan

Member
Licensed User
B4A to WebViewJS worked!

WebViewJS to B4A worked too but I noticed that it can only call Subs without parameters like:

B4A.CallSub('MySubName', true)

But it did work when I call a Sub with parameters like:

B4A.CallSub('MySubName(1,2,3)', true)

Am I missing something here?
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
Have a look at the first post in Martin's library thread https://www.b4x.com/android/forum/threads/webviewextras.12453/

You have to pass the data in a specific format, the second parameter of the call states whether or not the sub will update the GUI. The remaining parameters are all Strings for passing data. So you can only pass 3 string parameters at most. Although these can be JSON strings, so you can wrap an array of simple values in a JSON string using JSONstringify in Javascript and pass it as one parameter and retrieve the values in the array using JSON parse from the B4a JSON library.
 
Upvote 0

Erick Kalugdan

Member
Licensed User
Cool! It worked :)

Now here's another one ... I observed that the "onload" event in a <BODY> tag does not work with a WebView. I wanted the WebView to focus on an input box and open the keyboard when the page loads. Here's my sample code:

HTML:
<script>
function setFocusToTextBox(){
 document.getElementById("mytext").focus();
}
</script>

<body onload="setFocusToTextBox()">
 <input id="mytext" type="number" name="amount">
</body>

When that HTML loads in the WebView, nothing happens.

Am I missing something here?
 
Upvote 0
Top