B4A Library WebViewSettings

Hi all.

Here's a new library that makes it easy to set and get various WebView WebSettings.

Note that the official documentation for many of these settings is rather brief, sometimes vague or virtually non-existent!

Here is an alphabetical list of all methods with a short description.
I've implemented methods for many of the available settings but NOT all available settings.
If anyone has a particular need for a setting to be added please post a request in this thread.

WebViewSettings
Version:
1.31
  • WebViewSettings
    Methods:
    • getAllowFileAccess (webView1 As WebView) As Boolean
      Returns true if this WebView supports file access.
    • getCacheMode (webView1 As WebView) As String
      Return the current setting for overriding the cache mode.
      Possible return values are: "LOAD_CACHE_ELSE_NETWORK", "LOAD_CACHE_ONLY", "LOAD_DEFAULT", "LOAD_NO_CACHE" and "LOAD_NORMAL"
    • getCursiveFontFamily (webView1 As WebView) As String
      Get the cursive font family name. The default is "cursive".
      Returns the cursive font family name as a string.
    • getDOMStorageEnabled (webView1 As WebView) As Boolean
      Returns whether the DOM storage API is enabled.
    • getDatabasePath (webView1 As WebView) As String
      Returns the location where the database storage API databases are saved.
    • getDefaultFixedFontSize (webView1 As WebView) As Int
      Get the default fixed font size. The default is 16.
      Returns a non-negative integer between 1 and 72.
    • getDefaultFontSize (webView1 As WebView) As Int
      Get the default font size. The default is 16.
      Returns a non-negative integer between 1 and 72.
    • getDefaultTextEncodingName (webView1 As WebView) As String
      Get the default text encoding name. The default is "Latin-1".
      Returns the default text encoding name as a string.
    • getDefaultZoom (webView1 As WebView) As String
      Get the default zoom density of the page.
      Possible return values are: "CLOSE", "FAR" and "MEDIUM"
    • getDisplayZoomControls (webView1 As WebView) As Boolean
      Returns true if the on screen zoom buttons are displayed.
      Only supported on Android API level 11 and later.
    • getJavaScriptCanOpenWindowsAutomatically (webView1 As WebView) As Boolean
      Get if javascript can open windows automatically. The default is false.
      Returns true if javascript can open windows automatically during window.open().
    • getLightTouchEnabled (webView1 As WebView) As Boolean
      Returns true if light touches are enabled.
    • getLoadWithOverviewMode (webView1 As WebView) As Boolean
      Returns true if this WebView loads page with overview mode.
    • getLoadsImagesAutomatically (webView1 As WebView) As Boolean
      Return true if the WebView will load image resources automatically. The default is true.
    • getMinimumFontSize (webView1 As WebView) As Int
      Get the minimum font size. The default is 8.
      Returns a non-negative integer between 1 and 72.
    • getPluginState (webView1 As WebView) As String
      Get the current plugin state.
      Possible return values are: "OFF", "ON" and "ON_DEMAND"
    • getSaveFormData (webView1 As WebView) As Boolean
      Get whether the WebView is saving form data and displaying prior entries/autofill.
    • getSavePassword (webView1 As WebView) As Boolean
      Get whether the WebView is saving passwords.
    • getUseWideViewPort (webView1 As WebView) As Boolean
      Returns true if the WebView is using a wide viewport.
    • getUserAgentString (webView1 As WebView) As String
      Get the WebView's user-agent string.
    • setAllowFileAccess (webView1 As WebView, allow As Boolean)
      Enable or disable file access within WebView. File access is enabled by default.
      Note that this enables or disables file system access only.
      Assets and resources are still accessible using file:///android_asset
    • setAppCacheEnabled (webView1 As WebView, flag As Boolean)
      Tell the WebView to enable Application Caches API.
    • setAppCacheMaxSize (webView1 As WebView, appCacheMaxSize As Long)
      Set the maximum size for the Application Caches content.
      appCacheMaxSize - The cache size in bytes.
    • setAppCachePath (webView1 As WebView, appCachePath As String)
      Set a custom path to the Application Caches files.
      The client must ensure it exists before this call.
      appCachePath - String path to the directory containing Application Caches files. The appCache path can be the empty string but should not be null.
    • setCacheMode (webView1 As WebView, cacheMode As String)
      Override the way the cache is used. The way the cache is used is based on the navigation option.
      For a normal page load, the cache is checked and content is re-validated as needed.
      When navigating back, content is not revalidated, instead the content is just pulled from the cache.
      This function allows the client to override this behavior.
      cacheMode - Possible values are: "LOAD_CACHE_ELSE_NETWORK", "LOAD_CACHE_ONLY", "LOAD_DEFAULT", "LOAD_NO_CACHE" and "LOAD_NORMAL"
    • setCursiveFontFamily (webView1 As WebView, font As String)
      Set the cursive font family name.
      font - A font family name.
    • setDOMStorageEnabled (webView1 As WebView, Enabled As Boolean)
      Set whether the DOM storage API is enabled.
    • setDatabaseEnabled (webView1 As WebView, enabled As Boolean)
      Set whether the database storage API is enabled.
    • setDatabasePath (webView1 As WebView, path As String)
      Sets the location where the database storage API databases are saved.
      If path is an empty String "" then a default path will be set.
    • setDefaultFixedFontSize (webView1 As WebView, size As Int)
      Set the default fixed font size.
      size - A non-negative integer between 1 and 72.
    • setDefaultFontSize (webView1 As WebView, size As Int)
      Set the default font size.
      size - A non-negative integer between 1 and 72.
    • setDefaultTextEncodingName (webView1 As WebView, encoding As String)
      Set the default text encoding name to use when decoding html pages.
      The default is "Latin-1".
    • setDefaultZoom (webView1 As WebView, density As String)
      Set the default zoom density of the page.
      density - Possible values are: "CLOSE", "FAR" and "MEDIUM".
    • setDisplayZoomControls (webView1 As WebView, Enabled As Boolean)
      Sets whether the on screen zoom buttons are displayed.
      A combination of built in zoom controls enabled and on screen zoom controls disabled allows for pinch to zoom to work without the on screen controls.
      Only supported on Android API level 11 and later.
    • setGeolocationEnabled (webView1 As WebView, Enabled As Boolean)
      Sets whether Geolocation is enabled.
    • setJavaScriptCanOpenWindowsAutomatically (webView1 As WebView, flag As Boolean)
      Tell javascript to open windows automatically.
      This applies to the javascript function window.open().
    • setLightTouchEnabled (webView1 As WebView, enabled As Boolean)
      Enables using light touches to make a selection and activate mouseovers.
    • setLoadWithOverviewMode (webView1 As WebView, overview As Boolean)
      Set whether the WebView loads a page with overview mode.
    • setLoadsImagesAutomatically (webView1 As WebView, flag As Boolean)
      Tell the WebView to load image resources automatically.
    • setMediaPlaybackRequiresUserGesture (webView1 As WebView, Requires As Boolean)
      Sets whether the WebView requires a user gesture to play media.
      The default is true.
    • setMinimumFontSize (webView1 As WebView, size As Int)
      Set the minimum font size.
      size - A non-negative integer between 1 and 72.
    • setPluginState (webView1 As WebView, state As String)
      Tell the WebView to enable, disable, or have plugins load on demand.
      On demand mode means that if a plugin exists that can handle the embedded content, a placeholder icon will be shown instead of the plugin.
      When the placeholder is clicked, the plugin will be enabled.
    • setSaveFormData (webView1 As WebView, save As Boolean)
      Store whether the WebView is saving form data.
    • setSavePassword (webView1 As WebView, save As Boolean)
      Store whether the WebView is saving password.
    • setSupportZoom (webView1 As WebView, support As Boolean)
      Set whether the WebView supports zoom.
    • setUseWideViewPort (webView1 As WebView, use As Boolean)
      Tell the WebView to use the wide viewport.
    • setUserAgentString (webView1 As WebView, userAgent As String)
      Set the WebView's user-agent string.
      If the string "userAgent" is null or empty, it will use the system default user-agent string.
    • supportZoom (webView1 As WebView) As Boolean
      Returns whether the WebView supports zoom.

