Android Code Snippet Calling subs with webview links and buttons

skrjabin

Active Member
Licensed User
I like to use the webview as a very individual user interface. You can load the content offline with WebView1.LoadHtml or do online real time changes on the interface with WebView1.LoadUrl. You can use all the HTML, CSS or even JavaScript to draw a nice view and with this code you can interact, if the user presses a link or a button.

B4X:
Sub WebView1_OverrideUrl (Url As String) As Boolean
    Return CallSubByLink(Url) 'If True then cancel the regular Weblink, do the internal Sub instead
End Sub

Sub CallSubByLink(url As String) As Boolean
    Dim sValue As String = GetLinkParameter(url, "B4Xaction=")
    Select sValue 'define your intern Subs here, it is like a Button1_Click in the view
        Case "sample_1"
            Sample_1
        Case "sample_2"
            Sample_1
    End Select
    Return (sValue <> "") 'Returns True, if parameter "B4Xaction" exists, means that it should call a Sub in the App and is not a regular weblink
End Sub

Sub GetLinkParameter(url As String, parameter As String) As String
    Dim sValue As String
    Dim iPos As Long = url.IndexOf(parameter)
    If iPos >= 0 Then
        sValue  = url.SubString(iPos + parameter.Length) 'ToDo: If you have more Link-Parameter, you have to define the Value-end by searching for '&'
    End If
    Return sValue
End Sub

Sub Sample_1
    Msgbox("This is Sub Sample_1", "Webview-Button example")
End Sub

Sub Sample_2
    Msgbox("This is Sub Sample_2", "Webview-Button example")
End Sub
That way you can easyly create and quickly change a layout in runtime.
 
Top