B4A Library WebViewSettings

Discussion in 'Additional libraries, classes and official updates' started by warwound, Dec 1, 2011.

  1. warwound

    warwound Expert Licensed User

    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.
     

    Attached Files:

    Last edited: Mar 22, 2014
  2. Erel

    Erel Administrator Staff Member Licensed User

    Thank you for sharing this. I'm sure that it will be very useful for many developers.
     
  3. fb application

    fb application New Member

    what is the webviewsharing ?

    please let me know ?
    i am begner in this.
     
  4. warwound

    warwound Expert Licensed User

    Hi there.

    Can you be a bit more specific with your question - it's too vague for me to answer...

    Martin.
     
  5. BobsYourUncle

    BobsYourUncle Member Licensed User

    Brialliant library! Really useful! Many Thanks!
     
  6. Cableguy

    Cableguy Expert Licensed User

    UserAgentString Values

    Hi Warhound...

    Wich are the accepted values for UserAgentString???

    I found this page, wich has some strings, but cant figure out how to use them correctly.

    Thanks for putting this one together...really usefull
     
  7. warwound

    warwound Expert Licensed User

    Hi.

    Here's a site dedicated to the user agent string: UserAgentString.com - Netscape version 4.0.

    They list some possible default WebView user agent strings here: UserAgentString.com - List of Android Webkit Browser User Agent Strings.

    But i'll guess you want to set a non-WebView user agent string in a WebView to enable you to view web sites that don't serve the default WebView?

    Have you seen the hack to change the default Android browser user agent string?

    Change the User Agent on an Android Phone

    On my ZTE Blade with a custom Froyo 2.2 ROM the default Android browser user agent string is:

    Code:
    Mozilla/5.0 (Linux; U; Android 2.2; en-gb; ZTE-BLADE Build/FRF91) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1
    If i change it via that hack to 'Desktop' setting then the string is:

    Code:
    Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_5_7; en-us) AppleWebKit/530.17 (KHTML, like Gecko) Version/4.0 Safari/530.17
    Now - assuming that the 'Desktop' UA string is intended to give the browser access to the full version of a web site rather than any mobile version served up to the default UA string - i'd say that the Desktop UA String (or part of?) is what is needed in a WebView to enable viewing of non-mobile versions of web sites.

    Martin.
     
  8. straybullet

    straybullet Member Licensed User

    Can you not define setAppCachePath and Android will decide where to put the cache or must you set it and if so is there a safe default path that always exists such as...


    /data/data/com.your.package.appname/cache
     
  9. warwound

    warwound Expert Licensed User

    Hi.

    The default location for the WebView cache is equivalent to File.DirInternalCache / webviewCache.

    Same as you posted: /data/data/package.name.here/cache/webviewCache

    There's no need to set this when using a WebView - if you don't set a non-default location for the cache then the default will always be used.

    Martin.
     
  10. straybullet

    straybullet Member Licensed User

    Thanks I am hoping this will allow appcache for HTML5 offline mode to work with Webview
     
  11. straybullet

    straybullet Member Licensed User

    Could you please enable JavaScript for locally stored files if at all possible, this would make Webview so much more useful.


    Thanks!
     
    Last edited: Jan 3, 2012
  12. NeoTechni

    NeoTechni Well-Known Member Licensed User

    That's a limitation with Android itself, not B4A.

    EDIT: I know a workaround. Use a server object, and host the web page on the device through that.
    The webview would connect to http://127.0.0.1 and think it's online.
     
    Last edited: Jan 3, 2012
  13. NeoTechni

    NeoTechni Well-Known Member Licensed User

    You should merge all get/sets into a single sub.
     
  14. straybullet

    straybullet Member Licensed User

    Sorry to sound so dumb, but do we have a server object for B4A that's plug and play?
     
  15. Erel

    Erel Administrator Staff Member Licensed User

    Please start a new thread for this question.
     
  16. straybullet

    straybullet Member Licensed User

    Last edited: Jan 3, 2012
  17. warwound

    warwound Expert Licensed User

    I think Erel was meaning that a new thread should be started for the subject of running a webserver locally on the device.

    Back to your original question though...

    Do you mean that if you load a webpage locally via the filesystem that javascript is not enabled?

    I load webpages from the filesystem and have no problems executing javascript in those pages.
    (Maybe you're referring to HTML5 features that will only work when the webpage has been loaded from a webserver and these HTML5 features will not work when a webpage is loaded using from the file system using the file prototcol (file:///etc)?

    In reply to NeoTechni's post...

    Yes it looks like a single get and a single set method would be far better than all these individual set and get methods BUT...

    The existing library offers IDE context help for each method.

    If i re-wrote the library with a single get and a single set method that context help would either have to be removed OR the help would have to cover every available setting.

    Does that make sense?

    If you use the existing library then it's IDE friendly.

    An updated library (one set and one get method) would be very cumbersome in the IDE.
    The context help pop-up would have to cover every available setting.

    Meanwhile i'm very interested in running a minimal webserver locally so shall wait for the new thread on that subject. :)

    Martin.
     
  18. straybullet

    straybullet Member Licensed User

    Martin,

    Thanks, yes HTML5 features would be a huge plus to anyone that wants to expand their apps using Webview, but I had issues with simple Javascript not running in the current webview.

    I like to put a Javascript search on my page so users can search for text and so far none of the source I have tried works in the current webview, it doesn't even seem to execute. Thats why I was requesting a webview (or now a simple http server, which I know is more difficult that I am making it sound) to serve up pages locally to bypass the restrictions of local pages not running HTML5
     
  19. warwound

    warwound Expert Licensed User

    Hi all.

    WebViewSettings is now updated to version 1.1.

    Five new methods have been added:

    getDatabasePath()
    Returns the location where the database storage API databases are saved.

    getDOMStorageEnabled()
    Returns whether the DOM storage API is enabled.

    setDatabaseEnabled()
    Set whether the database storage API is enabled.

    setDatabasePath()
    Sets the location where the database storage API databases are saved.
    Pass a path as a String or pass an empty "" String.
    If path is an empty String "" then a default path will be set.

    setDOMStorageEnabled()
    Set whether the DOM storage API is enabled.

    I have also updated my WebViewExtras library to enable the HTML5 database stoarge API.

    Both WebViewExtras and WebViewSettings are required to enable the database storage API in a WebView:

    Code:
    Sub Globals
       
    Dim WebView1 As WebView
       
    Dim WebViewExtras1 As WebViewExtras
       
    Dim WebViewSetting1 As WebViewSettings
    End Sub

    Sub Activity_Create(FirstTime As Boolean)
       WebView1.Initialize(
    "WebView1")
       
       WebView1.JavaScriptEnabled=
    True
       WebViewExtras1.addJavascriptInterface(WebView1, 
    "B4A"' NOT ESSENTIAL
       WebViewExtras1.addWebChromeClient(WebView1)
       WebViewSetting1.setDatabaseEnabled(WebView1, 
    True)
       
    '   WebViewSetting1.setDOMStorageEnabled(WebView1, True)
       
       
    Log("DefaultWebViewDatabasePath: "&WebViewSetting1.getDatabasePath(WebView1))
       
    '   the WebView will fail to create any databases if the database path is NOT set
       WebViewSetting1.setDatabasePath(WebView1, "")
       
    Log("NewWebViewDatabasePath: "&WebViewSetting1.getDatabasePath(WebView1))
       
       
       
    Activity.AddView(WebView1, 00100%x100%y)
       WebView1.LoadUrl(
    "file:///android_asset/my_webpage.htm")
       
    End Sub
    The WebView needs the WebChromeClient from WebViewExtras and then you need to setDatabaseEnabled to True and call getDatabasePath.

    With that done you can now create and use the database storage API - i'd suggest a Google search for more info on the database storage API, but here's a link that'll outline the possibilities: Introducing Web SQL Databases | HTML5 Doctor.

    You can see i've commented out a line WebViewSetting1.setDOMStorageEnabled.
    I've included the set and get DOM storage enabled methods here for completeness but have not used either method so far.
    It looks as though the DOM storage API is what's known as the HTML5 localStorage API.

    Verions 1.1 of WebViewSettings is attached to post #1 in this thread.

    Martin.
     
    Last edited: Apr 7, 2012
  20. nad

    nad Active Member Licensed User

    Hello Martin,

    I would like to use a webview and be able to pinch in and out with both fingers but hide the zoom in/out buttons the webview creates. (I want to design my own buttons)

    If i use webview1.zoomenabled=true it disables the pinch (not what i need).

    Wondering if .setDisplayZoomControls(false); could be included in your library in the future.

    That would be great!. Thanks for your awesome library.

    Cheers,
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice