Android Question Webview call to B4A

Mark Stuart

Active Member
Licensed User
Longtime User
Hi y'all,

I'm viewing html content in a Webview on a layout.

When scrolling the Webview to the bottom, I want something (javascript) to "notify" B4A that it has reached the bottom. The idea is to set a view (checkbox) Enabled=True.
So it would be a Javascript to B4A callback, I guess.

Searched the forum and found the WebViewExtras, but it only does B4A to Javascript.

Is this even possible to do?
And where would the example code be on the forum?

Thanx,
Mark Stuart
 

LucasHeer

Active Member
Licensed User
Longtime User
Hey sir!

This is what you will want to use:
https://www.b4x.com/android/forum/threads/webviewextras.12453/#post106486

You can add the B4A interface to your WebView using WebViewExtras. This allows your to call procedures both ways:

JS -> B4A
JavaScript:
B4A.CallSub('WebView_PageAtBottom', true);

B4A -> JS
B4X:
MyWebViewExtras.executeJavascript(WebView1, "alert('Hello World!')")

Also! not sure about Texas, but snows about to hit! better hunker down! ⛄
 
Upvote 0

Mark Stuart

Active Member
Licensed User
Longtime User
Hey sir!

This is what you will want to use:
https://www.b4x.com/android/forum/threads/webviewextras.12453/#post106486

You can add the B4A interface to your WebView using WebViewExtras. This allows your to call procedures both ways:

JS -> B4A
JavaScript:
B4A.CallSub('WebView_PageAtBottom', true);

B4A -> JS
B4X:
MyWebViewExtras.executeJavascript(WebView1, "alert('Hello World!')")

Also! not sure about Texas, but snows about to hit! better hunker down! ⛄
Can you give a little more code example of the js > b4a, please? I assume there has to be a Javascript to make that call?
I don't know js to make this happen, where scrolling to the bottom of the webview will trigger the js to make the callback to enable a b4a checkbox.
 
Upvote 0

LucasHeer

Active Member
Licensed User
Longtime User
Can you give a little more code example of the js > b4a, please? I assume there has to be a Javascript to make that call?
I don't know js to make this happen, where scrolling to the bottom of the webview will trigger the js to make the callback to enable a b4a checkbox.

Here is an example that should work (Using WebViewExtras v2.2 library):

B4X:
Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
  
    Private WebView1 As WebView
    Private WebViewExtras1 As WebViewExtras
    Private WebChromeClient1 As DefaultWebChromeClient
    Private JavascriptInterface1 As DefaultJavascriptInterface
End Sub

Public Sub Initialize
'    B4XPages.GetManager.LogEvents = True
End Sub

'This event will be called once, before the page becomes visible.
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
  
    JavascriptInterface1.Initialize
    WebViewExtras1.Initialize(WebView1)
    WebChromeClient1.Initialize("DefaultWebChromeClient1")
  
    'Webview Acceleration
    Dim jo As JavaObject = WebView1
    jo.RunMethod("setLayerType", Array(2, Null))
  
    WebViewExtras1.SetWebChromeClient(WebChromeClient1)
    WebViewExtras1.addJavascriptInterface(JavascriptInterface1, "B4A")
End Sub

Sub B4XPage_Appear
    WebView1.LoadUrl("https://www.b4x.com/")
End Sub

