Android Question How to start an activity using a button in a WebView?

asales

Expert
Licensed User
Longtime User
I tried to use a click on a button in a webview to launch other activity but it's not working.

I followed this example of this post:
http://www.b4x.com/android/forum/threads/link-in-un-webview.31660/

This is my code:

B4X:
Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.

    Private wvTur As WebView
    Dim NameBar, AddressBar As String
    Dim MyWebViewExtras As WebViewExtras

End Sub

Sub Activity_Create(FirstTime As Boolean)

    NameBar = "Gondola Restaurant"
    AddressBar = "Philadelphia Street"
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("webpage1")

    wvTur.JavaScriptEnabled=True
    MyWebViewExtras.addJavascriptInterface(wvTur, "B4A")

    wvTur.LoadHtml("<html><body><h3>" & NameBar & "</h3><p>Address:<br/><strong>" & AddressBar & "</strong></p> " & "<p>Telephone:<br/><strong>5555-7777</strong></p>"& "<button onclick='B4A.CallSub('MyB4ASub', true)'"&">Show Activity</button>"&"</body></html>")
    wvTur.ZoomEnabled = False
End Sub

Sub MyB4ASub
    ' this Sub will be called by the onclick event of the image
    StartActivity(Activity2)
End Sub

My project is attached.

Thank you in advance to anyone who can help me.
 

Attachments

  • linkweb.zip
    9 KB · Views: 289
Last edited:

asales

Expert
Licensed User
Longtime User
Other question: if I put two buttons to start differents activities, how I can identifier which button was clicked?
 
Upvote 0

asales

Expert
Licensed User
Longtime User
Hi Erel. Thanks.
Can you give an example?

In the code
B4X:
<FORM action='openActivity'><INPUT type='submit' value='Show Activity'></FORM>

sent by NJDude, I received the string "file:///openActivity" in the URL parameter.

How to distinguish different buttons on it?
 
Upvote 0

NJDude

Expert
Licensed User
Longtime User
Create the buttons you need and change the "action" then, on the OverrideUrl event you check which button was clicked, for example:
B4X:
Sub rssView_OverrideUrl(Url As String) As Boolean

    URL = URL.Replace("file:///", "")

    If URL = "openActivity1" then ...
    If URL = "openActivity2" then ...
...

End Sub
 
Last edited:
Upvote 0
Top