Stay organized with collections Save and categorize content based on your preferences.

WebSettingsCompat

public class WebSettingsCompat


Compatibility version of android.webkit.WebSettings

Summary

Constants

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

Constants

DARK_STRATEGY_PREFER_WEB_THEME_OVER_USER_AGENT_DARKENING

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

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

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

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

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

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

getDisabledActionModeMenuItems

@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

@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

@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

@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

@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

@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

@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.

isAlgorithmicDarkeningAllowed

@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

@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.

setDisabledActionModeMenuItems

@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

@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

@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

@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

@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

@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 java.lang.IllegalArgumentException

if the allow-list contains a malformed origin.

setSafeBrowsingEnabled

@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.