Private Sub WebView1_PageFinished (Url As String)
    Dim bottom_of_page_hook_js As String = ($"
    var bottom_reached = false;
    window.addEventListener('scroll', function() {
          const scrolledTo = window.scrollY + window.innerHeight;
          const totalHeight = document.documentElement.scrollHeight;
          if (scrolledTo >= totalHeight - 1 && bottom_reached == false) {
            bottom_reached = true;
            B4A.CallSub('WebView_PageAtBottom', true);
          }
        });
    "$)
    WebViewExtras1.ExecuteJavascript(bottom_of_page_hook_js)
End Sub

Sub WebView_PageAtBottom
    MsgboxAsync("Bottom of WebView Page Reached!", "Test")
End Sub


You may also want Warwound's WebViewSettings library, it allows for a ton of modifications to the WebView.
 

Attachments

  • AnywhereTest.zip
    16.2 KB · Views: 6
Upvote 0

Mark Stuart

Active Member
Licensed User
Longtime User
Here is an example that should work (Using WebViewExtras v2.2 library):

B4X:
Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
 
    Private WebView1 As WebView
    Private WebViewExtras1 As WebViewExtras
    Private WebChromeClient1 As DefaultWebChromeClient
    Private JavascriptInterface1 As DefaultJavascriptInterface
End Sub

Public Sub Initialize
'    B4XPages.GetManager.LogEvents = True
End Sub

'This event will be called once, before the page becomes visible.
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
 
    JavascriptInterface1.Initialize
    WebViewExtras1.Initialize(WebView1)
    WebChromeClient1.Initialize("DefaultWebChromeClient1")
 
    'Webview Acceleration
    Dim jo As JavaObject = WebView1
    jo.RunMethod("setLayerType", Array(2, Null))
 
    WebViewExtras1.SetWebChromeClient(WebChromeClient1)
    WebViewExtras1.addJavascriptInterface(JavascriptInterface1, "B4A")
End Sub

Sub B4XPage_Appear
    WebView1.LoadUrl("https://www.b4x.com/")
End Sub

Private Sub WebView1_PageFinished (Url As String)
    Dim bottom_of_page_hook_js As String = ($"
    var bottom_reached = false;
    window.addEventListener('scroll', function() {
          const scrolledTo = window.scrollY + window.innerHeight;
          const totalHeight = document.documentElement.scrollHeight;
          if (scrolledTo >= totalHeight - 1 && bottom_reached == false) {
            bottom_reached = true;
            B4A.CallSub('WebView_PageAtBottom', true);
          }
        });
    "$)
    WebViewExtras1.ExecuteJavascript(bottom_of_page_hook_js)
End Sub

Sub WebView_PageAtBottom
    MsgboxAsync("Bottom of WebView Page Reached!", "Test")
End Sub


You may also want Warwound's WebViewSettings library, it allows for a ton of modifications to the WebView.
Does the _PageFinished inject the JS into the html? Not sure I fully understand how it all works.
 
Upvote 0

LucasHeer

Active Member
Licensed User
Longtime User
That's exactly how it works 👍 After b4x.com loads, it injects the event listener code into the page. You can filter the URL as well (so if specific code needs to be injected into specific pages).
 
Upvote 0

Mark Stuart

Active Member
Licensed User
Longtime User
I'm loading a template html assets file with another assets html file that contains formatted text. Reason for this is another story.
The combined html is then displayed in its nicely formatted way.
So I don't have, persay, a URL that can be passed. So how would that work as the required argument for _PageFinished()?
 
Upvote 0

LucasHeer

Active Member
Licensed User
Longtime User
I'm loading a template html assets file with another assets html file that contains formatted text. Reason for this is another story.
The combined html is then displayed in its nicely formatted way.
So I don't have, persay, a URL that can be passed. So how would that work as the required argument for _PageFinished()?
Hey sir! If you are loading HTML, then you can combine the hook/event into your HTML code 👍
 
Upvote 0

Mark Stuart

Active Member
Licensed User
Longtime User
Haven't been able to get the CallSub to work from the JS.
Looks like I need your eyes on it.
Can I do a "Start conversation" with you for the up load just to you?
 
Upvote 0

LucasHeer

Active Member
Licensed User
Longtime User
Haven't been able to get the CallSub to work from the JS.
Looks like I need your eyes on it.
Can I do a "Start conversation" with you for the up load just to you?
Yes sir, that works! Or if you want me to send you a google meets link, you can screenshare real quick. I will be on for about 5-6 more hours 👍
 
Upvote 0
Top