WebSettingsCompat


class WebSettingsCompat


Compatibility version of android.webkit.WebSettings

Summary

Nested types

@Retention(value = RetentionPolicy.CLASS)
@Target(value = [ElementType.METHOD, ElementType.FIELD, ElementType.TYPE])
@RequiresOptIn(level = RequiresOptIn.Level.ERROR)
annotation WebSettingsCompat.ExperimentalBackForwardCache

Denotes that the BackForwardCache API surface is experimental.

@Retention(value = RetentionPolicy.CLASS)
@Target(value = [ElementType.METHOD, ElementType.FIELD, ElementType.TYPE])
@RequiresOptIn(level = RequiresOptIn.Level.ERROR)
annotation WebSettingsCompat.ExperimentalSpeculativeLoading

Denotes that the SpeculativeLoading API surface is experimental.

Constants

const Int

AttributionRegistrationBehavior that allows apps to register app sources and app triggers from WebView.

const Int

AttributionRegistrationBehavior that allows apps to register app sources (sources associated with the app package name) and web triggers (triggers associated with the eTLD+1) from WebView.

const Int

AttributionRegistrationBehavior that disables source and trigger registration from WebView.

const Int

AttributionRegistrationBehavior that allows apps to register web sources and web triggers from WebView.

const Int

This property is deprecated.

refer to setForceDark

const Int

This property is deprecated.

refer to setForceDark

const Int

This property is deprecated.

refer to setForceDark

const Int

This property is deprecated.

refer to setForceDark

const Int

This property is deprecated.

refer to setForceDark

const Int

This property is deprecated.

refer to setForceDark

const Int

Disables all speculative loading tech.

const Int

Enabled Prerender for this WebSettings, See Prerender to learn more.

const Int

The support level that allows WebAuthn requests for the app in which the WebView is embedded.

const Int

The support level that allows apps to make WebAuthn calls for any website.

const Int

The support level that disables WebAuthn requests from WebView.

Public functions

