Android Question Dynamically change HTML element in android webView

cenyu

Active Member
Licensed User
Longtime User
Hi, i have a HTML file with code like this

Code: Copy
<div id='ver'>__APP_VER__</div>

I have one button1 into Activity Main and WebView witch display HTML file when load activity...
Now the question!
How to change div content when press button1?

Thanks!
 
Last edited:
I try and

Code: Copy
Dim Javascript As String
    Javascript = "javascript: document.getElementById('sid').value('4.0')"
    WebViewExtras1.executeJavascript(WebView1, Javascript)

But only return "Uncaught ReferenceError: DoFormat is not defined in file:///# (Line: 1)"
 
Last edited:
Upvote 0
You can try:
Code: Copy
Dim Javascript As String
Javascript = "var sid=document.getElementById('sid'); sid.value='4.0';"
WebViewExtras1.executeJavascript(WebView1, Javascript)

In your first post I see a jquery call. This can't be used in your case as far as i can tell. You should try:
Code: Copy
Dim Javascript As String
Javascript = "var ver=document.getElementById('ver');ver.innerHTML='2';"
WebViewExtras1.executeJavascript(WebView1, Javascript)
 
Upvote 0
Thanks, thanks, thanks for answer mc73!

Its work but only in debug mode...
When i compile apk i recevie error - "All WebView methods must be called on same thread"

But still....this is move from groud zero :-)

Thanks!
 
Last edited:
Upvote 0
IMO, at first it's necessary to discuss 'a desiign'. I'd say there are 3 different situations:

1) your program creates HTML from internal resources (for example, your show html help or output results)
In this case all is simple. LoadHtml + OverrideUrl + (sometimes) minimum javascript knowledge to access fields in forms.

2) your program creates HTML from external website, which belongs to your project. In this case you need to agree about protocol.
All the rest is like in point 1

3) your program takes information from external website, which is not under your control.

This is very bad situation, because external resource may change output in any moment.
 
Upvote 0
No, i my situation i use 1) my program read HTML from internal resources. I will show my code:
Code: Copy
Sub Activity_Resume
    SetupWebView
End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub Activity_KeyPress (KeyCode As Int) As Boolean

 
    If KeyCode = KeyCodes.KEYCODE_BACK Then
  
        ExitApplication
     End If
  
    Return True
  
  
End Sub


Sub SetupWebView
  
    WebView1.Initialize("WebView1")
    Activity.AddView(WebView1, 0, 0, 100%x, 100%y)
    WebView1.JavaScriptEnabled = True
    WebView1.ZoomEnabled=True
    'add the B4A javascript interface to the WebView
    WebViewExtras1.addJavascriptInterface(WebView1, "B4A")
    'много е важно че без него браузъра не си знае енкодинга
    WebSettings.setDefaultTextEncodingName(WebView1,"utf-8")
    WebView1.LoadHtml(File.ReadString(File.DirAssets , "home.html"))
    WebViewExtras1.addWebChromeClient(WebView1,"Event")
End Sub


'Вика се от HTML файла с javascript code
Sub ExitApp_Request(var1 As String)
    'Log($"check_data(${var1})"$)
    ExitApplication
End Sub



'Вика се от HTML файла с javascript code
Sub Book_Request(bookId As String)
 
    Dim Javascript As String = $"B4A.CallSub('Process_HTML', true, document.documentElement.outerHTML)"$
    WebViewExtras1.executeJavascript(WebView1, Javascript)
End Sub


private Sub Process_HTML(Html As String)
  
    Log("trying to set the form")
    Dim javascript As String = "var sid=document.getElementById('sid'); sid.value = '4321';" 'document.forms.myForm.submit();"
    WebViewExtras1.executeJavascript(WebView1, javascript)
  
  
End Sub


Everithing work in Debug mode only...
I read this link too https://www.b4x.com/android/forum/threads/webview-extra-execute-javastring-issue.65084/#content


And i want to tanks you for your time!
 
Upvote 0
From internal resources means that you can work with html like with string.
For instance, read html file into string (let's say, stringHtml), execute stringHtml.replace ("__APP_VER__", stringMyValue) and WebView.LoadHtml (stringHtml)
Of course, it's necessary to worry about encoding special symbols in stringMyValue (I mean <, >, space, quots etc.)
 
Upvote 0
but i want to use executeJavascript
no one stops you to do it
When i compile apk i recevie error - "All WebView methods must be called on same thread"
Sounds like a problem in your code.

Upload a small project which shoes the issue.
 
Upvote 0
Except myself :)
I can't find any problem into my code...
I should use CallSubDelayed

I will upload small project!
 
Upvote 0
This is small project with given problem. Here everithing work only on debug mode
 

Attachments

Last edited:
Upvote 0
Code: Copy
Sub Activity_Resume
    Book_Request("")
End Sub
Sub Book_Request(bookId As String)
    Dim javascript As String = "var sid=document.getElementById('sid').value = '4321'; " 'document.forms.myForm.submit();"
    WebViewExtras1.executeJavascript(WebView1, javascript)
End Sub

PD: I loaded the webview with the designer
 
Upvote 0
the javascript call to b4a works if you use

Code: Copy
<!DOCTYPE HTML>
<head>
</head>
<body>
  <a class="header-2" href="javascript:B4A.CallSub('Book_Request', true, 'var1');">Change value</a>
  <input type = 'text' id='sid' value='555'></input>
</body>
TRUE istead of FALSE
 
Upvote 0
Book_Request running by clicking the link in html....

Code: Copy
Sub Book_Request(bookId As String)
    Dim javascript As String = $"var sid=document.getElementById('sid').value = '${bookId}'; "$ 'document.forms.myForm.submit();"
    WebViewExtras1.executeJavascript(WebView1, javascript)
End Sub
 
Upvote 0
Back
Top