Android Question How to call B4A sub from html loaded into WebView

cenyu

Active Member
Licensed User
Longtime User
Hi, I have to call B4A sub from html loaded into WebView. I am using this libs:
  • WevViewExtras2 version 2.20
  • WevViewSettings version 1.30
I am using B4A version 10.

I am using WevViewExtras2 becouse i have to load https web page from specific url...
But into my app i first must check internet connection and show error page when there is no connection...
Into this web page i have one button "ExitApp"
My error.html is into file:///android_asset folder


This is my html code:
HTML:
  <div class="fixed-footer">
                <div class="row">
                    <div class="col-6">
                        <a href="javascript:B4A.CallSubPlus('AppClose_Request', true,'1');" class="btn btn-secondary btn-lg btn-block">ExitApp</a>
                    </div>
                    <div class="col-6">
                        <a href="javascript:B4A.CallSubPlus('AppReload_Request', true,'1');" class="btn btn-primary btn-lg btn-block">Reload</a>
                    </div>
                </div>
            </div>
        </div>



And this i my B4A code:

B4X:
Sub Activity_Create(FirstTime As Boolean) 
  
    WebView1.Initialize("WebView1")
     WebViewExtras1.Initialize(WebView1)
    Dim client As DefaultWebViewClient
    client.Initialize("client")
    WebViewExtras1.SetWebViewClient(client)
    
    Activity.AddView(WebView1, 0, 0, 100%x, Activity.Height)
  
    WebSettings.setDefaultTextEncodingName(WebView1,"utf-8")
    WebViewExtras1.addJavascriptInterface(WebView1, "B4A")
    WebView1.JavaScriptEnabled = True
    WebView1.ZoomEnabled=False
  
End Sub 
  
Sub AppClose_Request()
   ToastMessageShow("Exiting...",True)
   ExitApplication
 
End Sub


But when i press button i can't run Sub AppClose_Request() into B4A app!

May be this is limitation of WebViewExtras2
 

cenyu

Active Member
Licensed User
Longtime User
It's look like you can't help me... And not this is not trojan...
I trying to start B4A sub code from html page...
What trojan...?????
 
Upvote 0

JohnC

Expert
Licensed User
Longtime User
Try this change:
B4X:
<a href="javascript:void(0);" onclick="B4A.CallSubPlus('AppClose_Request', true,'1');" class="btn btn-secondary btn-lg btn-block">ExitApp</a>
 
Upvote 0

cenyu

Active Member
Licensed User
Longtime User
Still not working...
When i use WebViewExtra (v1.42) all works perfect but with WevViewExtras2 version 2.20 not work Thats why i thinking - there is WevViewExtras2 version 2.20 limitation.
As i say WevViewExtras2 version 2.20 i MUST use because into other part of my app i read https content wtch can not read with ... WebViewExtra (v1.42)
 
Upvote 0

cenyu

Active Member
Licensed User
Longtime User
When i put this code
B4X:
Sub WebView1_OverrideUrl (Url As String) As Boolean
  
    ToastMessageShow("Fired",True)
    Return True 'Don't try to navigate to this URL
  
  
  
End Sub


Nothing changes.... I found that i can't call javascript function wtiten into html like this


HTML:
<a href="javascript:doCallBack();"   class="btn btn-secondary btn-lg btn-block">Exit</a>

<script>
 function doCallBack(){
    alert(1)
     
    }
   </script>

May be i have error when enabling javascript on WebView and WebViewExtras?

B4X:
WebView1.Initialize("WebView1")
    WebView1.JavaScriptEnabled = True
    WebView1.ZoomEnabled=False
  
  
    Dim client As DefaultWebViewClient
    client.Initialize("client")
  
    WebViewExtras1.Initialize(WebView1)
    WebViewExtras1.JavaScriptEnabled=True
    WebViewExtras1.SetWebViewClient(client)
    WebViewExtras1.addJavascriptInterface(WebView1, "B4A")
    WebSettings.setDefaultTextEncodingName(WebView1,"utf-8")
    Activity.AddView(WebView1, 0, 0, 100%x, Activity.Height)
 
Upvote 0
Top