Android Example Catch Webview Item's Longclick without late Javascript injection

Discussion in 'Tutorials & Examples' started by fredo, Apr 17, 2019.

  1. fredo

    fredo Well-Known Member Licensed User

    Sample project to intercept clicks and longclicks on images or text portions in a webview.

    The test project was created, because it became necessary to find a suitable solution from the very much information around Webview.

    One differentiates between html sources according to whether one self-generates the content or whether the html comes from an external source.

    To handle HTML from an external source, you have to inject a certain javascript after loading the page, which will be shown later in another example project.

    In this example the HTML source is created by yourself, so that items to be identified later can be marked in advance.
    This project is based on the AJWebkit from warwound, because it was the easiest way to solve the longclick problem.

    2019-04-17_18-33-57.jpg 2019-04-17_18-34-14.jpg 2019-04-17_18-34-07.jpg

    Sub Globals
    Private FlingableWebViewAj1 As FlingableWebView ' warwound -->
                                                        ' warwound -->
    Dim WebViewClient1 As DefaultWebViewClient
    End Sub

    Sub Activity_Create(FirstTime As Boolean)
    ' warwound infos on the differences of webviews -->
    ' ---- ---- ---- ---- ---- ---- ---- ---- ----
        ' FlingableWebView
        ' ---- ---- ---- ---- ---- ---- ---- ---- ----
        FlingableWebViewAj1.ZoomEnabled = 
        FlingableWebViewAj1.JavaScriptEnabled = 
    Activity.AddView(FlingableWebViewAj1, 00100%x100%y)

    Dim JavascriptInterface1 As DefaultJavascriptInterface
    ' Enable a _PageFinished event
    End Sub

    Sub Handle_DWVC1_PageFinished (FlingableWebView1 As FlingableWebView, Url As String)
    Log("#-Sub Handle_DWVC1_PageFinished, Url=" & Url)
    End Sub
    Sub Handle_DWVC1_OverrideUrl(FlingableWebView As FlingableWebView, url As StringAs Boolean
    Log("#-Sub Handle_DWVC1_OverrideUrl, Url=" & url)
    Return True
    End Sub
    Sub FlingableWebViewAj1_SingleTap(X As Float, Y As Float) As Boolean
    Log("#-Sub FlingableWebViewAj1_SingleTap")
    Dim AjHitResult As HitTestResult = FlingableWebViewAj1.GetHitTestResult
    Log("#-  x80, AjHitResult.GetType=" & AjHitResult.GetType & ", AjHitResult.GetExtra=" & AjHitResult.GetExtra)
    If AjHitResult.GetExtra = Null Then Return True
    Select Case True
    Case AjHitResult.GetExtra.StartsWith("myspecialtag_image"), AjHitResult.GetExtra.StartsWith("file://")
    "ShowImageView", AjHitResult.GetExtra.Replace("myspecialtag_image:""").Replace("file://"""))

    Case AjHitResult.GetExtra.StartsWith("myspecialtag_c2textid")
    "Message""TEXT clicked-->" & CRLF & CRLF & AjHitResult.GetExtra.Replace("myspecialtag_c2textid:"""))
    End Select
    Return False ' !!!
    End Sub

    Sub FlingableWebViewAj1_LongPress(X As Float, Y As Float)
    Log("#-Sub FlingableWebViewAj1_LongPress")
    Dim AjHitResult As HitTestResult = FlingableWebViewAj1.GetHitTestResult
    Log("#-  x100, AjHitResult.GetType=" & AjHitResult.GetType & ", AjHitResult.GetExtra=" & AjHitResult.GetExtra)

    "AjHitResult.GetType = " & AjHitResult.GetType & CRLF & CRLF & "AjHitResult.GetExtra = " & AjHitResult.GetExtra, "Test of FlingableWv LongPress")
    End Sub
    The solution is based solely on the work of warwound and his allies.

    This Testproject is only a relatively clear summary of the abundantly scattered information for a specific use case.

    Attached Files:

  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