Library files and demo attached to this post.
The demo is not a full demo - it simply shows the syntax to use for the getSavePassword and setSavePassword methods.
But you can soon update the demo to experiment with any of the other settings.

Martin.
 

Attachments

  • WebViewSettings_v_1_31.zip
    12.5 KB · Views: 4,196
Last edited:

warwound

Expert
Licensed User
Longtime User
Hi,

New to B4a and was trying to get a webview to allow multiple windows - I see in your documentation that you have the following

SetSupportMultipleWindows (Support AsBoolean)

But I don't think it has been defined within the WebExtras library. Is it already defined and am I missing it

Thank you

What are you hoping to achieve with this method?

Look at the documentation: http://developer.android.com/refere...tings.html#setSupportMultipleWindows(boolean)
Sets whether the WebView whether supports multiple windows.
If set to true, onCreateWindow(WebView, boolean, boolean, Message) must be implemented by the host application.
The default is false.
Here's the documentation for the WebChromeClient onCreateWindow method.

Looks like you'd need a custom WebView to use this feature.

Martin.
 

JdV

Active Member
Licensed User
Longtime User
Martin

This is a brilliant library and has saved me a lot of work. Thanks.

Is it possible to suppress the feature where a webview is automatically zoomed in when a user is entering text into a field?

