B4A Library UltimateWebView Custom View

IMPORTANT!!!
After one year of its existence, UltimateWebView migrated to WebkitLibrariesSet. The UltimateWebView library will no longer be upgraded in its current form. This thread will be neglected and there will be no answers to the questions asked in this thread. I recommend that you switch your projects in which you use UltimateWebView to WebkitLibrariesSet as soon as possible. To download the set of libraries and more information visit the following thread:



The purpose of the library is to implement all important classes in one library in order to make work as easy as possible. The library will be upgraded over time by adding new features and protocols.

For full library functionality, copy the following into the manifest:
Manifest:
'Important
SetApplicationAttribute(android:usesCleartextTraffic,"true")
AddPermission(android.permission.DOWNLOAD_WITHOUT_NOTIFICATION)
'---------------------
'Camera Permissions
AddPermission(android.permission.CAMERA)
AddPermission(android.permission.RECORD_AUDIO)
AddPermission(android.permission.MODIFY_AUDIO_SETTINGS)
AddPermission(android.permission.MICROPHONE)
AddPermission("android.hardware.camera")
AddManifestText(<uses-feature android:name="android.hardware.camera" android:required="true" />)
AddManifestText(<uses-feature android:name="android.hardware.camera.autofocus" android:required="false" />)
AddManifestText(<uses-feature android:name="android.hardware.camera.flash" android:required="false" />)
'---------------------

'Geolocation Permissions
AddPermission(android.permission.ACCESS_FINE_LOCATION)
AddPermission(android.permission.ACCESS_COARSE_LOCATION)
AddPermission(android.permission.ACCESS_BACKGROUND_LOCATION)
AddPermission(android.permission.WRITE_EXTERNAL_STORAGE)
'------------------------

AddManifestText(<uses-permission
   android:name="android.permission.WRITE_EXTERNAL_STORAGE"
   android:maxSdkVersion="18" />
)

AddApplicationText(
  <provider
  android:name="android.support.v4.content.FileProvider"
  android:authorities="$PACKAGE$.provider"
  android:exported="false"
  android:grantUriPermissions="true">
  <meta-data
  android:name="android.support.FILE_PROVIDER_PATHS"
  android:resource="@xml/provider_paths"/>
  </provider>
)
CreateResource(xml, provider_paths,
   <files-path name="name" path="shared" />
)

UltimateWebView is a Custom View Library and it is possible to add a View through the Designer.

Sample project:
B4AExample:
#Region  Project Attributes
    #ApplicationLabel: B4A Example
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

#BridgeLogger: True

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 txtUrl As EditText
    Private btnGo As Button
    Private UltimateWebView1 As UltimateWebView
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("Main")
 
    UltimateWebView1.SetWebViewClient 'Sets WebViewClient and its Events.
    UltimateWebView1.SetWebChromeClient 'Sets WebChromeClient and its Events.
    'Other UltimateWebViewSettings
    UltimateWebView1.Settings.JavaScriptEnabled=True
    UltimateWebView1.Settings.AllowContentAccess=True
    UltimateWebView1.Settings.AllowFileAccess=True
    UltimateWebView1.Settings.AppCacheEnabled=True
    UltimateWebView1.Settings.CacheMode=UltimateWebView1.Settings.CacheMode_LOAD_DEFAULT
    UltimateWebView1.Settings.JavaScriptCanOpenWindowsAutomatically=True
    UltimateWebView1.Settings.DisplayZoomControls=False
    UltimateWebView1.Settings.DomStorageEnabled=True
    UltimateWebView1.Settings.MediaPlaybackRequiresUserGesture=False
    UltimateWebView1.Settings.AllowFileAccessFromFileURLs=True
    UltimateWebView1.Settings.AllowUniversalAccessFromFileURLs=True
    UltimateWebView1.Settings.GeolocationEnabled=True
    UltimateWebView1.SetDownloadListener 'Sets and start DownloadListener'
 
    'CookieManager Settings to accept all cookies
    UltimateWebView1.CookieManager.AcceptCookies=True
    UltimateWebView1.CookieManager.AcceptThirdPartyCookies=True
    UltimateWebView1.CookieManager.AcceptFileSchemeCookies=True
    UltimateWebView1.CookieManager.Flush
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)
    If UserClosed Then
        ExitApplication
    End If
End Sub

