Android Question Link in WebView calls B4A subroutine?

Discussion in 'Android Questions' started by canalrun, Jul 26, 2019.

  1. canalrun

    canalrun Well-Known Member Licensed User

    in B4A I currently dynamically create a small, simple webpage (as a string, s1) and load it into a WebView using: wv.LoadHtml(s1).

    Is it possible, as I'm creating the webpage, to create a link within the webpage that when clicked will cause the B4A program to call a subroutine?

    (The B4A subroutine I'm thinking about will restore marker points from a file and display them on a map)

    Nothing too complicated.

  2. DonManfred

    DonManfred Expert Licensed User

    Search for webviewextra and javascriptinterface
  3. canalrun

    canalrun Well-Known Member Licensed User

  4. DonManfred

    DonManfred Expert Licensed User

  5. HAH

    HAH Active Member Licensed User

    or simply use webview override event and consume it.
  6. canalrun

    canalrun Well-Known Member Licensed User

    Thanks. That works very well.

    In my html file I embedd a link:

    <a href="reload:xxxx">Reload</a>
    When I click on the link the following code runs (my WebView is named wvSession):

    Sub wvSession_OverrideUrl (Url As StringAs Boolean
    If (Url.IndexOf("reload:") > -1Then
    Dim id As String = Url.SubString2(7, Url.Length)
    Log("id: " & id)
    End If
    End Sub

    Sub ReloadSession(id As String)
    Dim slst As List
      slst = 
    File.ReadList(File.DirDefaultExternal, "Sessions.txt")
    For Each s1 As String In slst
    If ((s1.Contains("<!--")) And (s1.Contains(id))) Then
    Log("found id: " & s1)
    End If
    End Sub
    I am looking for an html comment line that contains the "id" in the file Sessions.txt.

  7. HAH

    HAH Active Member Licensed User

    Your code appears to be fine, is it working? is Log("found id: " & s1) executed?
  8. canalrun

    canalrun Well-Known Member Licensed User

    Yes, works very well.

    I dynamically generate a "session" and append it to a file called Sessions.txt. Each "session" is an exercise session. The session block contains parameters such as ID, duration, laps, and time, plus I repeat the parameters in an HTML comment comma separated string that can later be restored to the screen display to remind me of that exercise session. I restore the session by clicking the dynamically generated link (containing the session ID) that is saved as part of the session block.

    Thanks for the idea.
    Last edited: Jul 28, 2019
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice