WebSettings

Manages settings state for a WebView. When a WebView is first created, it
obtains a set of default settings. These default settings will be returned
from any getter call. A WebSettings object obtained from
WebView.getSettings() is tied to the life of the WebView. If a WebView has
been destroyed, any method call on WebSettings will throw an
IllegalStateException.

Causes the current thread to wait until another thread invokes the
notify() method or the
notifyAll() method for this object, or
some other thread interrupts the current thread, or a certain
amount of real time has elapsed.

LOAD_DEFAULT

Default cache usage mode. If the navigation type doesn't impose any
specific behavior, use cached resources when they are available
and not expired, otherwise load resources from the network.
Use with setCacheMode(int).

MIXED_CONTENT_ALWAYS_ALLOW

Used with setMixedContentMode(int)
In this mode, the WebView will allow a secure origin to load content from any other origin,
even if that origin is insecure. This is the least secure mode of operation for the WebView,
and where possible apps should not set this mode.

Constant Value:
0
(0x00000000)

MIXED_CONTENT_COMPATIBILITY_MODE

Used with setMixedContentMode(int)
In this mode, the WebView will attempt to be compatible with the approach of a modern web
browser with regard to mixed content. Some insecure content may be allowed to be loaded by
a secure origin and other types of content will be blocked. The types of content are allowed
or blocked may change release to release and are not explicitly defined.
This mode is intended to be used by apps that are not in control of the content that they
render but desire to operate in a reasonably secure environment. For highest security, apps
are recommended to use MIXED_CONTENT_NEVER_ALLOW.

Constant Value:
2
(0x00000002)

MIXED_CONTENT_NEVER_ALLOW

Used with setMixedContentMode(int)
In this mode, the WebView will not allow a secure origin to load content from an insecure
origin. This is the preferred and most secure mode of operation for the WebView and apps are
strongly advised to use this mode.

setAllowFileAccess

Enables or disables 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 and
file:///android_res.

Parameters

allow

boolean

setAllowFileAccessFromFileURLs

Sets whether JavaScript running in the context of a file scheme URL
should be allowed to access content from other file scheme URLs. To
enable the most restrictive, and therefore secure, policy this setting
should be disabled. Note that the value of this setting is ignored if
the value of getAllowUniversalAccessFromFileURLs() is true.
Note too, that this setting affects only JavaScript access to file scheme
resources. Other access to such resources, for example, from image HTML
elements, is unaffected. To prevent possible violation of same domain policy
when targeting Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1 and earlier,
you should explicitly set this value to false.

setAllowUniversalAccessFromFileURLs

Sets whether JavaScript running in the context of a file scheme URL
should be allowed to access content from any origin. This includes
access to content from other file scheme URLs. See
setAllowFileAccessFromFileURLs(boolean). To enable the most restrictive,
and therefore secure policy, this setting should be disabled.
Note that this setting affects only JavaScript access to file scheme
resources. Other access to such resources, for example, from image HTML
elements, is unaffected. To prevent possible violation of same domain policy
when targeting Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1 and earlier,
you should explicitly set this value to false.

setAppCacheEnabled

Sets whether the Application Caches API should be enabled. The default
is false. Note that in order for the Application Caches API to be
enabled, a valid database path must also be supplied to
setAppCachePath(String).

Parameters

flag

boolean: true if the WebView should enable Application Caches

setAppCacheMaxSize

This method was deprecated
in API level 18.
In future quota will be managed automatically.

Sets the maximum size for the Application Cache content. The passed size
will be rounded to the nearest value that the database can support, so
this should be viewed as a guide, not a hard limit. Setting the
size to a value less than current database size does not cause the
database to be trimmed. The default size is Long.MAX_VALUE.
It is recommended to leave the maximum size set to the default value.

Parameters

appCacheMaxSize

long: the maximum size in bytes

setAppCachePath

Sets the path to the Application Caches files. In order for the
Application Caches API to be enabled, this method must be called with a
path to which the application can write. This method should only be
called once: repeated calls are ignored.

setBlockNetworkImage

Sets whether the WebView should not load image resources from the
network (resources accessed via http and https URI schemes). Note
that this method has no effect unless
getLoadsImagesAutomatically() returns true. Also note that
disabling all network loads using setBlockNetworkLoads(boolean)
will also prevent network images from loading, even if this flag is set
to false. When the value of this setting is changed from true to false,
network images resources referenced by content currently displayed by
the WebView are fetched automatically. The default is false.

Parameters

flag

boolean: whether the WebView should not load image resources from the
network

setBlockNetworkLoads

Sets whether the WebView should not load resources from the network.
Use setBlockNetworkImage(boolean) to only avoid loading
image resources. Note that if the value of this setting is
changed from true to false, network resources referenced by content
currently displayed by the WebView are not fetched until
WebView.reload() is called.
If the application does not have the
Manifest.permission.INTERNET permission, attempts to set
a value of false will cause a SecurityException
to be thrown. The default value is false if the application has the
Manifest.permission.INTERNET permission, otherwise it is
true.

setBuiltInZoomControls

Sets whether the WebView should use its built-in zoom mechanisms. The
built-in zoom mechanisms comprise on-screen zoom controls, which are
displayed over the WebView's content, and the use of a pinch gesture to
control zooming. Whether or not these on-screen controls are displayed
can be set with setDisplayZoomControls(boolean). The default is false.

The built-in mechanisms are the only currently supported zoom
mechanisms, so it is recommended that this setting is always enabled.

Parameters

enabled

boolean: whether the WebView should use its built-in zoom mechanisms

setCacheMode

Overrides the way the cache is used. The way the cache is used is based
on the navigation type. 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 retrieved from the cache.
This method allows the client to override this behavior by specifying
one of LOAD_DEFAULT,
LOAD_CACHE_ELSE_NETWORK, LOAD_NO_CACHE or
LOAD_CACHE_ONLY. The default value is LOAD_DEFAULT.

setDatabaseEnabled

Sets whether the database storage API is enabled. The default value is
false. See also setDatabasePath(String) for how to correctly set up the
database storage API.
This setting is global in effect, across all WebView instances in a process.
Note you should only modify this setting prior to making any WebView
page load within a given process, as the WebView implementation may ignore
changes to this setting after that point.

setDatabasePath

This method was deprecated
in API level 19.
Database paths are managed by the implementation and calling this method
will have no effect.

Sets the path to where database storage API databases should be saved.
In order for the database storage API to function correctly, this method
must be called with a path to which the application can write. This
method should only be called once: repeated calls are ignored.

setDefaultZoom

This method was deprecated
in API level 19.
This method is no longer supported, see the function documentation for
recommended alternatives.

Sets the default zoom density of the page. This must be called from the UI
thread. The default is WebSettings.ZoomDensity.MEDIUM.
This setting is not recommended for use in new applications. If the WebView
is utilized to display mobile-oriented pages, the desired effect can be achieved by
adjusting 'width' and 'initial-scale' attributes of page's 'meta viewport'
tag. For pages lacking the tag, WebView.setInitialScale(int)
and setUseWideViewPort(boolean) can be used.

setEnableSmoothTransition

This method was deprecated
in API level 17.
This method is now obsolete, and will become a no-op in future.

Sets whether the WebView will enable smooth transition while panning or
zooming or while the window hosting the WebView does not have focus.
If it is true, WebView will choose a solution to maximize the performance.
e.g. the WebView's content may not be updated during the transition.
If it is false, WebView will keep its fidelity. The default value is false.

setGeolocationDatabasePath

This method was deprecated
in API level 24.
Geolocation database are managed by the implementation and calling this method
will have no effect.

Sets the path where the Geolocation databases should be saved. In order
for Geolocation permissions and cached positions to be persisted, this
method must be called with a path to which the application can write.

setLoadWithOverviewMode

Sets whether the WebView loads pages in overview mode, that is,
zooms out the content to fit on screen by width. This setting is
taken into account when the content width is greater than the width
of the WebView control, for example, when getUseWideViewPort()
is enabled. The default is false.

Parameters

overview

boolean

setLoadsImagesAutomatically

Sets whether the WebView should load image resources. Note that this method
controls loading of all images, including those embedded using the data
URI scheme. Use setBlockNetworkImage(boolean) to control loading only
of images specified using network URI schemes. Note that if the value of this
setting is changed from false to true, all images resources referenced
by content currently displayed by the WebView are loaded automatically.
The default is true.

setOffscreenPreRaster

Sets whether this WebView should raster tiles when it is
offscreen but attached to a window. Turning this on can avoid
rendering artifacts when animating an offscreen WebView on-screen.
Offscreen WebViews in this mode use more memory. The default value is
false.
Please follow these guidelines to limit memory usage:

WebView size should be not be larger than the device screen size.

Limit use of this mode to a small number of WebViews. Use it for
visible WebViews and WebViews about to be animated to visible.

setPluginState

This method was deprecated
in API level 18.
Plugins are not supported in API level
Build.VERSION_CODES.KITKAT or later;
enabling plugins is a no-op.

Tells the WebView to enable, disable, or have plugins 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. The default is
WebSettings.PluginState.OFF.

setSaveFormData

Sets whether the WebView should save form data. In Android O, the
platform has implemented a fully functional Autofill feature to store
form data. Therefore, the Webview form data save feature is disabled.
Note that the feature will continue to be supported on older versions of
Android as before.
This function does not have any effect.

setSupportZoom

Sets whether the WebView should support zooming using its on-screen zoom
controls and gestures. The particular zoom mechanisms that should be used
can be set with setBuiltInZoomControls(boolean). This setting does not
affect zooming performed using the WebView.zoomIn() and
WebView.zoomOut() methods. The default is true.

setUseWideViewPort

Sets whether the WebView should enable support for the "viewport"
HTML meta tag or should use a wide viewport.
When the value of the setting is false, the layout width is always set to the
width of the WebView control in device-independent (CSS) pixels.
When the value is true and the page contains the viewport meta tag, the value
of the width specified in the tag is used. If the page does not contain the tag or
does not provide a width, then a wide viewport will be used.

Parameters

use

boolean: whether to enable support for the viewport meta tag

setUserAgentString

Sets the WebView's user-agent string. If the string is null or empty,
the system default value will be used.
Note that starting from Build.VERSION_CODES.KITKAT Android
version, changing the user-agent while loading a web page causes WebView
to initiate loading once again.