Sub Activity_KeyPress (KeyCode As Int) As Boolean 'Return True to consume the event
    If KeyCode=KeyCodes.KEYCODE_BACK Then
        If UltimateWebView1.CanGoBack=True Then
            UltimateWebView1.GoBack
            Return True
        Else
            Return False
        End If
    Else
        Return False
    End If
End Sub

Sub btnGo_Click
    'You can use LoadUrl2 like in code bellow
    '--------------------------
    'Dim Headerrs As Map
    'Headerrs.Initialize
    'Headerrs.Put("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.183 Safari/537.36")
    'UltimateWebView1.LoadUrl2(txtUrl.Text,Headerrs)
    '---------------------------
 
    UltimateWebView1.LoadUrl(txtUrl.Text)
End Sub

Sub UltimateWebView1_FileDownloadInitialized (DownloadProperties1 As DownloadProperties) 'When click to download link or button this event will bi fired.
    Log("Download INITIALIZED")
    'DownloadProperties fields:
    '---------------------------------
    'Log(DownloadProperties1.url)
    'Log(DownloadProperties1.userAgent)
    'Log(DownloadProperties1.contentDisposition)
    'Log(DownloadProperties1.mimeType)
    'Log(DownloadProperties1.contentLength)
    'Log(DownloadProperties1.cookies)
    'Log(DownloadProperties1.fileName)
    'Log(DownloadProperties1.FileExtension)
    'Log(DownloadProperties1.DownloadID)
    '---------------------------------
 
    UltimateWebView1.StartFileDownload(DownloadProperties1,"TEST",True,True) 'File will be downloaded with native DownloadManager. If you want internall download, you can use your own method with parameters from DownloadProperties.
End Sub

Sub UltimateWebView1_FileDownloadStarted (DownloadProperties1 As DownloadProperties)
    Log("Download STARTED")
    'Log(DownloadProperties1.url)
    'Log(DownloadProperties1.userAgent)
    'Log(DownloadProperties1.contentDisposition)
    'Log(DownloadProperties1.mimeType)
    'Log(DownloadProperties1.contentLength)
    'Log(DownloadProperties1.cookies)
    'Log(DownloadProperties1.fileName)
    'Log(DownloadProperties1.FileExtension)
    'Log(DownloadProperties1.DownloadID)
End Sub

Sub UltimateWebView1_FileDownloadCompleted (Success As Boolean, DownloadProperties1 As DownloadProperties)
    Log("Download COMPLETED; Success:" & Success)
    'Log(DownloadProperties1.url)
    'Log(DownloadProperties1.userAgent)
    'Log(DownloadProperties1.contentDisposition)
    'Log(DownloadProperties1.mimeType)
    'Log(DownloadProperties1.contentLength)
    'Log(DownloadProperties1.cookies)
    'Log(DownloadProperties1.fileName)
    'Log(DownloadProperties1.FileExtension)
    'Log(DownloadProperties1.DownloadID)
End Sub

Private Sub UltimateWebView1_OverrideUrl (WebResourceRequest1 As WebResourceRequest) As Boolean
    'Log(WebResourceRequest1.GetUrl)
    'Log(WebResourceRequest1.GetMethod)
    'Log(WebResourceRequest1.HasGesture)
    'Log(WebResourceRequest1.IsForMainFrame)
    'Log(WebResourceRequest1.IsRedirect)
    'Dim M As Map=WebResourceRequest1.GetRequestHeaders
    'If M.IsInitialized Then
        'For i=0 To M.Size-1
            'Log(M.GetKeyAt(i))
            'Log(M.GetValueAt(i))
        'Next
    'End If
    Return False
End Sub

Sub UltimateWebView1_PageFinished (Url As String)
 
End Sub

Sub UltimateWebView1_PageStarted (Url As String, FavIcon As Bitmap)
    'If FavIcon<>Null Then
        'do stuff...
    'End If
End Sub

Sub UltimateWebView1_PageLoadingProgressChanged(Progress As Int)
 
End Sub

Sub UltimateWebView1_ReceivedIcon (Icon As Bitmap)
    'If Icon<>Null Then
    'do stuff...
    'End If
End Sub

Sub UltimateWebView1_ReceivedTitle (Title As String)
 
End Sub

'Very important event for UltimateWebView permissions request!!!
Sub UltimateWebView1_PermissionRequest (RequestedPermission As String)
    Dim Permissions As RuntimePermissions
    Permissions.CheckAndRequest(RequestedPermission)
    Wait For Activity_PermissionResult (Permission As String, result As Boolean)
    UltimateWebView1.GrantPermission(result)
End Sub

Sub UltimateWebView1_ScaleChanged (OldScale As Float, NewScale As Float)
 
End Sub

Private Sub UltimateWebView1_ReceivedError (WebResourceRequest1 As WebResourceRequest, WebResourceError1 As WebResourceError)
    'Log("ReceivedError")
    'Log(WebResourceRequest1.GetUrl)
    'Log(WebResourceError1.Description)
    'Log(WebResourceError1.ErrorCode)
End Sub

Private Sub UltimateWebView1_ReceivedHttpError (WebResponseRequest1 As WebResourceRequest, WebResourceResponse1 As WebResourceResponse)
    'Log("ReceivedHttpError")
    'Log(WebResponseRequest1.GetUrl)
    'Log(WebResourceResponse1.Encoding)
    'Log(WebResourceResponse1.StatusCode)
End Sub

Private Sub UltimateWebView1_ReceivedHttpAuthRequest (HttpAuthHandler1 As HttpAuthHandler, HttpAuthRequestProperties1 As HttpAuthRequestProperties)
    'Log("ReceivedHttpAuthRequest")
    'Log(HttpAuthRequestProperties1.Host)
    'Log(HttpAuthRequestProperties1.Realm)
End Sub

Private Sub UltimateWebView1_ReceivedLoginRequest (LoginRequestProperties1 As LoginRequestProperties)
    'Log("ReceivedLoginRequest")
    'Log(LoginRequestProperties1.Realm)
    'Log(LoginRequestProperties1.Account)
    'Log(LoginRequestProperties1.Args)
End Sub

Private Sub UltimateWebView1_UpdateVisitedHistory (Url As String, IsReload As Boolean)
    'Log("UpdateVisitedHistory")
    'Log(Url)
    'Log(IsReload)
End Sub


Private Sub UltimateWebView1_PageCommitVisible (Url As String)
 
End Sub

Private Sub UltimateWebView1_ShouldInterceptRequest (Request As WebResourceRequest) As WebResourceResponse
    'Log("ShouldInterceptRequest")
    'Log(Request.GetUrl)
    'Dim ins As InputStream
    'ins.InitializeFromBytesArray(Array As Byte(100,231,155),0,3)
    'Dim Response As WebResourceResponse
    'Response.Initialize
    'Response.Create("text/plain","utf-8",ins)
    'Return Response
    Return Null
End Sub


Private Sub UltimateWebView1_JsAlert (JsProperties1 As JsProperties, JsResult1 As JsResult) As Boolean
    'Log("JsAlert")
    'Log(JsProperties1.Url)
    'Log(JsProperties1.Message)
    'Log(JsProperties1.DefaultValue)
    'JsResult1.Confirm
    Return False
End Sub

Private Sub UltimateWebView1_JsBeforeUnload (JsProperties1 As JsProperties, JsResult1 As JsResult) As Boolean
    'Log("JsBeforeUnload")
    'Log(JsProperties1.Url)
    'Log(JsProperties1.Message)
    'Log(JsProperties1.DefaultValue)
    'JsResult1.Confirm
    Return False
End Sub

Private Sub UltimateWebView1_JsConfirm (JsProperties1 As JsProperties, JsResult1 As JsResult) As Boolean
    'Log("JsConfirm")
    'Log(JsProperties1.Url)
    'Log(JsProperties1.Message)
    'Log(JsProperties1.DefaultValue)
    'JsResult1.Confirm
    Return False
End Sub

UltimateWebView
Author:
Ivica Golubovic
Version: 1.1

Changes:
  • Method GetWebView is removed from UltimateWebView Class.
  • Property WebView (get/set) added to UltimateWebView Class. You can now use this property to set existing WebView to UltimateWebView.
  • Added method ClearFormData.
  • Added method ClearMatches.
  • Added method ClearSslPreferences.
  • Added method ComputeScroll.
  • Added class WebBackForwardList.
  • Added type WebHistoryItem to class WebBackForwardList.
  • Added method CopyBackForwardList.
  • Added method FindAllAsync.
  • Added method FindNext.
  • Added method FlingScroll.
