Android Question WebviewExtras Or JSInterface Not Responded

Ganiadi

Active Member
Licensed User
Longtime User
Hi all,
I need some help regarding to the interface link between Java script in HTML and B4a, where actually i need when i press button in HTML it may directed to B4a for handling.

Here the HML with java script that i created with Onclick Button Handling ( Expected when i CLick it, it will raise Test Function In B4a, Where i Included The sample that i modified from GoogleMapsWebView Example ) but it doesnt Work.
Pls advice..

<script type="text/javascript">
function myFunction() {B4A.CallSub('Test'); })}
</script>
<html>
<head>
<title></title>
<meta name="" content="">
</head>
<body>
<button id="demo" onclick="myFunction()">Click Me To change my text color.</button>
</body>
</html>
 

Attachments

  • TestiFace.zip
    5.8 KB · Views: 235

Ganiadi

Active Member
Licensed User
Longtime User
I don't see this script in the project you've uploaded.
I mean that actually the script is from outside html / url address that simple contain that handle.
It execute the html from the url but when i click the button it doesnt responded
Tks
 
Upvote 0

Ganiadi

Active Member
Licensed User
Longtime User
I don't see this script in the project you've uploaded.
Below is the complete Source :

Sub Process_Globals
Dim cUrl As String
End Sub

Sub Globals
Dim Webpage As WebView
Dim WebViewerExtra As WebViewExtras
End Sub

Sub Activity_Create(FirstTime As Boolean)

Webpage.Initialize("MyWeb")
Activity.AddView(Webpage, 0, 0, 100%x, 100%y)
WebViewerExtra.addJavascriptInterface(Webpage, "B4A")

Test

End Sub
Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub Test
Dim myHtml As StringBuilder
Dim myHtmlString As String

myHtml.Initialize
myHtml.Append ("<html>")

myHtml.Append ("<script language='JavaScript'>")
myHtml.Append("function myfunction(){")
myHtml.Append(" B4A.CallSub('b4aGetID');")
myHtml.Append ("}</script>")

myHtml.Append("<body>")
myHtml.Append ("<p id=""demo"" onclick=""myfunction()"">Click Me To change my text color.</p>")
myHtml.Append ("</body></html>")
myHtmlString=myHtml.ToString
Webpage.LoadHtml(myHtmlString)


End Sub


Sub b4aGetID()
Msgbox("press","test")
End Sub

Sub myWeb_OverrideUrl (Url As String) As Boolean
ToastMessageShow(Url,False)
End Sub

Sub myWeb_PageFinished (Url As String)
ToastMessageShow(Url,False)
End Subhing it

It doesnt fire Test when i click the button demo, the only thing it only fire MyWeb_OverrideUrl for the first time it launch
But This is not that i want.

Tks for help...
 
Upvote 0

Ganiadi

Active Member
Licensed User
Longtime User
Tku Erel For your reference

But i tried to add the parameter like below but still not working :
I changed based on the reference by adding callUIThread
CallSub(subName As String, callUIThread As boolean)


B4X:
myHtml.Append ("<script language='JavaScript'>")
myHtml.Append("function myfunction(){")
myHtml.Append(" B4A.CallSub('b4aGetID', True);")
myHtml.Append ("}</script>")

[Code/]

Is there any wrong, Pls Advice

Tks
 
Upvote 0

Ganiadi

Active Member
Licensed User
Longtime User
1. You should learn how to use the smart strings literal. It will make your code simpler: https://www.b4x.com/android/forum/threads/50135/#content
2. See the code posted in that thread. It should be true, not True.

Tks Erel for correction, but i tried to change it and still no action when i pressed the button is not fire the b4aGetID
Below is the code i've changed

B4X:
#Region Module Attributes
    #FullScreen: False
    #IncludeTitle: True
    #ApplicationLabel: webviewextra
    #VersionCode: 1
    #VersionName:
    #SupportedOrientations: portrait
    #CanInstallToExternalStorage: False
#End Region

'Activity module
Sub Process_Globals
    Dim cUrl As String
End Sub

Sub Globals
    Dim Webpage As WebView
    Dim WebViewerExtra As WebViewExtras
  
    Private Button1 As Button
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("1")
    Webpage.Initialize("MyWeb")
    Activity.AddView(Webpage, 0, 0, 100%x, 100%y)
    WebViewerExtra.addJavascriptInterface(Webpage, "B4A")

    Test
  
