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,225
Last edited:

Inman

Well-Known Member
Licensed User
Longtime User
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.

For some reason this method is not working for me (Android 4.2.2 ROM based on CM10.1). The zoom button continues to be visible. If you have a device with Jellybean, could you please give it a try?
 

warwound

Expert
Licensed User
Longtime User
I don't have an Android 4.2.2 device.
Testing on 4.1.1 (Galaxy Tab2) and 4.1.2 (Galaxy S3) the zoom controls are displayed or hidden as expected.

A search on Google does not return any relevant info regarding 4.2.2 so i'm not sure what to suggest.
The official documentation says the method works on Android API 11 and newer: WebSettings | Android Developers, with no mention of any changes in Jelly Bean 4.2.2.

If you use setSupportZoom(WebView1, False) and then setDisplayZoomControls(WebView1, False) do you still see the zoom controls?
If not then try setSupportZoom(WebView1, True) after those two statements and see if zoom is enabled without displaying the zoom control.

Other than that i can only leave you to scour the web for relevant info and if you find a solution post again and i'll try and add the solution to WebViewSettings.

You use the WebView a lot it seems from your posts - would you like to test/preview the new but not yet uploaded version of WebViewExtras?
The update is a major update - completely incompatible with the previous version and also very much untested (hence the need for beta testers).

The new version of WebViewExtras wraps the WebSettings object so WebViewSettings will no longer exist as a separate library.
Take a look at the reference: WebViewExtras.
Support for FindListener, WebBackForwardList, WebHistoryItem, WebResourceResponse and more.
The WebViewClient raises a ScaleChanged(OldScale As Float, NewScale As Float) event - that's something you previously requested but was only implemented in the deprecated form, not as an event.

Let me know if setSupportZoom effects display of the built in zoom controls and also if you'd like to test the new WebViewExtras.

Martin.
 

Inman

Well-Known Member
Licensed User
Longtime User
I will try out your suggestions and get back.

The new WebViewExtras sound lovely. I am currently using the available version in my project. I need to push a major update for my app in the next few days so I can't test your new version now as the incompatibility will make it difficult. I will get in touch with you once I am free.
 
Last edited:

Inman

Well-Known Member
Licensed User
Longtime User
I had a webview.sendtoback line in my code, removing which the zoom controls disappeared using your library. Weird!
 

airblaster

Active Member
Licensed User
Longtime User
Hi warwound,

I'm currently using the unreleased beta version of WebViewExtras you posted above.
Somehow I can't get AddJavascriptInterface to work there. Is it already implemented, or am I trying to use an unfinished function?

The Code I use looks like this:
B4X:
WebViewExtras1.AddJavascriptInterface(WebView1, "B4A")
WebViewExtras1.LoadUrl("javascript:B4A.CallSub('getCouponId', false, document.getElementsByName('cp:coupon_id')[0].getAttribute('content'));")

I also tried
B4X:
WebViewExtras1.LoadUrl("javascript:window.B4A.CallSub('getCouponId', false, document.getElementsByName('cp:coupon_id')[0].getAttribute('content'));")
But it didn't work either.

In both cases I get the following error message:
B4X:
Uncaught TypeError: Object android.webkit.WebView@41acd720 has no method 'CallSub' in null (Line: 1)
 

warwound

Expert
Licensed User
Longtime User
Hi.

Try something like this:

B4X:
Dim JavascriptInterface1 As JavascriptInterface
JavascriptInterface1.Initialize
WebViewExtras1.AddJavascriptInterface(JavascriptInterface1, "B4A")

Martin.
 

Eric H

Active Member
Licensed User
Longtime User
B4X:
WebViewSettings1.setUserAgentString(WebView1, "DESKTOP_USERAGENT")
works for me to enable desktop user agent. Just posting it in case it is useful to someone searching this thread.
 

GiulioVale

Active Member
Licensed User
Longtime User
setDisplayZoomControls cause error on Android 2.3.6.
edit: api level error

Any solution to catch "this error"?
 
Last edited:

chrjak

Active Member
Licensed User
Longtime User
Hey
Maybe you can add the feature, that disables marking and copy to clipboard function
 

Swissmade

Well-Known Member
Licensed User
Longtime User
Nice Lib can this also be made for B4J
 

JohnC

Expert
Licensed User
Longtime User
Hi, I am trying to get HTML5 videos to play automatically on page load without the user having to click the play button.

On this page it seems this can be done with android 4.2.2+ (second post from the bottom):
http://stackoverflow.com/questions/...5-video-autoplay-not-working-on-android-4-0-3

by doing the following:
WebView.getSettings().setMediaPlaybackRequiresUserGesture(false);

But obviously your library does not support this settings yet.

Is there any change of adding this setting to your library soon?

Thanks.
 

warwound

Expert
Licensed User
Longtime User
@JohnCody

WebViewSettings updated to version 1.31

New method added:

  • setMediaPlaybackRequiresUserGesture (webView1 As WebView, Requires As Boolean)
    Sets whether the WebView requires a user gesture to play media.
    The default is true.

Version 1.31 is attached to the first post in this thread.

Martin.
 

JohnC

Expert
Licensed User
Longtime User
@JohnCody

WebViewSettings updated to version 1.31

New method added:

  • setMediaPlaybackRequiresUserGesture (webView1 As WebView, Requires As Boolean)
    Sets whether the WebView requires a user gesture to play media.
    The default is true.
wow! that was quick - I will be working on that project again early next week and let you know how it works.

Thank you :)
 

warwound

Expert
Licensed User
Longtime User
If your project requires advanced control and configuration of the WebView you might be interested in my WebViewExtras2 library...
It's the update to WebViewExtras, combining all features of WebViewExtras and WebViewSettings and a lot lot more.

You can find a reference document here: http://b4a.martinpearman.co.uk/webviewextras/WebViewExtras2.html.
The library can be downloaded from here: http://b4a.martinpearman.co.uk/webviewextras/WebViewExtras2-20140318.zip.

If you read through the WebViewExtras thread from post #76 onwards you'll see i've been creating a custom version of the new WebViewExtras2 library for forum member andrewj and i'll be updating WebViewExtras2 with the new features from this custom library over the next week or so (as time permits).

I've not properly uploaded WebViewExtras2 to the forum yet - again time is limited and i have not got time to put together demo projects for it.
But it's working fine and might be of use to you.

Martin.
 

JohnC

Expert
Licensed User
Longtime User
Excellent!

By first adding the chrome client to the webview, then disabling this "setMediaPlaybackRequiresUserGesture" setting, the webview can now auto-play an HTML5 video by adding the xxx.play() method in the window.onload event for the page.

Thank you :)

[note: this setting might only work with 4.2.2+ devices]
 

Simon Blackwell

New Member
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
 
Top