java-static Int
@RequiresFeature(name = WebViewFeature.ATTRIBUTION_REGISTRATION_BEHAVIOR, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
getAttributionRegistrationBehavior(settings: WebSettings)

Read the current behavior for attribution registration.

java-static Boolean
@RequiresFeature(name = WebViewFeature.BACK_FORWARD_CACHE, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
@WebSettingsCompat.ExperimentalBackForwardCache
getBackForwardCacheEnabled(settings: WebSettings)

Get the current status of BackForwardCache for this WebSettings.

java-static Int
@RequiresFeature(name = WebViewFeature.DISABLED_ACTION_MODE_MENU_ITEMS, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
getDisabledActionModeMenuItems(settings: WebSettings)

Gets the action mode menu items that are disabled, expressed in an integer field flag.

java-static Boolean
@RequiresFeature(name = WebViewFeature.ENTERPRISE_AUTHENTICATION_APP_LINK_POLICY, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
getEnterpriseAuthenticationAppLinkPolicyEnabled(settings: WebSettings)

Gets whether EnterpriseAuthenticationAppLinkPolicy is allowed to have any effect on WebView.

java-static Int
@RequiresFeature(name = WebViewFeature.FORCE_DARK, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
getForceDark(settings: WebSettings)

This function is deprecated.

refer to setForceDark

java-static Int
@RequiresFeature(name = WebViewFeature.FORCE_DARK_STRATEGY, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
getForceDarkStrategy(settings: WebSettings)

This function is deprecated.

refer to setForceDark

java-static Boolean
@RequiresFeature(name = WebViewFeature.OFF_SCREEN_PRERASTER, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
getOffscreenPreRaster(settings: WebSettings)

Gets whether this WebView should raster tiles when it is offscreen but attached to a window.

java-static (Mutable)Set<String!>
@RequiresFeature(name = WebViewFeature.REQUESTED_WITH_HEADER_ALLOW_LIST, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
getRequestedWithHeaderOriginAllowList(settings: WebSettings)

Get the currently configured allow-list of origins, which is guaranteed to receive the X-Requested-With HTTP header on requests from the WebView owning the passed WebSettings.

java-static Boolean
@RequiresFeature(name = WebViewFeature.SAFE_BROWSING_ENABLE, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
getSafeBrowsingEnabled(settings: WebSettings)

Gets whether Safe Browsing is enabled.

java-static Int
@RequiresFeature(name = WebViewFeature.SPECULATIVE_LOADING, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
@WebSettingsCompat.ExperimentalSpeculativeLoading
getSpeculativeLoadingStatus(settings: WebSettings)

Gets speculative loading status for this WebSettings.

java-static UserAgentMetadata
@RequiresFeature(name = WebViewFeature.USER_AGENT_METADATA, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
getUserAgentMetadata(settings: WebSettings)

Get the WebView's user-agent metadata which used to generate user-agent client hints.

java-static Int
@RequiresFeature(name = WebViewFeature.WEB_AUTHENTICATION, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
getWebAuthenticationSupport(settings: WebSettings)

Returns the support level for the given WebSettings

java-static WebViewMediaIntegrityApiStatusConfig
@RequiresFeature(name = WebViewFeature.WEBVIEW_MEDIA_INTEGRITY_API_STATUS, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
getWebViewMediaIntegrityApiStatus(settings: WebSettings)

Returns the WebViewMediaIntegrityApiStatusConfig currently in use.

java-static Boolean
@RequiresFeature(name = WebViewFeature.ALGORITHMIC_DARKENING, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
isAlgorithmicDarkeningAllowed(settings: WebSettings)

Get if algorithmic darkening is allowed or not for this WebView.

java-static Unit
@RequiresFeature(name = WebViewFeature.ALGORITHMIC_DARKENING, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
setAlgorithmicDarkeningAllowed(settings: WebSettings, allow: Boolean)

Control whether algorithmic darkening is allowed.

java-static Unit
@RequiresFeature(name = WebViewFeature.ATTRIBUTION_REGISTRATION_BEHAVIOR, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
setAttributionRegistrationBehavior(settings: WebSettings, behavior: Int)

Control how WebView interacts with Attribution Reporting.

java-static Unit
@RequiresFeature(name = WebViewFeature.BACK_FORWARD_CACHE, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
@WebSettingsCompat.ExperimentalBackForwardCache
setBackForwardCacheEnabled(
    settings: WebSettings,
    backForwardCacheEnabled: Boolean
)

Enables BackForwardCache for the given WebSettings.

java-static Unit
@RequiresFeature(name = WebViewFeature.DISABLED_ACTION_MODE_MENU_ITEMS, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
setDisabledActionModeMenuItems(settings: WebSettings, menuItems: Int)

Disables the action mode menu items according to menuItems flag.

java-static Unit
@RequiresFeature(name = WebViewFeature.ENTERPRISE_AUTHENTICATION_APP_LINK_POLICY, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
setEnterpriseAuthenticationAppLinkPolicyEnabled(
    settings: WebSettings,
    enabled: Boolean
)

Sets whether EnterpriseAuthenticationAppLinkPolicy if set by admin is allowed to have any effect on WebView.

java-static Unit
@RequiresFeature(name = WebViewFeature.FORCE_DARK, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
setForceDark(settings: WebSettings, forceDarkMode: Int)

This function is deprecated.

The "force dark" model previously implemented by WebView was complex and didn't interoperate well with current Web standards for prefers-color-scheme and color-scheme.

java-static Unit
@RequiresFeature(name = WebViewFeature.FORCE_DARK_STRATEGY, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
setForceDarkStrategy(settings: WebSettings, forceDarkBehavior: Int)

This function is deprecated.

refer to setForceDark

java-static Unit
@RequiresFeature(name = WebViewFeature.OFF_SCREEN_PRERASTER, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
setOffscreenPreRaster(settings: WebSettings, enabled: Boolean)

Sets whether this WebView should raster tiles when it is offscreen but attached to a window.

java-static Unit
@RequiresFeature(name = WebViewFeature.REQUESTED_WITH_HEADER_ALLOW_LIST, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
setRequestedWithHeaderOriginAllowList(
    settings: WebSettings,
    allowList: (Mutable)Set<String!>
)

Set an allow-list of origins to receive the X-Requested-With HTTP header from the WebView owning the passed WebSettings.

java-static Unit
@RequiresFeature(name = WebViewFeature.SAFE_BROWSING_ENABLE, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
setSafeBrowsingEnabled(settings: WebSettings, enabled: Boolean)

Sets whether Safe Browsing is enabled.

java-static Unit
@RequiresFeature(name = WebViewFeature.SPECULATIVE_LOADING, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
@WebSettingsCompat.ExperimentalSpeculativeLoading
setSpeculativeLoadingStatus(
    settings: WebSettings,
    speculativeLoadingStatus: Int
)

Sets whether speculative loading status for this WebSettings.

java-static Unit
@RequiresFeature(name = WebViewFeature.USER_AGENT_METADATA, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
setUserAgentMetadata(settings: WebSettings, metadata: UserAgentMetadata)

Sets the WebView's user-agent metadata to generate user-agent client hints.

java-static Unit
@RequiresFeature(name = WebViewFeature.WEB_AUTHENTICATION, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
setWebAuthenticationSupport(settings: WebSettings, support: Int)

Sets the support level for the given WebSettings.

java-static Unit
@RequiresFeature(name = WebViewFeature.WEBVIEW_MEDIA_INTEGRITY_API_STATUS, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
setWebViewMediaIntegrityApiStatus(
    settings: WebSettings,
    permissionConfig: WebViewMediaIntegrityApiStatusConfig
)

Sets permissions provided through WebViewMediaIntegrityApiStatusConfig for using the WebView Integrity API.

Constants

ATTRIBUTION_BEHAVIOR_APP_SOURCE_AND_APP_TRIGGER

Added in 1.9.0
const val ATTRIBUTION_BEHAVIOR_APP_SOURCE_AND_APP_TRIGGER = 3: Int

AttributionRegistrationBehavior that allows apps to register app sources and app triggers from WebView.

ATTRIBUTION_BEHAVIOR_APP_SOURCE_AND_WEB_TRIGGER

Added in 1.9.0
const val ATTRIBUTION_BEHAVIOR_APP_SOURCE_AND_WEB_TRIGGER = 1: Int

AttributionRegistrationBehavior that allows apps to register app sources (sources associated with the app package name) and web triggers (triggers associated with the eTLD+1) from WebView.

This is the default behavior.

ATTRIBUTION_BEHAVIOR_DISABLED

Added in 1.9.0
const val ATTRIBUTION_BEHAVIOR_DISABLED = 0: Int

AttributionRegistrationBehavior that disables source and trigger registration from WebView.

Note that the initial network call to the Attribution Source or Trigger URIs may still happen depending on the installed version of WebView, but any response is discarded and nothing will be stored on the device.

ATTRIBUTION_BEHAVIOR_WEB_SOURCE_AND_WEB_TRIGGER

Added in 1.9.0
const val ATTRIBUTION_BEHAVIOR_WEB_SOURCE_AND_WEB_TRIGGER = 2: Int

AttributionRegistrationBehavior that allows apps to register web sources and web triggers from WebView.

This option should only be used after applying to use web sources.

DARK_STRATEGY_PREFER_WEB_THEME_OVER_USER_AGENT_DARKENING

Added in 1.3.0
Deprecated in 1.5.0
const val DARK_STRATEGY_PREFER_WEB_THEME_OVER_USER_AGENT_DARKENING = 2: Int

In this mode WebView content will be darkened by a user agent unless web page supports dark theme. WebView determines whether web pages supports dark theme by the presence of color-scheme metadata containing "dark" value. For example, <meta name="color-scheme" content="dark light">. If the metadata is not presented WebView content will be darkened by a user agent and prefers-color-scheme media query will evaluate to light.

See specification for more information.

DARK_STRATEGY_USER_AGENT_DARKENING_ONLY

Added in 1.3.0
Deprecated in 1.5.0
const val DARK_STRATEGY_USER_AGENT_DARKENING_ONLY = 0: Int

In this mode WebView content will be darkened by a user agent and it will ignore the web page's dark theme if it exists. To avoid mixing two different darkening strategies, the prefers-color-scheme media query will evaluate to light.

See specification for more information.

DARK_STRATEGY_WEB_THEME_DARKENING_ONLY

Added in 1.3.0
Deprecated in 1.5.0
const val DARK_STRATEGY_WEB_THEME_DARKENING_ONLY = 1: Int

In this mode WebView content will always be darkened using dark theme provided by web page. If web page does not provide dark theme support WebView content will be rendered with a default theme.

See specification for more information.

FORCE_DARK_AUTO

Added in 1.2.0
Deprecated in 1.5.0
const val FORCE_DARK_AUTO = 1: Int

Enable force dark dependent on the state of the WebView parent view. If the WebView parent view is being automatically force darkened (@see android.view.View#setForceDarkAllowed), then WebView content will be rendered so as to emulate a dark theme. WebViews that are not attached to the view hierarchy will not be inverted.

If your app uses a dark theme, WebView will not be inverted. Similarly, if your app's theme inherits from a DayNight theme, WebView will not be inverted. In either of these cases, you should control the mode manually with FORCE_DARK_ON or FORCE_DARK_OFF.

See Force Dark documentation for more information.

See also
setForceDark

FORCE_DARK_OFF

Added in 1.2.0
Deprecated in 1.5.0
const val FORCE_DARK_OFF = 0: Int

Disable force dark, irrespective of the force dark mode of the WebView parent. In this mode, WebView content will always be rendered as-is, regardless of whether native views are being automatically darkened.

See also
setForceDark

FORCE_DARK_ON

Added in 1.2.0
Deprecated in 1.5.0
const val FORCE_DARK_ON = 2: Int

Unconditionally enable force dark. In this mode WebView content will always be rendered so as to emulate a dark theme.

See also
setForceDark

SPECULATIVE_LOADING_DISABLED

Added in 1.12.0
@WebSettingsCompat.ExperimentalSpeculativeLoading
const val SPECULATIVE_LOADING_DISABLED = 0: Int

Disables all speculative loading tech. See this to learn more.

This is the default behavior.

SPECULATIVE_LOADING_PRERENDER_ENABLED

Added in 1.12.0
@WebSettingsCompat.ExperimentalSpeculativeLoading
const val SPECULATIVE_LOADING_PRERENDER_ENABLED = 1: Int

Enabled Prerender for this WebSettings, See Prerender to learn more.

WEB_AUTHENTICATION_SUPPORT_FOR_APP

Added in 1.12.0
const val WEB_AUTHENTICATION_SUPPORT_FOR_APP = 1: Int

The support level that allows WebAuthn requests for the app in which the WebView is embedded. See Digital Asset Links to learn how to associate an app with a website.

WEB_AUTHENTICATION_SUPPORT_FOR_BROWSER

Added in 1.12.0
const val WEB_AUTHENTICATION_SUPPORT_FOR_BROWSER = 2: Int

The support level that allows apps to make WebAuthn calls for any website. See Privileged apps to learn how to make WebAuthn calls for any website.

WEB_AUTHENTICATION_SUPPORT_NONE

Added in 1.12.0
const val WEB_AUTHENTICATION_SUPPORT_NONE = 0: Int

The support level that disables WebAuthn requests from WebView.

This is the default behavior.

Public functions

getAttributionRegistrationBehavior

Added in 1.9.0
@RequiresFeature(name = WebViewFeature.ATTRIBUTION_REGISTRATION_BEHAVIOR, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
java-static fun getAttributionRegistrationBehavior(settings: WebSettings): Int

Read the current behavior for attribution registration.

Parameters
settings: WebSettings

Settings retrieved from getSettings.

getBackForwardCacheEnabled

Added in 1.12.0
@RequiresFeature(name = WebViewFeature.BACK_FORWARD_CACHE, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
@WebSettingsCompat.ExperimentalBackForwardCache
java-static fun getBackForwardCacheEnabled(settings: WebSettings): Boolean

Get the current status of BackForwardCache for this WebSettings. This API is experimental, it may change in the future without notice. Please use accordingly.

This method should only be called if isFeatureSupported returns true for BACK_FORWARD_CACHE.

Parameters
settings: WebSettings

Settings retrieved from getSettings.

Returns
Boolean

Whether BackForwardCache is enabled or not.

getDisabledActionModeMenuItems

Added in 1.1.0
@RequiresFeature(name = WebViewFeature.DISABLED_ACTION_MODE_MENU_ITEMS, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
java-static fun getDisabledActionModeMenuItems(settings: WebSettings): Int

Gets the action mode menu items that are disabled, expressed in an integer field flag. The default value is MENU_ITEM_NONE

This method should only be called if isFeatureSupported returns true for DISABLED_ACTION_MODE_MENU_ITEMS.

Returns
Int

all the disabled menu item flags combined with bitwise OR.

getEnterpriseAuthenticationAppLinkPolicyEnabled

Added in 1.6.0
@RequiresFeature(name = WebViewFeature.ENTERPRISE_AUTHENTICATION_APP_LINK_POLICY, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
java-static fun getEnterpriseAuthenticationAppLinkPolicyEnabled(settings: WebSettings): Boolean

Gets whether EnterpriseAuthenticationAppLinkPolicy is allowed to have any effect on WebView.

See this for more information on EnterpriseAuthenticationAppLinkPolicy.

This method should only be called if isFeatureSupported returns true for ENTERPRISE_AUTHENTICATION_APP_LINK_POLICY.

Returns
Boolean

true if EnterpriseAuthenticationAppLinkPolicy is enabled and false otherwise.

getForceDark

Added in 1.2.0
Deprecated in 1.5.0
@RequiresFeature(name = WebViewFeature.FORCE_DARK, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
java-static fun getForceDark(settings: WebSettings): Int

Get the force dark mode for this WebView.

The default force dark mode is FORCE_DARK_AUTO.

This method should only be called if isFeatureSupported returns true for FORCE_DARK.

Returns
Int

the currently set force dark mode.

See also
setForceDark

getForceDarkStrategy

Added in 1.3.0
Deprecated in 1.5.0
@RequiresFeature(name = WebViewFeature.FORCE_DARK_STRATEGY, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
java-static fun getForceDarkStrategy(settings: WebSettings): Int

Get how content is darkened for this WebView.

The default force dark strategy is DARK_STRATEGY_PREFER_WEB_THEME_OVER_USER_AGENT_DARKENING

This method should only be called if isFeatureSupported returns true for FORCE_DARK_STRATEGY.

Returns
Int

the currently set force dark strategy.

getOffscreenPreRaster

Added in 1.1.0
@RequiresFeature(name = WebViewFeature.OFF_SCREEN_PRERASTER, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
java-static fun getOffscreenPreRaster(settings: WebSettings): Boolean

Gets whether this WebView should raster tiles when it is offscreen but attached to a window.

This method should only be called if isFeatureSupported returns true for OFF_SCREEN_PRERASTER.

Returns
Boolean

true if this WebView will raster tiles when it is offscreen but attached to a window.

getRequestedWithHeaderOriginAllowList

Added in 1.6.0
@RequiresFeature(name = WebViewFeature.REQUESTED_WITH_HEADER_ALLOW_LIST, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
java-static fun getRequestedWithHeaderOriginAllowList(settings: WebSettings): (Mutable)Set<String!>

Get the currently configured allow-list of origins, which is guaranteed to receive the X-Requested-With HTTP header on requests from the WebView owning the passed WebSettings.

Any origin not on this allow-list may not receive the header, depending on the current installed WebView provider.

The format of the strings in the allow-list follows the origin rules of addWebMessageListener.

Parameters
settings: WebSettings

Settings retrieved from getSettings.

Returns
(Mutable)Set<String!>

The configured set of allow-listed origins.

getSafeBrowsingEnabled

Added in 1.1.0
@RequiresFeature(name = WebViewFeature.SAFE_BROWSING_ENABLE, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
java-static fun getSafeBrowsingEnabled(settings: WebSettings): Boolean

Gets whether Safe Browsing is enabled. See setSafeBrowsingEnabled.

This method should only be called if isFeatureSupported returns true for SAFE_BROWSING_ENABLE.

Returns
Boolean

true if Safe Browsing is enabled and false otherwise.

getSpeculativeLoadingStatus

Added in 1.12.0
@RequiresFeature(name = WebViewFeature.SPECULATIVE_LOADING, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
@WebSettingsCompat.ExperimentalSpeculativeLoading
java-static fun getSpeculativeLoadingStatus(settings: WebSettings): Int

Gets speculative loading status for this WebSettings. This API is experimental, it may change in the future without notice. Please use accordingly.

This method should only be called if isFeatureSupported returns true for SPECULATIVE_LOADING.

Parameters
settings: WebSettings

Settings retrieved from getSettings.

Returns
Int

The current status for the speculative loading. It will to be one of SpeculativeLoadingStatus.

getUserAgentMetadata

Added in 1.9.0
@RequiresFeature(name = WebViewFeature.USER_AGENT_METADATA, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
java-static fun getUserAgentMetadata(settings: WebSettings): UserAgentMetadata

Get the WebView's user-agent metadata which used to generate user-agent client hints.

See this for more information about User-Agent Client Hints.

This method should only be called if isFeatureSupported returns true for USER_AGENT_METADATA.

Parameters
settings: WebSettings

Settings retrieved from getSettings.

getWebAuthenticationSupport

Added in 1.12.0
@RequiresFeature(name = WebViewFeature.WEB_AUTHENTICATION, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
java-static fun getWebAuthenticationSupport(settings: WebSettings): Int

Returns the support level for the given WebSettings

This method should only be called if isFeatureSupported returns true for WEB_AUTHENTICATION.

Parameters
settings: WebSettings

Settings retrieved from getSettings.

Returns
Int

the current support level.

getWebViewMediaIntegrityApiStatus

Added in 1.10.0
@RequiresFeature(name = WebViewFeature.WEBVIEW_MEDIA_INTEGRITY_API_STATUS, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
java-static fun getWebViewMediaIntegrityApiStatus(settings: WebSettings): WebViewMediaIntegrityApiStatusConfig

Returns the WebViewMediaIntegrityApiStatusConfig currently in use.

isAlgorithmicDarkeningAllowed

Added in 1.5.0
@RequiresFeature(name = WebViewFeature.ALGORITHMIC_DARKENING, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
java-static fun isAlgorithmicDarkeningAllowed(settings: WebSettings): Boolean

Get if algorithmic darkening is allowed or not for this WebView. The default is false.

Returns
Boolean

if the algorithmic darkening is allowed or not.

setAlgorithmicDarkeningAllowed

Added in 1.5.0
@RequiresFeature(name = WebViewFeature.ALGORITHMIC_DARKENING, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
java-static fun setAlgorithmicDarkeningAllowed(settings: WebSettings, allow: Boolean): Unit

Control whether algorithmic darkening is allowed.

Note: This API and the behaviour described only apply to apps with targetSdkVersionTIRAMISU.

WebView always sets the media query prefers-color-scheme according to the app's theme attribute isLightTheme, i.e. prefers-color-scheme is light if isLightTheme is true or not specified, otherwise it is dark. This means that the web content's light or dark style will be applied automatically to match the app's theme if the content supports it.

Algorithmic darkening is disallowed by default.

If the app's theme is dark and it allows algorithmic darkening, WebView will attempt to darken web content using an algorithm, if the content doesn't define its own dark styles and doesn't explicitly disable darkening.

If Android is applying Force Dark to WebView then WebView will ignore the value of this setting and behave as if it were set to true.

The deprecated setForceDark and related API are no-ops in apps with targetSdkVersionTIRAMISU, but they still apply to apps with targetSdkVersion<TIRAMISU.

The below table summarizes how APIs work with different apps.

App Web content which uses prefers-color-scheme Web content which does not use prefers-color-scheme
App with isLightTheme True or not set Renders with the light theme defined by the content author. Renders with the default styling defined by the content author.
App with Android forceDark in effect Renders with the dark theme defined by the content author. Renders with the styling modified to dark colors by an algorithm if allowed by the content author.
App with isLightTheme False, targetSdkVersion<TIRAMISU, and has FORCE_DARK_AUTO Renders with the dark theme defined by the content author. Renders with the default styling defined by the content author.
App with isLightTheme False, targetSdkVersionTIRAMISU, and setAlgorithmicDarkening(false) Renders with the dark theme defined by the content author. Renders with the default styling defined by the content author.
App with isLightTheme False, targetSdkVersionTIRAMISU, and setAlgorithmicDarkening(true) Renders with the dark theme defined by the content author. Renders with the styling modified to dark colors by an algorithm if allowed by the content author.

To check if WebViewFeature.ALGORITHMIC_DARKENING is supported, isFeatureSupported should be called after WebView is created.

Parameters
settings: WebSettings

The WebSettings object to update.

allow: Boolean

allow algorithmic darkening or not.

setAttributionRegistrationBehavior

Added in 1.9.0
@RequiresFeature(name = WebViewFeature.ATTRIBUTION_REGISTRATION_BEHAVIOR, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
java-static fun setAttributionRegistrationBehavior(settings: WebSettings, behavior: Int): Unit

Control how WebView interacts with Attribution Reporting.

WebView supports Attribution Reporting across apps and web pages by enabling the web content to register sources and triggers.

By default, attribution sources will be registered as attributed to the app, while triggers are attributed to the loaded web content (ATTRIBUTION_BEHAVIOR_APP_SOURCE_AND_WEB_TRIGGER). Apps should only need to change this default if they have a specific semantic for how they use WebView. In particular, in-app browsers should follow the steps to apply to the allowlist for registering web sources and then set the ATTRIBUTION_BEHAVIOR_WEB_SOURCE_AND_WEB_TRIGGER behavior.

Parameters
settings: WebSettings

Settings retrieved from getSettings.

behavior: Int

New behavior to use.

setBackForwardCacheEnabled

Added in 1.12.0
@RequiresFeature(name = WebViewFeature.BACK_FORWARD_CACHE, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
@WebSettingsCompat.ExperimentalBackForwardCache
java-static fun setBackForwardCacheEnabled(
    settings: WebSettings,
    backForwardCacheEnabled: Boolean
): Unit

Enables BackForwardCache for the given WebSettings. This API is experimental, it may change in the future without notice. Please use accordingly.

This method should only be called if isFeatureSupported returns true for BACK_FORWARD_CACHE.

Parameters
settings: WebSettings

Settings retrieved from getSettings.

backForwardCacheEnabled: Boolean

whether BackForwardCache should be enabled for this WebSettings

setDisabledActionModeMenuItems

Added in 1.1.0
@RequiresFeature(name = WebViewFeature.DISABLED_ACTION_MODE_MENU_ITEMS, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
java-static fun setDisabledActionModeMenuItems(settings: WebSettings, menuItems: Int): Unit

Disables the action mode menu items according to menuItems flag.

This method should only be called if isFeatureSupported returns true for DISABLED_ACTION_MODE_MENU_ITEMS.

Parameters
settings: WebSettings

The WebSettings object to update.

menuItems: Int

an integer field flag for the menu items to be disabled.

setEnterpriseAuthenticationAppLinkPolicyEnabled

Added in 1.6.0
@RequiresFeature(name = WebViewFeature.ENTERPRISE_AUTHENTICATION_APP_LINK_POLICY, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
java-static fun setEnterpriseAuthenticationAppLinkPolicyEnabled(
    settings: WebSettings,
    enabled: Boolean
): Unit

Sets whether EnterpriseAuthenticationAppLinkPolicy if set by admin is allowed to have any effect on WebView.

EnterpriseAuthenticationAppLinkPolicy in WebView allows admins to specify authentication urls. When WebView is redirected to authentication url, and an app on the device has registered as the default handler for the url, that app is launched.

EnterpriseAuthenticationAppLinkPolicy is enabled by default.

See this for more information on EnterpriseAuthenticationAppLinkPolicy.

This method should only be called if isFeatureSupported returns true for ENTERPRISE_AUTHENTICATION_APP_LINK_POLICY.

Parameters
settings: WebSettings

The WebSettings object to update.

enabled: Boolean

Whether EnterpriseAuthenticationAppLinkPolicy should be enabled.

setForceDark

Added in 1.2.0
Deprecated in 1.5.0
@RequiresFeature(name = WebViewFeature.FORCE_DARK, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
java-static fun setForceDark(settings: WebSettings, forceDarkMode: Int): Unit

Set the force dark mode for this WebView.

This method should only be called if isFeatureSupported returns true for FORCE_DARK.

If equals to FORCE_DARK_ON then setForceDarkStrategy is used to specify darkening strategy.

Parameters
forceDarkMode: Int

the force dark mode to set.

See also
getForceDark

setForceDarkStrategy

Added in 1.3.0
Deprecated in 1.5.0
@RequiresFeature(name = WebViewFeature.FORCE_DARK_STRATEGY, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
java-static fun setForceDarkStrategy(settings: WebSettings, forceDarkBehavior: Int): Unit

Set how WebView content should be darkened.

This method should only be called if isFeatureSupported returns true for FORCE_DARK_STRATEGY.

The specified strategy is only used if force dark mode is on. See setForceDark.

Parameters
forceDarkBehavior: Int

the force dark strategy to set.

setOffscreenPreRaster

Added in 1.1.0
@RequiresFeature(name = WebViewFeature.OFF_SCREEN_PRERASTER, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
java-static fun setOffscreenPreRaster(settings: WebSettings, enabled: Boolean): Unit

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.

This method should only be called if isFeatureSupported returns true for OFF_SCREEN_PRERASTER.

setRequestedWithHeaderOriginAllowList

Added in 1.6.0
@RequiresFeature(name = WebViewFeature.REQUESTED_WITH_HEADER_ALLOW_LIST, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
java-static fun setRequestedWithHeaderOriginAllowList(
    settings: WebSettings,
    allowList: (Mutable)Set<String!>
): Unit

Set an allow-list of origins to receive the X-Requested-With HTTP header from the WebView owning the passed WebSettings.

Historically, this header was sent on all requests from WebView, containing the app package name of the embedding app. Depending on the version of installed WebView, this may no longer be the case, as the header was deprecated in late 2022, and its use discontinued.

Apps can use this method to restore the legacy behavior for servers that still rely on the deprecated header, but it should not be used to identify the WebView to first-party servers under the control of the app developer.

The format of the strings in the allow-list follows the origin rules of addWebMessageListener.

Parameters
settings: WebSettings

Settings retrieved from getSettings.

allowList: (Mutable)Set<String!>

Set of origins to allow-list.

Throws
java.lang.IllegalArgumentException

if the allow-list contains a malformed origin.

setSafeBrowsingEnabled

Added in 1.1.0
@RequiresFeature(name = WebViewFeature.SAFE_BROWSING_ENABLE, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
java-static fun setSafeBrowsingEnabled(settings: WebSettings, enabled: Boolean): Unit

Sets whether Safe Browsing is enabled. Safe Browsing allows WebView to protect against malware and phishing attacks by verifying the links.

Safe Browsing can be disabled for all WebViews using a manifest tag (read general Safe Browsing info). The manifest tag has a lower precedence than this API.

Safe Browsing is enabled by default for devices which support it.

This method should only be called if isFeatureSupported returns true for SAFE_BROWSING_ENABLE.

Parameters
settings: WebSettings

The WebSettings object to update.

enabled: Boolean

Whether Safe Browsing is enabled.

setSpeculativeLoadingStatus

Added in 1.12.0
@RequiresFeature(name = WebViewFeature.SPECULATIVE_LOADING, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
@WebSettingsCompat.ExperimentalSpeculativeLoading
java-static fun setSpeculativeLoadingStatus(
    settings: WebSettings,
    speculativeLoadingStatus: Int
): Unit

Sets whether speculative loading status for this WebSettings. This API is experimental, it may change in the future without notice. Please use accordingly.

This method should only be called if isFeatureSupported returns true for SPECULATIVE_LOADING.

Parameters
settings: WebSettings

Settings retrieved from getSettings.

speculativeLoadingStatus: Int

The new status for the speculative loading. It will to be one of SpeculativeLoadingStatus

setUserAgentMetadata

Added in 1.9.0
@RequiresFeature(name = WebViewFeature.USER_AGENT_METADATA, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
java-static fun setUserAgentMetadata(settings: WebSettings, metadata: UserAgentMetadata): Unit

Sets the WebView's user-agent metadata to generate user-agent client hints.

UserAgentMetadata in WebView is used to populate user-agent client hints, they can provide the client’s branding and version information, the underlying operating system’s branding and major version, as well as details about the underlying device.

The user-agent string can be set with setUserAgentString, here are the details on how this API interacts it to generate user-agent client hints.

If the UserAgentMetadata is null and the overridden user-agent contains the system default user-agent, the system default value will be used.

If the UserAgentMetadata is null but the overridden user-agent doesn't contain the system default user-agent, only the low-entry user-agent client hints will be generated.

See this for more information about User-Agent Client Hints.

This method should only be called if isFeatureSupported returns true for USER_AGENT_METADATA.

Parameters
settings: WebSettings

Settings retrieved from getSettings.

metadata: UserAgentMetadata

the WebView's user-agent metadata.

setWebAuthenticationSupport

Added in 1.12.0
@RequiresFeature(name = WebViewFeature.WEB_AUTHENTICATION, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
java-static fun setWebAuthenticationSupport(settings: WebSettings, support: Int): Unit

Sets the support level for the given WebSettings.

This method should only be called if isFeatureSupported returns true for WEB_AUTHENTICATION.

Parameters
settings: WebSettings

Settings retrieved from getSettings.

support: Int

The new support level which this WebView will use.

setWebViewMediaIntegrityApiStatus

Added in 1.10.0
@RequiresFeature(name = WebViewFeature.WEBVIEW_MEDIA_INTEGRITY_API_STATUS, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
java-static fun setWebViewMediaIntegrityApiStatus(
    settings: WebSettings,
    permissionConfig: WebViewMediaIntegrityApiStatusConfig
): Unit

Sets permissions provided through WebViewMediaIntegrityApiStatusConfig for using the WebView Integrity API.