End Sub
Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub Test
Dim myHtml As StringBuilder
Dim myHtmlString As String

    myHtml.Initialize
    myHtml.Append ("<html>")

    myHtml.Append ("<script language='JavaScript'>")
    myHtml.Append("function myfunction(){")
    myHtml.Append("    B4A.CallSub('b4aGetID',true);")
    myHtml.Append ("}</script>")

    myHtml.Append("<body>")
    myHtml.Append ("<p id=""demo"" onclick=""myfunction()"">Click Me To change my text color.</p>")
    myHtml.Append ("</body></html>")
    myHtmlString=myHtml.ToString
    Webpage.LoadHtml(myHtmlString)
    WebViewerExtra.executeJavascript(Webpage,"myfunction")

  
End Sub


Sub b4aGetID()
    Msgbox("press","test")
End Sub

Sub myWeb_OverrideUrl (Url As String) As Boolean
   ToastMessageShow(Url,False)
End Sub

Sub myWeb_PageFinished (Url As String)
   ToastMessageShow(Url,False)
End Sub

[code/]

Please help ...

Tks
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
1. You dont need Stringbuilder. The Smart literal is enough
2. Call the javascript AFTER the page is loaded if you want to programatically start the function in javascript.

B4X:
Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Dim MyWebViewExtras As WebViewExtras
    Dim WebView1 As WebView
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("layoutMain")
    WebView1.Height=100%y
    WebView1.Width=100%x
    '    add the JavascriptInterface to WebView1
    MyWebViewExtras.addJavascriptInterface(WebView1, "B4A")
    MyWebViewExtras.addWebChromeClient(WebView1,"Chrome")

  WebView1.LoadHtml($"<html>
<script language="JavaScript">
function myfunction(){
  B4A.CallSub('b4aGetID',true);
}
</script>
<body>
<p id="demo" onclick="myfunction()">Click Me To change my text color.</p>
</body></html>"$)

End Sub
Sub www_OverrideUrl (Url As String) As Boolean  
    Log($"www_OverrideURL(${Url})"$)
    Return True
End Sub
Sub www_PageFinished (Url As String)
    Log($"www_PageFinished(${Url})"$)
  If Url = "file:///" Then
        MyWebViewExtras.executeJavascript(WebView1,"myfunction();") ' Call the javasript after the page is loaded!
    End If
End Sub
Sub b4aGetID()
  Log("b4aGetID()")
End Sub
 
Last edited:
Upvote 0

Ganiadi

Active Member
Licensed User
Longtime User
1. You dont need Stringbuilder. The Smart literal is enough
2. Call the javascript AFTER the page is loaded if you want to programatically start the function in javascript.

B4X:
Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Dim MyWebViewExtras As WebViewExtras
    Dim WebView1 As WebView
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("layoutMain")
    WebView1.Height=100%y
    WebView1.Width=100%x
    '    add the JavascriptInterface to WebView1
    MyWebViewExtras.addJavascriptInterface(WebView1, "B4A")
    MyWebViewExtras.addWebChromeClient(WebView1,"Chrome")

  WebView1.LoadHtml($"<html>
<script language="JavaScript">
function myfunction(){
  B4A.CallSub('b4aGetID',true);
}
</script>
<body>
<p id="demo" onclick="myfunction()">Click Me To change my text color.</p>
</body></html>"$)

End Sub
Sub www_OverrideUrl (Url As String) As Boolean 
    Log($"www_OverrideURL(${Url})"$)
    Return True
End Sub
Sub www_PageFinished (Url As String)
    Log($"www_PageFinished(${Url})"$)
  If Url = "file:///" Then
        MyWebViewExtras.executeJavascript(WebView1,"myfunction();") ' Call the javasript after the page is loaded!
    End If
End Sub
Sub b4aGetID()
  Log("b4aGetID()")
End Sub

Hi Don,

Tks for your help and suggestion, i've changed the source as suggested but it doesnt fire the ba4GetID when i "click Me To Change my text color"

???
Tks
 
Upvote 0

Ganiadi

Active Member
Licensed User
Longtime User
I dont know what but you did something wrong i guess. It is working here
Hi Don,

Tks so much with the file, but it really weird since i still can not get any logs or msgbox when i click click "Me To Change my text color"

i dont have any idea...
???

Tks & Best Regards
 
Upvote 0

Ganiadi

Active Member
Licensed User
Longtime User
It always shows the log-entry for me... Did you get the log-entries?
It also work for me if a put a msgbox call to the sub b4aGetID
What version of webviewextras are you using?
Im using ver 1.42, or do you have another version ?
Pls update me...

Tks Don
 
Upvote 0
Top