Android Question WebView ScaleToFit for b4a

dieterp

Active Member
Licensed User
Longtime User
I am adding an image to a WebView in B4a but the image doesn't scale down to the size of the WebView. How do I accomplish this? I know in b4i the 'ScaleToFit' function handles this with the WebView
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Try this:
B4X:
Sub LoadImage(img As String)
   WebView1.LoadHtml($"
   <html>
   <head><style>img{display: inline; height: auto; max-width: 100%;}</style></head><img src="${WebViewAssetFile(img)}">
   </body>
   </html>"$)
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

Based on this answer: http://stackoverflow.com/questions/10395292/android-webview-scaling-image-to-fit-the-screen
 
Upvote 0

dieterp

Active Member
Licensed User
Longtime User
Thanks for the reply Erel. I'm struggling a bit to get this to work but I have found another way to approach the problem.
 
Upvote 0
Top