You can now import an existing WebView object into UltimateWebView, it is not necessary to add an UltimateWebView object through the Designer.
Example:
Example:
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 WebView1 As WebView
    Private UltimateWebView1 As UltimateWebView
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("Main")
 
    'First Initialize UltimateWebView
    UltimateWebView1.Initialize(Me,"UltimateWebView1")
    'Import native WebView into UltimateWebView
    UltimateWebView1.WebView=WebView1
 
    UltimateWebView1.SetWebViewClient
    UltimateWebView1.SetWebChromeClient
    UltimateWebView1.Settings.JavaScriptEnabled=True
    UltimateWebView1.Settings.AllowContentAccess=True
    UltimateWebView1.Settings.AllowFileAccess=True
    UltimateWebView1.Settings.AppCacheEnabled=True
    UltimateWebView1.Settings.CacheMode=UltimateWebView1.Settings.CacheMode_LOAD_DEFAULT
    UltimateWebView1.Settings.JavaScriptCanOpenWindowsAutomatically=True
    UltimateWebView1.Settings.DisplayZoomControls=False
    UltimateWebView1.Settings.DomStorageEnabled=True
    UltimateWebView1.Settings.MediaPlaybackRequiresUserGesture=False
    UltimateWebView1.Settings.AllowFileAccessFromFileURLs=True
    UltimateWebView1.Settings.AllowUniversalAccessFromFileURLs=True
    UltimateWebView1.Settings.GeolocationEnabled=True
    UltimateWebView1.SetDownloadListener
 
    UltimateWebView1.CookieManager.AcceptCookies=True
    UltimateWebView1.CookieManager.AcceptThirdPartyCookies=True
    UltimateWebView1.CookieManager.AcceptFileSchemeCookies=True
    UltimateWebView1.CookieManager.Flush
End Sub

A very important event for the permissions required for WebRTC, Geolocation, etc. Without this event permisions will be denied.
Permission Event:
Sub UltimateWebView1_PermissionRequest (RequestedPermission As String)
    Dim Permissions As RuntimePermissions
    Permissions.CheckAndRequest(RequestedPermission)
    Wait For Activity_PermissionResult (Permission As String, result As Boolean)
    UltimateWebView1.GrantPermission(result)
End Sub

UltimateWebView
Author:
Ivica Golubovic
Version: 1.2

Changes:
  • Added event FileChooserInitialized.
  • Added class FileChooserParams.
  • Added method FileChooserStart.
  • Added automatic permissions requests for UploadFileChooser.
  • Added the ability to upload multiple files with UploadFileChooser if allowed.
  • Added event OverrideUrlWithExternalAppIntent.
FileChooserInitialized event added. It will be activated when a Web source requires uploading a file or multiple files. To start FileChooser, call the FileChooserStart method or use event objects the way you want. Do not add this event if you do not want your application to have this feature.
Example::
Private Sub UltimateWebView1_FileChooserInitialized (FilePathCallback As Object, FileChooserParams1 As FileChooserParams)
    'ForceIsCaptureEnabled As Boolean:
    '    True: Use resources such as camera, microphone, etc. by force if the required file format is appropriate.
    '    False: Use a predefined value assigned to FileChooserParams.
    UltimateWebView1.FileChooserStart(FilePathCallback,FileChooserParams1,True) 'Use this method or use your own method from given parameters
End Sub

Added event OverrideUrlWithExternalAppIntent which will process the request to launch an external application outside of UltimateWebView (e.g. PlayStore, Maps, AppGalery, applies to all installed applications). The result of the event is an Intent which you can use in the way you want (e.g. StartActivity (Intent) - opens the application or application chooser, depending on the case).
Example::
Sub UltimateWebView1_OverrideUrlWithExternalAppIntent (WebResourceRequest1 As WebResourceRequest, ExternalAppIntent As Intent) As Boolean
    'ExternalAppIntent - Intent to use
    StartActivity(ExternalAppIntent) 'You can use this method or do with event what ever you want
    Return True 'True to stop page loading and handling event, False to finish loading page (Error web page will be shown)
End Sub

UltimateWebView
Author:
Ivica Golubovic
Version: 1.3
Changes: Visit this post: https://www.b4x.com/android/forum/threads/ultimatewebview-custom-view.135666/post-859400


UltimateWebView
Author:
Ivica Golubovic
Version: 1.4
Changes: Visit this post: https://www.b4x.com/android/forum/threads/ultimatewebview-custom-view.135666/post-860033

