Hi,
we decide to use more functionality from webview, because the main components are already working in html. So, i try to interact between webview and b4x.
I try the example which pulls a webview function from the activity. that works. But more important is the other way, pushing from the webview to the activity.
I found a article here that says the easiest way for b4a and b4i is the "OverrideUrl" from webview. I´ve tried it - it does nothing :/
Can anyone tell me what my mistake is?
B4X:
#Region Project Attributes
#ApplicationLabel: B4A Example
#VersionCode: 1
#VersionName:
'SupportedOrientations possible values: unspecified, landscape or portrait.
#SupportedOrientations: unspecified
#CanInstallToExternalStorage: False
#End Region
#Region Activity Attributes
#FullScreen: False
#IncludeTitle: True
#End Region
Sub Process_Globals
End Sub
Sub Globals
Private WebView1 As WebView
Dim we As WebViewExtras
End Sub
Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout("1")
we.addJavascriptInterface(WebView1,"B4A")
WebView1.LoadUrl(WebViewAssetFile("index.html"))
End Sub
Sub TheContentIs(content As String)
Msgbox(content,"Content From JS")
End Sub
Sub WebViewAssetFile (FileName As String) As String
#if B4J
Return File.GetUri(File.DirAssets, FileName)
#Else If B4A
Dim jo As JavaObject
jo.InitializeStatic("anywheresoftware.b4a.objects.streams.File")
If jo.GetField("virtualAssetsFolder") = Null Then
Return "file:///android_asset/" & FileName.ToLowerCase
Else
Return "file://" & File.Combine(jo.GetField("virtualAssetsFolder"), _
jo.RunMethod("getUnpackedVirtualAssetFile", Array As Object(FileName)))
End If
#Else If B4i
Return $"file://${File.Combine(File.DirAssets, FileName)}"$
#End If
End Sub
Sub Activity_Resume
End Sub
Sub Activity_Pause (UserClosed As Boolean)
End Sub
Sub Button1_Click
we.executeJavascript(WebView1,"javascript: doCallBack();")
End Sub
Sub Webview1_OverrideUrl (Url As String)
Msgbox(Url,"Called")
If Url.Contains ("#") Then
Msgbox(Url,"Contains Hash")
End If
End Sub
In the html is a button
HTML:
<button onclick="changeHash('#nice',this)">Push me</button>
</body>
<script>
function changeHash(h,t){
window.location.href=h
t.innerHTML=h;
}
</script>
Webview1_OverrideUrl will not called...
Regards
Matze
If I understood you correctly, when you click on 'Button1' should the javascript be executed and the response sent by the javascript interface be captured?
Ok, I will try to help you...
First, download library from this link:
https://www.b4x.com/android/forum/threads/ultimatewebview-custom-view.135666/#post-860033
Merge WebView with UltimateWebView as in the code below.
[CODE lang="b4x" title="Example:"]Sub Activity_Create(FirstTime As Boolean)
'Do not forget to load the layout file created with the visual designer. For example:
Activity.LoadLayout("Main") 'Use your activity name
UltimateWebView1.Initialize(Me, "UltimateWebView1") 'Initializes UltimateWebView
UltimateWebView1.WebView=WebView1 'Merge your WebView with UltimateWebView
UltimateWebView1.SetWebViewClient
UltimateWebView1.SetWebChromeClient
UltimateWebView1.Settings.JavaScriptEnabled=True 'Very important for JavaScripts
UltimateWebView1.Settings.AllowContentAccess=True
UltimateWebView1.Settings.AllowFileAccess=True
UltimateWebView1.Settings.AppCacheEnabled=True
UltimateWebView1.Settings.CacheMode=UltimateWebView1.Settings.Constants.CACHEMODE_LOAD_DEFAULT
UltimateWebView1.Settings.JavaScriptCanOpenWindowsAutomatically=True
UltimateWebView1.Settings.DisplayZoomControls=False
UltimateWebView1.Settings.DomStorageEnabled=True
UltimateWebView1.Settings.MediaPlaybackRequiresUserGesture=False
UltimateWebView1.Settings.AllowFileAccessFromFileURLs=True
UltimateWebView1.Settings.AllowUniversalAccessFromFileURLs=True
UltimateWebView1.Settings.GeolocationEnabled=True
UltimateWebView1.SetDownloadListener
UltimateWebView1.CookieManager.AcceptCookies=True
UltimateWebView1.CookieManager.AcceptThirdPartyCookies=True
UltimateWebView1.CookieManager.AcceptFileSchemeCookies=True
UltimateWebView1.CookieManager.Flush
UltimateWebView1.LoadUrl(WebViewAssetFile("index.html"))
End Sub[/CODE]
Second use this code in your Button1_Click Sub (For synchronized result):
[CODE lang="b4x" title="Example:"]Sub Button1_Click
UltimateWebView1.ExecuteJavaScript2([Your JavaScript Code])
Wait For UltimateWebView1_ExecuteJavaScriptResult (Result As String)
If Result<>"" Then
If Result.Contains ("#") Then
Log("Result Contains Hash")
End If
End If
End Sub[/CODE]
For Asynchronized result:
[CODE lang="b4x" title="Example:"]Sub Button1_Click
UltimateWebView1.ExecuteJavaScript2([Your JavaScript Code])
End Sub
'Result Event:
Sub UltimateWebView1_ExecuteJavaScriptResult (Result As String)
If Result<>"" Then
If Result.Contains ("#") Then
Log("Result Contains Hash")
End If
End If
End Sub[/CODE]