I have both of these options set but it still zooms in when I press on a text field:
B4X:
webviewsettings1.setSupportZoom(webview1, False)
webviewsettings1.setDefaultZoom(webview1, "FAR")

Regards

Joe
 

kkkpe

Active Member
Licensed User
Longtime User
HELP...HELP... I can not get the geolocation:

manifest editor:
AddPermission(android.permission.ACCESS_FINE_LOCATION)

Sub Globals
'These global variables will be redeclared each time the activity is created.
'These variables can only be accessed from this module.
Dim WebView1 As WebView
Dim WebViewExtras1 As WebViewExtras
Dim WebViewSetting1 As WebViewSettings
Dim settaggi As WebSettings
Dim WebChromeClient1 As DefaultWebChromeClient '---lib2
End Sub

Sub Activity_Create(FirstTime As Boolean)
WebView1.Initialize("WebView1")
WebView1.JavaScriptEnabled=True

WebChromeClient1.Initialize("WebChromeClient1")

WebViewExtras1.Initialize(WebView1)
WebViewExtras1.addJavascriptInterface(WebView1, "B4A") ' NOT ESSENTIAL
WebViewExtras1.SetWebChromeClient(WebChromeClient1)


WebViewSetting1.setDatabaseEnabled(WebView1, True)
WebViewSetting1.setAppCacheEnabled(WebView1,True)
WebViewSetting1.setDOMStorageEnabled(WebView1,True)
WebViewSetting1.setGeolocationEnabled(WebView1,True)
WebViewSetting1.setJavaScriptCanOpenWindowsAutomatically(WebView1,True)
WebViewSetting1.setLoadWithOverviewMode(WebView1,True)
WebViewSetting1.setDatabasePath(WebView1,"")



'Log("DefaultWebViewDatabasePath: "&WebViewSetting1.getDatabasePath(WebView1))
' the WebView will fail to create any databases if the database path is NOT set

'Log("NewWebViewDatabasePath: "&WebViewSetting1.getDatabasePath(WebView1))


Activity.AddView(WebView1, 0, 0, 100%x, 100%y)
WebView1.LoadUrl("http://192.168.1.50/mobile/test.php")
End Sub
 

warwound

Expert
Licensed User
Longtime User
HELP...HELP... I can not get the geolocation:

manifest editor:
AddPermission(android.permission.ACCESS_FINE_LOCATION)

Sub Globals
'These global variables will be redeclared each time the activity is created.
'These variables can only be accessed from this module.
Dim WebView1 As WebView
Dim WebViewExtras1 As WebViewExtras
Dim WebViewSetting1 As WebViewSettings
Dim settaggi As WebSettings
Dim WebChromeClient1 As DefaultWebChromeClient '---lib2
End Sub