UltimateWebView
Author:
Ivica Golubovic
Version: 1.5
Changes: Visit this post: https://www.b4x.com/android/forum/threads/ultimatewebview-custom-view.135666/post-861510

UltimateWebView
Author:
Ivica Golubovic
Version: 1.6
Changes: Visit this post for description: https://www.b4x.com/android/forum/threads/ultimatewebview-custom-view.135666/post-864977

UltimateWebView
Author:
Ivica Golubovic
Version: 1.7
Changes: Visit this post: https://www.b4x.com/android/forum/threads/ultimatewebview-custom-view.135666/post-866430

UltimateWebView
Author:
Ivica Golubovic
Version: 2.0
Changes: Visit this post: https://www.b4x.com/android/forum/threads/ultimatewebview-custom-view.135666/post-869686

UltimateWebView
Author:
Ivica Golubovic
Version: 2.01
Changes: Visit this post: https://www.b4x.com/android/forum/threads/ultimatewebview-custom-view.135666/post-869704

UltimateWebView
Author:
Ivica Golubovic
Version: 2.1
Changes: Visit this post: https://www.b4x.com/android/forum/threads/ultimatewebview-custom-view.135666/post-871370

UltimateWebView
Author:
Ivica Golubovic
Version: 2.11
Changes: Fixed bug where object is not visible in Designer (AddView-CustomView-UltimateWebView).

UltimateWebView
Author:
Ivica Golubovic
Version: 2.12
Changes: Visit this post: https://www.b4x.com/android/forum/threads/ultimatewebview-custom-view.135666/post-875746

UltimateWebView
Author:
Ivica Golubovic
Version: 2.20
Changes: Visit this post: https://www.b4x.com/android/forum/threads/ultimatewebview-custom-view.135666/post-906883

UltimateWebView
Author:
Ivica Golubovic
Version: 2.21
Changes: Visit this post: https://www.b4x.com/android/forum/threads/ultimatewebview-custom-view.135666/post-908288

UltimateWebView
Author:
Ivica Golubovic
Version: 2.3
Changes: Visit this post: https://www.b4x.com/android/forum/threads/ultimatewebview-custom-view.135666/post-912804

UltimateWebView
Author:
Ivica Golubovic
Version: 2.31
Changes: Visit this post: https://www.b4x.com/android/forum/threads/ultimatewebview-custom-view.135666/post-914392

Library references htm file is packed together with jar and xml file.

If this library makes your work easier and saves time in creating your application, please make a donation.
 
Last edited:

Mashiane

Expert
Licensed User
Longtime User
Q2. Also considering that we have WebViewExtras here in the forum, what is the relation between that and this? Can they work together? There is no need for webview estras when you use this?

Q3. What makes your library the go to library of choice? The documentation assumes that anyone who will use your library will know what they will be doing. In most cases, that is not true. Don't you want to include examples perhaps as part of demonstrating the power of this library, especially the feautures that make this lib outstanding from anything else available? This will help provide some green lights into this baby of yours.

#JustSomeThoughts
 

Ivica Golubovic

Active Member
Licensed User
One of the most interesting questions that people have asked here is about uploading a file via the webview.?

Is something like that possible with your webview library?
Yes, absolutely. The FileChooserInitialized event will be activated and the FileChooserStart method can be used to start FileChooser automatically. Of course, you can use the event parameters the way you want and do the file upload process in your own way, it is not necessary to use the FileChooserStart method.;)
Example::
Private Sub UltimateWebView1_FileChooserInitialized (FilePathCallback As Object, FileChooserParams1 As FileChooserParams)
    UltimateWebView1.FileChooserStart(FilePathCallback,FileChooserParams1,False)
End Sub

Q2. Also considering that we have WebViewExtras here in the forum, what is the relation between that and this? Can they work together? There is no need for webview estras when you use this?
As of version 1.1, the native WebView can be easily used as UltimateWebWiev (e.g. UltimateWebView1.WebView = WebView1). The same can be implemented WebView which is processed through WebViewExtras. WebViewExtra is an extra library, but it's a bit outdated. The UltimateWebView library is up to date and easy to use on the latest SDK versions. The UltimateWebView library contains almost all (and more) methods contained in the WebViewExtra library and therefore it is not necessary to use both libraries.

