Android Question Problem with link in Webview [Solved]

Roger Daley

Well-Known Member
Licensed User
Longtime User
Hi All

A difficult question to phrase.

I have an App [Configulator] that in in the Help file has a link to a web site. That web site has links to downloadable text files [Orangedox/Dropbox site]

If I go to the web site directly from the phones browser [Firefox], link to Orangedox the relevant file downloads successfully.
If I go to the web site using the link within the Webview I can link to Orangedox but the download does not work.

I suspect that originating in the Webview means that Orangedox doesn't have a download destination. [I am guessing.]

B4X:
Sub HelpSub_Click
    BuzzChirp
    StartActivity(HelpActiv)
End Sub

'HelpActiv

#Region  Activity Attributes
    #FullScreen: true
    #IncludeTitle: false
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
   
   

End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
   
    Private helpview As WebView

End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")
   
    Activity.LoadLayout("helplay")
    helpview.LoadURL("file:///android_asset/helpfile.htm")

End Sub

The simple solution is to not have the web site address as a link but it is preferable.

Any enlightening ideas welcome.

Regards Roger
 

eurojam

Well-Known Member
Licensed User
Longtime User
Hi Roger,
I usually use:
B4X:
mywebview.Loadhtml(File.ReadString(File.DirAssets, "help.htm") )
which you can use after downloading the file with httputils2
 
Upvote 0

Roger Daley

Well-Known Member
Licensed User
Longtime User
Obviously missing something.

I've had several attempts at using the code but the Help File only opens as a blank screen. 3 steps backward.

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 helpview As WebView

End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")
   
    Activity.LoadLayout("helplay")
    'helpview.LoadURL("file:///android_asset/helpfile.htm")
   
    'helpview.LoadHtml("<html><body background='" & WebViewAssetFile("helpfile.htm") & "'></body></html>")
    'helpview.LoadHtml("<html><body ='" & WebViewAssetFile("helpfile.htm"))
    helpview.LoadHtml(WebViewAssetFile("helpfile.htm"))
   
End Sub

Sub WebViewAssetFile (FileName As String) As String
    Dim jo As JavaObject
    jo.InitializeStatic("anywheresoftware.b4a.objects.streams.File")
    If jo.GetField("virtualAssetsFolder") = Null Then
        Return "file:///android_asset/" & FileName.ToLowerCase
    Else
        Return "file://" & File.Combine(jo.GetField("virtualAssetsFolder"), _
       jo.RunMethod("getUnpackedVirtualAssetFile", Array As Object(FileName)))
    End If
End Sub


Regards Roger
 
Upvote 0

Roger Daley

Well-Known Member
Licensed User
Longtime User
Hi Roger,
I usually use:
B4X:
mywebview.Loadhtml(File.ReadString(File.DirAssets, "help.htm") )
which you can use after downloading the file with httputils2

Many thanks eurojam, unfortunately this loads but is corrupted.

helpview.LoadURL("file:///android_asset/helpfile.htm") may be incorrect but has been working since 2014. Thanks MaFu.
https://www.b4x.com/android/forum/threads/how-to-add-a-help-file.43287/#post-263282

None of this seems to explain the inability to download from Orangedox when using a link in a Webview.


Regards Roger
 
Upvote 0

Roger Daley

Well-Known Member
Licensed User
Longtime User
You should use LoadURL with this. Another simpler option was posted by @eurojam.

This works as far as displaying the Help file but the original problem of not downloading from Orangedox remains.

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 helpview As WebView

End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")
   
    Activity.LoadLayout("helplay")
    'helpview.LoadURL("file:///android_asset/helpfile.htm")
   
    helpview.Loadurl(WebViewAssetFile("helpfile.htm"))

End Sub

Sub WebViewAssetFile (FileName As String) As String
    Dim jo As JavaObject
    jo.InitializeStatic("anywheresoftware.b4a.objects.streams.File")
    If jo.GetField("virtualAssetsFolder") = Null Then
        Return "file:///android_asset/" & FileName.ToLowerCase
    Else
        Return "file://" & File.Combine(jo.GetField("virtualAssetsFolder"), _
       jo.RunMethod("getUnpackedVirtualAssetFile", Array As Object(FileName)))
    End If
End Sub

Regards Roger
 
Upvote 0

Roger Daley

Well-Known Member
Licensed User
Longtime User
Note that you shouldn't use android_asset directly. You should instead use this code: https://www.b4x.com/android/forum/threads/images-loaded-in-webviews-are-not-displayed-in-mode-“debug-rapid-”-b4a-3-50-version.38981/#post-230988

You will need to handle the OverrideUrl event and download the file yourself with HttpUtils2.

I have been searching for examples/tutorials on OverrideUrl as this appears to be what I need, but can't find anything relevant. I what I need to do is capture the Url when the link in the Webview is pressed and then raise the browser.

Regards Roger
 
Upvote 0

eurojam

Well-Known Member
Licensed User
Longtime User
here we go:
B4X:
Sub wvImpressum_OverrideUrl (Url As String) As Boolean
    Dim p As PhoneIntents
    Dim message As Email
    wvImpressum.StopLoading
  
    If Url.StartsWith("mailto:") Then
      message.To.Add(Url.SubString(7))
      StartActivity(message.GetIntent)
    Else  
      StartActivity(p.OpenBrowser(Url))
    End If

End Sub

where wvImpressum is the event name from the webview....
 
Upvote 0

Roger Daley

Well-Known Member
Licensed User
Longtime User
here we go:
B4X:
Sub wvImpressum_OverrideUrl (Url As String) As Boolean
    Dim p As PhoneIntents
    Dim message As Email
    wvImpressum.StopLoading
 
    If Url.StartsWith("mailto:") Then
      message.To.Add(Url.SubString(7))
      StartActivity(message.GetIntent)
    Else 
      StartActivity(p.OpenBrowser(Url))
    End If

End Sub

where wvImpressum is the event name from the webview....
Thanks eurojam,
Super fast response. I will see how it goes. Tomorrow, when my eyes and brain are less fuzzy.

Regards Roger
 
Upvote 0

Roger Daley

Well-Known Member
Licensed User
Longtime User
here we go:
B4X:
Sub wvImpressum_OverrideUrl (Url As String) As Boolean
    Dim p As PhoneIntents
    Dim message As Email
    wvImpressum.StopLoading
 
    If Url.StartsWith("mailto:") Then
      message.To.Add(Url.SubString(7))
      StartActivity(message.GetIntent)
    Else 
      StartActivity(p.OpenBrowser(Url))
    End If

End Sub

where wvImpressum is the event name from the webview....

Hi
Well it's a new day but I can't see how to implement this Sub. The "event name" in the Webview is a URL.
http://softwarehorsetrailer.simplesite.com/ It doesn't seem to fit substituting "wvImpressum" with the URL

What am I missing?

Regards Roger
 
Upvote 0

Roger Daley

Well-Known Member
Licensed User
Longtime User
Hi All

Back from lunch brain finally engaged. Epiphany.

1. The "event name" is the name of the Webview that contains the link.
2. The Webview is in the Help Activity. [Not the Main] :oops:

Thanks to Erel and eurojam.

Regards Roger
 
Upvote 0
Top