WebSettingsCompat

public class WebSettingsCompat


Compatibility version of android.webkit.WebSettings

Summary

Constants

static final int

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

static final 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.

static final int

AttributionRegistrationBehavior that disables source and trigger registration from WebView.

static final int

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

static final int

This field is deprecated.

refer to setForceDark

static final int

This field is deprecated.

refer to setForceDark

static final int

This field is deprecated.

refer to setForceDark

static final int

This field is deprecated.

refer to setForceDark

static final int

This field is deprecated.

refer to setForceDark

static final int

This field is deprecated.

refer to setForceDark

Public methods

static int
@RequiresFeature(name = WebViewFeature.ATTRIBUTION_REGISTRATION_BEHAVIOR, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
getAttributionRegistrationBehavior(@NonNull WebSettings settings)

Read the current behavior for attribution registration.

static int
@RequiresFeature(name = WebViewFeature.DISABLED_ACTION_MODE_MENU_ITEMS, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
getDisabledActionModeMenuItems(@NonNull WebSettings settings)

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

static boolean
@RequiresFeature(name = WebViewFeature.ENTERPRISE_AUTHENTICATION_APP_LINK_POLICY, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
getEnterpriseAuthenticationAppLinkPolicyEnabled(
    @NonNull WebSettings settings
)

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

static int
@RequiresFeature(name = WebViewFeature.FORCE_DARK, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
getForceDark(@NonNull WebSettings settings)

This method is deprecated.

refer to setForceDark

static int
@RequiresFeature(name = WebViewFeature.FORCE_DARK_STRATEGY, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
getForceDarkStrategy(@NonNull WebSettings settings)

This method is deprecated.

refer to setForceDark

static boolean
@RequiresFeature(name = WebViewFeature.OFF_SCREEN_PRERASTER, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
getOffscreenPreRaster(@NonNull WebSettings settings)

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

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

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.

static boolean
@RequiresFeature(name = WebViewFeature.SAFE_BROWSING_ENABLE, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
getSafeBrowsingEnabled(@NonNull WebSettings settings)

Gets whether Safe Browsing is enabled.

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

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

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

Returns the WebViewMediaIntegrityApiStatusConfig currently in use.

static boolean
@RequiresFeature(name = WebViewFeature.ALGORITHMIC_DARKENING, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
isAlgorithmicDarkeningAllowed(@NonNull WebSettings settings)

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

static void
@RequiresFeature(name = WebViewFeature.ALGORITHMIC_DARKENING, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
setAlgorithmicDarkeningAllowed(
    @NonNull WebSettings settings,
    boolean allow
)

Control whether algorithmic darkening is allowed.

static void
@RequiresFeature(name = WebViewFeature.ATTRIBUTION_REGISTRATION_BEHAVIOR, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
setAttributionRegistrationBehavior(
    @NonNull WebSettings settings,
    int behavior
)

Control how WebView interacts with Attribution Reporting.

static void
@RequiresFeature(name = WebViewFeature.DISABLED_ACTION_MODE_MENU_ITEMS, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
setDisabledActionModeMenuItems(
    @NonNull WebSettings settings,
    int menuItems
)

Disables the action mode menu items according to menuItems flag.

static void
@RequiresFeature(name = WebViewFeature.ENTERPRISE_AUTHENTICATION_APP_LINK_POLICY, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
setEnterpriseAuthenticationAppLinkPolicyEnabled(
    @NonNull WebSettings settings,
    boolean enabled
)

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

static void
@RequiresFeature(name = WebViewFeature.FORCE_DARK, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
setForceDark(@NonNull WebSettings settings, int forceDarkMode)

This method 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.

static void
@RequiresFeature(name = WebViewFeature.FORCE_DARK_STRATEGY, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
setForceDarkStrategy(
    @NonNull WebSettings settings,
    int forceDarkBehavior
)

This method is deprecated.

refer to setForceDark

static void
@RequiresFeature(name = WebViewFeature.OFF_SCREEN_PRERASTER, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
setOffscreenPreRaster(@NonNull WebSettings settings, boolean enabled)

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

static void
@RequiresFeature(name = WebViewFeature.REQUESTED_WITH_HEADER_ALLOW_LIST, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
setRequestedWithHeaderOriginAllowList(
    @NonNull WebSettings settings,
    @NonNull Set<String> allowList
)

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

static void
@RequiresFeature(name = WebViewFeature.SAFE_BROWSING_ENABLE, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
setSafeBrowsingEnabled(@NonNull WebSettings settings, boolean enabled)

Sets whether Safe Browsing is enabled.

static void
@RequiresFeature(name = WebViewFeature.USER_AGENT_METADATA, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
setUserAgentMetadata(
    @NonNull WebSettings settings,
    @NonNull UserAgentMetadata metadata
)

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

static void
@RequiresFeature(name = WebViewFeature.WEBVIEW_MEDIA_INTEGRITY_API_STATUS, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
setWebViewMediaIntegrityApiStatus(
    @NonNull WebSettings settings,
    @NonNull WebViewMediaIntegrityApiStatusConfig permissionConfig
)

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

Constants

ATTRIBUTION_BEHAVIOR_APP_SOURCE_AND_APP_TRIGGER

Added in 1.9.0
public static final int ATTRIBUTION_BEHAVIOR_APP_SOURCE_AND_APP_TRIGGER = 3

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
public static final int ATTRIBUTION_BEHAVIOR_APP_SOURCE_AND_WEB_TRIGGER = 1

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
public static final int ATTRIBUTION_BEHAVIOR_DISABLED = 0

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
public static final int ATTRIBUTION_BEHAVIOR_WEB_SOURCE_AND_WEB_TRIGGER = 2

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
public static final int DARK_STRATEGY_PREFER_WEB_THEME_OVER_USER_AGENT_DARKENING = 2

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
public static final int DARK_STRATEGY_USER_AGENT_DARKENING_ONLY = 0

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
public static final int DARK_STRATEGY_WEB_THEME_DARKENING_ONLY = 1

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
public static final int FORCE_DARK_AUTO = 1

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
public static final int FORCE_DARK_OFF = 0

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
public static final int FORCE_DARK_ON = 2

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

See also
setForceDark

Public methods

getAttributionRegistrationBehavior

Added in 1.9.0
@RequiresFeature(name = WebViewFeature.ATTRIBUTION_REGISTRATION_BEHAVIOR, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
public static int getAttributionRegistrationBehavior(@NonNull WebSettings settings)

Read the current behavior for attribution registration.

Parameters
@NonNull WebSettings settings

Settings retrieved from getSettings.

getDisabledActionModeMenuItems

Added in 1.1.0
@RequiresFeature(name = WebViewFeature.DISABLED_ACTION_MODE_MENU_ITEMS, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
public static int getDisabledActionModeMenuItems(@NonNull WebSettings settings)

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")
public static boolean getEnterpriseAuthenticationAppLinkPolicyEnabled(
    @NonNull WebSettings settings
)

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")
public static int getForceDark(@NonNull WebSettings settings)

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")
public static int getForceDarkStrategy(@NonNull WebSettings settings)

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")
public static boolean getOffscreenPreRaster(@NonNull WebSettings settings)

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")
public static @NonNull Set<StringgetRequestedWithHeaderOriginAllowList(@NonNull WebSettings settings)

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
@NonNull WebSettings settings

Settings retrieved from getSettings.

Returns
@NonNull 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")
public static boolean getSafeBrowsingEnabled(@NonNull WebSettings settings)

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.

getUserAgentMetadata

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

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
@NonNull WebSettings settings

Settings retrieved from getSettings.

getWebViewMediaIntegrityApiStatus

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

Returns the WebViewMediaIntegrityApiStatusConfig currently in use.

isAlgorithmicDarkeningAllowed

Added in 1.5.0
@RequiresFeature(name = WebViewFeature.ALGORITHMIC_DARKENING, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
public static boolean isAlgorithmicDarkeningAllowed(@NonNull WebSettings settings)

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")
public static void setAlgorithmicDarkeningAllowed(
    @NonNull WebSettings settings,
    boolean allow
)

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
boolean allow

allow algorithmic darkening or not.

setAttributionRegistrationBehavior

Added in 1.9.0
@RequiresFeature(name = WebViewFeature.ATTRIBUTION_REGISTRATION_BEHAVIOR, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
public static void setAttributionRegistrationBehavior(
    @NonNull WebSettings settings,
    int behavior
)

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
@NonNull WebSettings settings

Settings retrieved from getSettings.

int behavior

New behavior to use.

setDisabledActionModeMenuItems

Added in 1.1.0
@RequiresFeature(name = WebViewFeature.DISABLED_ACTION_MODE_MENU_ITEMS, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
public static void setDisabledActionModeMenuItems(
    @NonNull WebSettings settings,
    int menuItems
)

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
int menuItems

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")
public static void setEnterpriseAuthenticationAppLinkPolicyEnabled(
    @NonNull WebSettings settings,
    boolean enabled
)

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
boolean enabled

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")
public static void setForceDark(@NonNull WebSettings settings, int forceDarkMode)

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
int forceDarkMode

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")
public static void setForceDarkStrategy(
    @NonNull WebSettings settings,
    int forceDarkBehavior
)

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
int forceDarkBehavior

the force dark strategy to set.

setOffscreenPreRaster

Added in 1.1.0
@RequiresFeature(name = WebViewFeature.OFF_SCREEN_PRERASTER, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
public static void setOffscreenPreRaster(@NonNull WebSettings settings, boolean enabled)

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")
public static void setRequestedWithHeaderOriginAllowList(
    @NonNull WebSettings settings,
    @NonNull Set<String> allowList
)

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
@NonNull WebSettings settings

Settings retrieved from getSettings.

@NonNull Set<String> allowList

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")
public static void setSafeBrowsingEnabled(@NonNull WebSettings settings, boolean enabled)

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
boolean enabled

Whether Safe Browsing is enabled.

setUserAgentMetadata

Added in 1.9.0
@RequiresFeature(name = WebViewFeature.USER_AGENT_METADATA, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
public static void setUserAgentMetadata(
    @NonNull WebSettings settings,
    @NonNull UserAgentMetadata metadata
)

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
@NonNull WebSettings settings

Settings retrieved from getSettings.

@NonNull UserAgentMetadata metadata

the WebView's user-agent metadata.

setWebViewMediaIntegrityApiStatus

Added in 1.10.0
@RequiresFeature(name = WebViewFeature.WEBVIEW_MEDIA_INTEGRITY_API_STATUS, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
public static void setWebViewMediaIntegrityApiStatus(
    @NonNull WebSettings settings,
    @NonNull WebViewMediaIntegrityApiStatusConfig permissionConfig
)

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