Q3. What makes your library the go to library of choice? The documentation assumes that anyone who will use your library will know what they will be doing. In most cases, that is not true. Don't you want to include examples perhaps as part of demonstrating the power of this library, especially the feautures that make this lib outstanding from anything else available? This will help provide some green lights into this baby of yours.
This library is not the final WebBrowser like Chrome, but a set of classes and subroutines that are combined into one whole for easier access. Some programming knowledge is required. Ask a question for all the necessary information.
 
Last edited:

JohnC

Expert
Licensed User
Longtime User
This obviously took a long time to develop - and even more time to create a library and docs for others to use.

THANK YOU for contributing such a useful library to the community!
 

Ivica Golubovic

Active Member
Licensed User
Version 1.3 released. See first post.

Changes:
  • Added class UltimateWebViewConstants (All constants required for UltimateWebView and UltimateWebViewSettings have been put into one class for better organization and visibility. They are available through the UltimateWebView.Constants or UltimateWebView.Settings.Constants property.
  • Added method InvokeZoomPicker to UltimateWebView Class .
  • Added method PostUrl to UltimateWebView Class .
  • Added property BackgroundColor to UltimateWebView Class.
  • Added property Constants to UltimateWebView Class.
  • Added property Constants to UltimateWebViewSettings Class.
  • Added property DataDirectorySuffix to UltimateWebView Class.
  • Added property NetworkAvailable to UltimateWebView Class.
  • Added property OverScrollMode to UltimateWebView Class.
  • Added property ScrollBarStyle to UltimateWebView Class.
  • Added property Text to UltimateWebView Class.
  • Fixed event OverrideUrlWithExternalAppIntent. Intent now opens the application directly if is installed.
All constants required for UltimateWebView and UltimateWebViewSettings have been put into one class for better organization and visibility. They are available through the UltimateWebView.Constants or UltimateWebView.Settings.Constants property.
Example::
UltimateWebView1.Settings.CacheMode=UltimateWebView1.Constants.CACHEMODE_LOAD_DEFAULT
Or:
Example::
UltimateWebView1.Settings.CacheMode=UltimateWebView1.Settings.Constants.CACHEMODE_LOAD_DEFAULT
 
Last edited:

Ivica Golubovic

Active Member
Licensed User
Version 1.4 released. See first post.

Changes:
  • Added class UltimateMimeTypeMap.
  • Added class UltimateURLUtil.
  • Added property MimeTypeMap to UltimateWebView Class.
  • Added property URLUtil to UltimateWebView Class.
 
Last edited:

SAFCO

New Member
Sounds Great...
But how can I load html (As String)? Is it possible? something like this: UltimateWebView.loadHtml
 

Ivica Golubovic

Active Member
Licensed User
Version 1.5 released. See first post.

Changes:
  • Added class ConsoleMessage.
  • Added type MessageLevels with predefined values.
  • Added event ConsoleMessage (ConsoleMessage1 As ConsoleMessage) As Boolean.
  • Added method LoadFileFromDirInternal to UltimateWebView Class.
  • Added method LoadFileFromDirAssets to UltimateWebView Class.
 

Ivica Golubovic

Active Member
Licensed User
Make sure to check it in both release and debug modes, as in debug mode the assets folder is a regular folder.
As always, you aim directly at the essence. This is the main reason why this method was added to the library. Since SDK29 and the introduction of Scoped Storage it is not at all possible to load a file directly from DirAssets into WebView (ERR_ACCESS_DENIED or File does not exist). The entire path to the file in DirAssets is just "file: /// AssetsDir / [File Name]" which throws out the error of a non-existent file. I found a solution that I implemented in this method and now it opens the file in Debug and Release mode. Thank you for your cooperation.
 

Ivica Golubovic

Active Member
Licensed User
Sounds Great...
But how can I load html (As String)? Is it possible? something like this: UltimateWebView.loadHtml
From lib version 1.5:
  • LoadFileFromDirAssets (FileName As String) As String
    Loads file (HTML, TXT, PNG, JPG...) from DirAssets folder into UltimateWebView.
  • LoadFileFromDirInternal (FileName As String) As String
    Loads file (HTML, TXT, PNG, JPG...) from DirInternal folder into UltimateWebView.
From SDK29 and Scoped Storage you can only load HTML file in shared folder. To open file from external you must implement default FileChooser.

Additional information: From SDK30 WebView.Settings.AllowFileAccess is set to FALSE by default. In order for WebView to be able to open any file, you first need to set this option to TRUE.
 
Last edited:

Ivica Golubovic

Active Member
Licensed User
If anyone has tested the library in any way so far, any feedback is welcome. The goal is to make the library better, more stable, easier to use, to remove bugs. Please report both the pros and cons of this library. CHEERS
 

iz0ndg

Active Member
Licensed User
Longtime User
Hi Ivica, I need to parse the html code and with this
B4X:
        UltimateWebView1.ExecuteJavaScript2("document.documentElement.outerHTML")
        Wait For UltimateWebView1_ExecuteJavaScriptResult(Result As String)
        Log("text " & Result)
I can come up with the result but I don't have the pure html text but somehow encoded like this:

B4X:
"\u003Chtml lang=\"it\">\u003Chead>\n   \u003Cmeta charset=\"utf-8\">\n\n   \u003Cmeta http-equiv=\"X-UA-Compatible\" content=\"IE=edge\">\n   \u003Cmeta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no\">\n\n   \u003C!-- Disable tap highlight on IE -->\n

but I would need pure html like this :

HTML:
<html lang="it">
<head>
   <meta charset="utf-8">
   <meta http-equiv="X-UA-Compatible" content="IE=edge">
   <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">

how can I do ?

Tks.
 

Ivica Golubovic

Active Member
Licensed User
Response from javascript execution is in JSON. You must convert JSON result to html. There is several ways on internet how you can do this in Java. You must use javacode in your B4A project. There is no simple converter.
 

Ivica Golubovic

Active Member
Licensed User
Hi Ivica, I need to parse the html code and with this
B4X:
        UltimateWebView1.ExecuteJavaScript2("document.documentElement.outerHTML")
        Wait For UltimateWebView1_ExecuteJavaScriptResult(Result As String)
        Log("text " & Result)
I can come up with the result but I don't have the pure html text but somehow encoded like this:

B4X:
"\u003Chtml lang=\"it\">\u003Chead>\n   \u003Cmeta charset=\"utf-8\">\n\n   \u003Cmeta http-equiv=\"X-UA-Compatible\" content=\"IE=edge\">\n   \u003Cmeta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no\">\n\n   \u003C!-- Disable tap highlight on IE -->\n

but I would need pure html like this :

HTML:
<html lang="it">
<head>
   <meta charset="utf-8">
   <meta http-equiv="X-UA-Compatible" content="IE=edge">
   <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">

how can I do ?

Tks.
Add this Java code into Activity:
JavaCode::
#If Java
import java.util.regex.Pattern;
import java.util.regex.Matcher;
import java.lang.StringBuffer;
import java.lang.Integer;

public String jsonToHtml (String data) {
        Pattern p = Pattern.compile("\\\\u(\\p{XDigit}{4})");
        Matcher m = p.matcher(data);
        StringBuffer buf = new StringBuffer(data.length());
        while (m.find()) {
            String ch = String.valueOf((char) Integer.parseInt(m.group(1), 16));
            m.appendReplacement(buf, Matcher.quoteReplacement(ch));
        }
        m.appendTail(buf);
        return buf.toString();
    }

#End If

Add this code in B4A after javascript execution:
BA code::
Dim jo As JavaObject=Me
Dim HtmlResult As String=jo.RunMethod("jsonToHtml",Array(Result))
Log(HtmlResult)
 

iz0ndg

Active Member
Licensed User
Longtime User
Add this Java code into Activity:
JavaCode::
#If Java
import java.util.regex.Pattern;
import java.util.regex.Matcher;
import java.lang.StringBuffer;
import java.lang.Integer;

public String jsonToHtml (String data) {
        Pattern p = Pattern.compile("\\\\u(\\p{XDigit}{4})");
        Matcher m = p.matcher(data);
        StringBuffer buf = new StringBuffer(data.length());
        while (m.find()) {
            String ch = String.valueOf((char) Integer.parseInt(m.group(1), 16));
            m.appendReplacement(buf, Matcher.quoteReplacement(ch));
        }
        m.appendTail(buf);
        return buf.toString();
    }

#End If

Add this code in B4A after javascript execution:
BA code::
Dim jo As JavaObject=Me
Dim HtmlResult As String=jo.RunMethod("jsonToHtml",Array(Result))
Log(HtmlResult)
Thank you, with some adjustments it was very useful to me !
 
Top