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

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

2019-04-17_19-03-17.jpg

B4X:
Sub Globals
    Private FlingableWebViewAj1 As FlingableWebView ' warwound --> https://www.b4x.com/android/forum/threads/webviewextras.12453/page-7#post-235017
                                                    ' warwound --> https://www.b4x.com/android/forum/threads/webviewextras.12453/page-8#post-246624
                                                    
    Dim WebViewClient1 As DefaultWebViewClient
                                                        
End Sub

Sub Activity_Create(FirstTime As Boolean)
    
    ' warwound infos on the differences of webviews --> https://www.b4x.com/android/forum/threads/webviewextras.12453/page-6#post-231146
    
    ' ---- ---- ---- ---- ---- ---- ---- ---- ----
    ' FlingableWebView
    ' ---- ---- ---- ---- ---- ---- ---- ---- ----
    FlingableWebViewAj1.Initialize("FlingableWebViewAj1")
    FlingableWebViewAj1.ZoomEnabled = False
    FlingableWebViewAj1.JavaScriptEnabled = True
    Activity.AddView(FlingableWebViewAj1, 0, 0, 100%x, 100%y)

    Dim JavascriptInterface1 As DefaultJavascriptInterface
    JavascriptInterface1.Initialize(FlingableWebViewAj1)
    FlingableWebViewAj1.AddJavascriptInterface(JavascriptInterface1, "B4A")     
    
    ' Enable a _PageFinished event
    WebViewClient1.Initialize("Handle_DWVC1")
    FlingableWebViewAj1.SetWebViewClient(WebViewClient1)
    ...
    
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 String) As Boolean
    Log("#-Sub Handle_DWVC1_OverrideUrl, Url=" & url)
    Return True
End Sub
   
   
Sub FlingableWebViewAj1_SingleTap(X As Float, Y As Float) As Boolean
    Log("#-")
    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://")
            CallSubDelayed2(Me, "ShowImageView", AjHitResult.GetExtra.Replace("myspecialtag_image:", "").Replace("file://", ""))

        Case AjHitResult.GetExtra.StartsWith("myspecialtag_c2textid")
            CallSubDelayed2(Me, "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("#-")
    Log("#-Sub FlingableWebViewAj1_LongPress")
    Dim AjHitResult As HitTestResult = FlingableWebViewAj1.GetHitTestResult
    Log("#-  x100, AjHitResult.GetType=" & AjHitResult.GetType & ", AjHitResult.GetExtra=" & AjHitResult.GetExtra)

    MsgboxAsync("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.
 

Attachments

  • WebViewLongClick_03.zip
    242.2 KB · Views: 475
Top