Sub Activity_Create(FirstTime As Boolean)
WebView1.Initialize("WebView1")
WebView1.JavaScriptEnabled=True

WebChromeClient1.Initialize("WebChromeClient1")

WebViewExtras1.Initialize(WebView1)
WebViewExtras1.addJavascriptInterface(WebView1, "B4A") ' NOT ESSENTIAL
WebViewExtras1.SetWebChromeClient(WebChromeClient1)


WebViewSetting1.setDatabaseEnabled(WebView1, True)
WebViewSetting1.setAppCacheEnabled(WebView1,True)
WebViewSetting1.setDOMStorageEnabled(WebView1,True)
WebViewSetting1.setGeolocationEnabled(WebView1,True)
WebViewSetting1.setJavaScriptCanOpenWindowsAutomatically(WebView1,True)
WebViewSetting1.setLoadWithOverviewMode(WebView1,True)
WebViewSetting1.setDatabasePath(WebView1,"")



'Log("DefaultWebViewDatabasePath: "&WebViewSetting1.getDatabasePath(WebView1))
' the WebView will fail to create any databases if the database path is NOT set

'Log("NewWebViewDatabasePath: "&WebViewSetting1.getDatabasePath(WebView1))


Activity.AddView(WebView1, 0, 0, 100%x, 100%y)
WebView1.LoadUrl("http://192.168.1.50/mobile/test.php")
End Sub

What goes wrong?
Do you get an event raised when your webpage requests a location?
I see no sub to handle the event raised when the webpage requests a location...
 

deantangNYP

Active Member
Licensed User
Longtime User
HI Sir,

i am trying to scale and fit my video stream within my webview. Tried the following, but could not.
Please advise. Thanks

WebViewSettings1.setUseWideViewPort(WebView1,True)
WebViewSettings1.se.setLoadWithOverviewMode(WebView1,True)
WebView1.LoadUrl("http://192.168.0.51:9090/stream")
 

JohnC

Expert
Licensed User
Longtime User
I'm getting a weird problem...

I am using:
B4X:
wvs.setDisplayZoomControls(wv,False)
So the zoom buttons are not displayed in the webview. And this works fine.

But I recently added the IME control so that when the user clicks on a textbox in a webpage, I shorten the webview size so the textbox will be visible when the keyboard is visible.

But, now the zoom control are visible again like it ignores the above setting.

I narrowed it down to this line:
B4X:
IME.AddHeightChangedEvent
When I add this line to the project, that's when the zoom buttons will appear.

Any idea why adding this lines causes the webview to ignore the .SetDisplayZoonControls(wv, False) setting?
 

JohnC

Expert
Licensed User
Longtime User
This is actually turned into a big problem because I can't seem to get the wvs.setDisplayZoomControls(wv,False) method working with IME.

So, it looks very unprofessional for the zoom controls to keep appearing every time the user scrolls the webpage without doing any zooming.

Could this be a problem with this library of the mix of Webview and IME?
 
Last edited:

cenyu

Active Member
Licensed User
Longtime User
Is it possible to set WebViewSetting to disable SSL and open https page
 

JohnC

Expert
Licensed User
Longtime User
Is it possible to set WebViewSetting to disable SSL and open https page
If you try to make an "Http" connection, and it automatically changes to "Https", then that usually means the website is configured to force an https connection and there is nothing you can do on your end to prevent this.

Most websites are forcing https connections now because google make a new rule that non-https websites will be ranked lower in search results.
 
Last edited:

cenyu

Active Member
Licensed User
Longtime User
B4X:
Dim URL                 As String
    Dim webview1             As WebView
    Dim WebViewExtras1         As WebViewExtras
    webview1.Initialize("webview1")
    Activity.AddView (webview1,0,0,100%x,100%y)
    URL="https://192.168.0.200"
    WebViewExtras1.addWebChromeClient(webview1, "WebViewExtras1")
    webview1.LoadUrl(URL)


I try this but empty page is shown
 
Top