WebSettingsCompat
public class WebSettingsCompat
Compatibility version of android.webkit.WebSettings
Summary
Nested types |
|---|
@Retention(value = RetentionPolicy.CLASS)Denotes that the SpeculativeLoading API surface is experimental. |
@Retention(value = RetentionPolicy.CLASS)Denotes that the SpeculativeLoading API surface is experimental. |
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 |
static final int |
This field is deprecated. refer to |
static final int |
This field is deprecated. refer to |
static final int |
FORCE_DARK_AUTO = 1This field is deprecated. refer to |
static final int |
FORCE_DARK_OFF = 0This field is deprecated. refer to |
static final int |
FORCE_DARK_ON = 2This field is deprecated. refer to |
static final int |
Disables all speculative loading tech. |
static final int |
Enabled Prerender for this WebSettings, See Prerender to learn more. |
static final int |
The support level that allows WebAuthn requests for the app in which the WebView is embedded. |
static final int |
The support level that allows apps to make WebAuthn calls for any website. |
static final int |
The support level that disables WebAuthn requests from WebView. |
Public methods |
|
|---|---|
static boolean |
@RequiresFeature(name = WebViewFeature.COOKIE_INTERCEPT, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")Returns whether cookie access during request intercept is enabled. |
static int |
@RequiresFeature(name = WebViewFeature.ATTRIBUTION_REGISTRATION_BEHAVIOR, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")Read the current behavior for attribution registration. |
static boolean |
@RequiresFeature(name = WebViewFeature.BACK_FORWARD_CACHE, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")Get the current status of BackForwardCache for this |
static @NonNull BackForwardCacheSettings |
@RequiresFeature(name = WebViewFeature.BACK_FORWARD_CACHE_SETTINGS, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")Get the current |
static int |
@RequiresFeature(name = WebViewFeature.DISABLED_ACTION_MODE_MENU_ITEMS, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")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")Gets whether EnterpriseAuthenticationAppLinkPolicy is allowed to have any effect on WebView. |
static int |
@RequiresFeature(name = WebViewFeature.FORCE_DARK, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")This method is deprecated. refer to |
static int |
@RequiresFeature(name = WebViewFeature.FORCE_DARK_STRATEGY, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")This method is deprecated. refer to |
static boolean |
@RequiresFeature(name = WebViewFeature.PAYMENT_REQUEST, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")Get the current status of |
static boolean |
@RequiresFeature(name = WebViewFeature.OFF_SCREEN_PRERASTER, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")Gets whether this WebView should raster tiles when it is offscreen but attached to a window. |
static boolean |
@RequiresFeature(name = WebViewFeature.PAYMENT_REQUEST, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")Get the current status of PaymentRequest for the given |
static @NonNull Set<String> |
@RequiresFeature(name = WebViewFeature.REQUESTED_WITH_HEADER_ALLOW_LIST, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")This method is deprecated. The origin trial to disable the X-Requested-With feature has ended, so this API now just returns an empty set. |
static boolean |
@RequiresFeature(name = WebViewFeature.SAFE_BROWSING_ENABLE, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")Gets whether Safe Browsing is enabled. |
static int |
@RequiresFeature(name = WebViewFeature.SPECULATIVE_LOADING, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")Gets speculative loading status for this |
static @NonNull UserAgentMetadata |
@RequiresFeature(name = WebViewFeature.USER_AGENT_METADATA, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")Get the WebView's user-agent metadata which used to generate user-agent client hints. |
static int |
@RequiresFeature(name = WebViewFeature.WEB_AUTHENTICATION, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")Returns the support level for the given |
static @NonNull WebViewMediaIntegrityApiStatusConfig |
@RequiresFeature(name = WebViewFeature.WEBVIEW_MEDIA_INTEGRITY_API_STATUS, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")Returns the |
static boolean |
@RequiresFeature(name = WebViewFeature.ALGORITHMIC_DARKENING, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")Get if algorithmic darkening is allowed or not for this WebView. |
static void |
@RequiresFeature(name = WebViewFeature.ALGORITHMIC_DARKENING, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")Control whether algorithmic darkening is allowed. |
static void |
@RequiresFeature(name = WebViewFeature.ATTRIBUTION_REGISTRATION_BEHAVIOR, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")Control how WebView interacts with Attribution Reporting. |
static void |
@RequiresFeature(name = WebViewFeature.BACK_FORWARD_CACHE, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")Enables BackForwardCache for the given |
static void |
@RequiresFeature(name = WebViewFeature.BACK_FORWARD_CACHE_SETTINGS, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")Sets the |
static void |
@RequiresFeature(name = WebViewFeature.COOKIE_INTERCEPT, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")This method controls if the relevant |
static void |
@RequiresFeature(name = WebViewFeature.DISABLED_ACTION_MODE_MENU_ITEMS, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")Disables the action mode menu items according to |
static void |
@RequiresFeature(name = WebViewFeature.ENTERPRISE_AUTHENTICATION_APP_LINK_POLICY, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")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")This method is deprecated. The "force dark" model previously implemented by WebView was complex and didn't interoperate well with current Web standards for |
static void |
@RequiresFeature(name = WebViewFeature.FORCE_DARK_STRATEGY, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")This method is deprecated. refer to |
static void |
@RequiresFeature(name = WebViewFeature.PAYMENT_REQUEST, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")Enables support for |
static void |
@RequiresFeature(name = WebViewFeature.HYPERLINK_CONTEXT_MENU_ITEMS, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")Sets which items appear in the context menu when a user long-presses a hyperlink. |
static void |
@RequiresFeature(name = WebViewFeature.OFF_SCREEN_PRERASTER, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")Sets whether this WebView should raster tiles when it is offscreen but attached to a window. |
static void |
@RequiresFeature(name = WebViewFeature.PAYMENT_REQUEST, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")Enables PaymentRequest for the given |
static void |
@RequiresFeature(name = WebViewFeature.REQUESTED_WITH_HEADER_ALLOW_LIST, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")This method is deprecated. The origin trial to disable the X-Requested-With feature has ended, so this API no longer does anything. |
static void |
@RequiresFeature(name = WebViewFeature.SAFE_BROWSING_ENABLE, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")Sets whether Safe Browsing is enabled. |
static void |
@RequiresFeature(name = WebViewFeature.SPECULATIVE_LOADING, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")Sets whether speculative loading status for this |
static void |
@RequiresFeature(name = WebViewFeature.USER_AGENT_METADATA, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")Sets the WebView's user-agent metadata to generate user-agent client hints. |
static void |
@RequiresFeature(name = WebViewFeature.WEB_AUTHENTICATION, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")Sets the support level for the given |
static void |
@RequiresFeature(name = WebViewFeature.WEBVIEW_MEDIA_INTEGRITY_API_STATUS, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")Sets permissions provided through |
Constants
ATTRIBUTION_BEHAVIOR_APP_SOURCE_AND_APP_TRIGGER
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
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
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
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.
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.
| See also | |
|---|---|
setForceDarkStrategy |
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.
| See also | |
|---|---|
setForceDarkStrategy |
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.
| See also | |
|---|---|
setForceDarkStrategy |
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 |
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 |
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 |
SPECULATIVE_LOADING_DISABLED
@WebSettingsCompat.ExperimentalSpeculativeLoading
public static final int SPECULATIVE_LOADING_DISABLED = 0
Disables all speculative loading tech. See this to learn more.
This is the default behavior.
SPECULATIVE_LOADING_PRERENDER_ENABLED
@WebSettingsCompat.ExperimentalSpeculativeLoading
public static final int SPECULATIVE_LOADING_PRERENDER_ENABLED = 1
Enabled Prerender for this WebSettings, See Prerender to learn more.
WEB_AUTHENTICATION_SUPPORT_FOR_APP
public static final int WEB_AUTHENTICATION_SUPPORT_FOR_APP = 1
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
public static final int WEB_AUTHENTICATION_SUPPORT_FOR_BROWSER = 2
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
public static final int WEB_AUTHENTICATION_SUPPORT_NONE = 0
The support level that disables WebAuthn requests from WebView.
This is the default behavior.
Public methods
areCookiesIncludedInShouldInterceptRequest
@RequiresFeature(name = WebViewFeature.COOKIE_INTERCEPT, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
public static boolean areCookiesIncludedInShouldInterceptRequest(
@NonNull WebSettings settings
)
Returns whether cookie access during request intercept is enabled.
This method should only be called if isFeatureSupported returns true for COOKIE_INTERCEPT.
| Parameters | |
|---|---|
@NonNull WebSettings settings |
Settings retrieved from |
getAttributionRegistrationBehavior
@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 |
getBackForwardCacheEnabled
@RequiresFeature(name = WebViewFeature.BACK_FORWARD_CACHE, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
public static boolean getBackForwardCacheEnabled(@NonNull WebSettings settings)
Get the current status of BackForwardCache for this WebSettings.
This method should only be called if isFeatureSupported returns true for BACK_FORWARD_CACHE.
| Parameters | |
|---|---|
@NonNull WebSettings settings |
Settings retrieved from |
| Returns | |
|---|---|
boolean |
Whether BackForwardCache is enabled or not. |
getBackForwardCacheSettings
@RequiresFeature(name = WebViewFeature.BACK_FORWARD_CACHE_SETTINGS, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
@WebSettingsCompat.ExperimentalBackForwardCacheSettings
public static @NonNull BackForwardCacheSettings getBackForwardCacheSettings(@NonNull WebSettings settings)
Get the current BackForwardCacheSettings for this WebSettings.
This method should only be called if isFeatureSupported returns true for BACK_FORWARD_CACHE_SETTINGS.
| Parameters | |
|---|---|
@NonNull WebSettings settings |
Settings retrieved from |
| Returns | |
|---|---|
@NonNull BackForwardCacheSettings |
The current settings for the BackForwardCache. If the BackForwardCache feature is disabled, a default instance is returned instead. |
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 |
|
@RequiresFeature(name = WebViewFeature.FORCE_DARK, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
public static intgetForceDark(@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 |
@RequiresFeature(name = WebViewFeature.FORCE_DARK_STRATEGY, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
public static intgetForceDarkStrategy(@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. |
| See also | |
|---|---|
setForceDarkStrategy |
getHasEnrolledInstrumentEnabled
@RequiresFeature(name = WebViewFeature.PAYMENT_REQUEST, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
public static boolean getHasEnrolledInstrumentEnabled(@NonNull WebSettings settings)
Get the current status of hasEnrolledInstrument() method in PaymentRequest for the given WebSettings.
This method should only be called if isFeatureSupported returns true for PAYMENT_REQUEST.
| Parameters | |
|---|---|
@NonNull WebSettings settings |
Settings retrieved from |
| Returns | |
|---|---|
boolean |
Whether |
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 |
|
getPaymentRequestEnabled
@RequiresFeature(name = WebViewFeature.PAYMENT_REQUEST, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
public static boolean getPaymentRequestEnabled(@NonNull WebSettings settings)
Get the current status of PaymentRequest for the given WebSettings.
This method should only be called if isFeatureSupported returns true for PAYMENT_REQUEST.
| Parameters | |
|---|---|
@NonNull WebSettings settings |
Settings retrieved from |
| Returns | |
|---|---|
boolean |
Whether PaymentRequest is enabled. |
getRequestedWithHeaderOriginAllowList
@RequiresFeature(name = WebViewFeature.REQUESTED_WITH_HEADER_ALLOW_LIST, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
public static @NonNull Set<String>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.
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 |
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 |
|
getSpeculativeLoadingStatus
@RequiresFeature(name = WebViewFeature.SPECULATIVE_LOADING, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
@WebSettingsCompat.ExperimentalSpeculativeLoading
public static int getSpeculativeLoadingStatus(@NonNull WebSettings settings)
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 | |
|---|---|
@NonNull WebSettings settings |
Settings retrieved from |
| Returns | |
|---|---|
int |
The current status for the speculative loading. It will to be one of |
getUserAgentMetadata
@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 |
getWebAuthenticationSupport
@RequiresFeature(name = WebViewFeature.WEB_AUTHENTICATION, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
@UiThread
public static int getWebAuthenticationSupport(@NonNull WebSettings settings)
Returns the support level for the given WebSettings
This method should only be called if isFeatureSupported returns true for WEB_AUTHENTICATION.
| Parameters | |
|---|---|
@NonNull WebSettings settings |
Settings retrieved from |
| Returns | |
|---|---|
int |
the current support level. |
getWebViewMediaIntegrityApiStatus
@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
@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. |
| See also | |
|---|---|
setAlgorithmicDarkeningAllowed |
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 targetSdkVersion ≥ TIRAMISU.
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 targetSdkVersion ≥ TIRAMISU, 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, targetSdkVersion ≥ TIRAMISU, 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, targetSdkVersion ≥ TIRAMISU, 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 | |
|---|---|
@NonNull WebSettings settings |
The WebSettings object to update. |
boolean allow |
allow algorithmic darkening or not. |
setAttributionRegistrationBehavior
@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 |
int behavior |
New behavior to use. |
setBackForwardCacheEnabled
@RequiresFeature(name = WebViewFeature.BACK_FORWARD_CACHE, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
public static void setBackForwardCacheEnabled(
@NonNull WebSettings settings,
boolean backForwardCacheEnabled
)
Enables BackForwardCache for the given WebSettings.
This method should only be called if isFeatureSupported returns true for BACK_FORWARD_CACHE.
| Parameters | |
|---|---|
@NonNull WebSettings settings |
Settings retrieved from |
boolean backForwardCacheEnabled |
whether BackForwardCache should be enabled for this |
setBackForwardCacheSettings
@RequiresFeature(name = WebViewFeature.BACK_FORWARD_CACHE_SETTINGS, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
@WebSettingsCompat.ExperimentalBackForwardCacheSettings
public static void setBackForwardCacheSettings(
@NonNull WebSettings settings,
@NonNull BackForwardCacheSettings backForwardCacheSettings
)
Sets the BackForwardCacheSettings for this WebSettings.
This method should only be called if isFeatureSupported returns true for BACK_FORWARD_CACHE_SETTINGS.
Calling this API will enable the BackForwardCache feature, which is equivalent to calling setBackForwardCacheEnabled with true.
| Parameters | |
|---|---|
@NonNull WebSettings settings |
Settings retrieved from |
@NonNull BackForwardCacheSettings backForwardCacheSettings |
The new settings for the BackForwardCache. |
setCookiesIncludedInShouldInterceptRequest
@RequiresFeature(name = WebViewFeature.COOKIE_INTERCEPT, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
public static void setCookiesIncludedInShouldInterceptRequest(
@NonNull WebSettings settings,
boolean enabled
)
This method controls if the relevant Cookie header will be added to the WebResourceRequest object passed to shouldInterceptRequest. It also enables the use of setCookies, which will otherwise be ignored.
Prefer using this method over calling CookieManager as part of intercepting requests if it is necessary to access cookies, as this approach will provide the correct set of cookies for the request.
This method should only be called if isFeatureSupported returns true for COOKIE_INTERCEPT.
| Parameters | |
|---|---|
@NonNull WebSettings settings |
Settings retrieved from |
boolean enabled |
Whether cookie access during request intercept should be enabled. |
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 | |
|---|---|
@NonNull WebSettings settings |
The WebSettings object to update. |
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 | |
|---|---|
@NonNull WebSettings settings |
The WebSettings object to update. |
boolean enabled |
Whether EnterpriseAuthenticationAppLinkPolicy should be enabled. |
@RequiresFeature(name = WebViewFeature.FORCE_DARK, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
public static voidsetForceDark(@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 |
@RequiresFeature(name = WebViewFeature.FORCE_DARK_STRATEGY, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
public static voidsetForceDarkStrategy(
@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. |
| See also | |
|---|---|
getForceDarkStrategy |
setHasEnrolledInstrumentEnabled
@RequiresFeature(name = WebViewFeature.PAYMENT_REQUEST, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
public static void setHasEnrolledInstrumentEnabled(
@NonNull WebSettings settings,
boolean enabled
)
Enables support for hasEnrolledInstrument() method in PaymentRequest for the given WebSettings. This only has any effect if WebSettingsCompat.setPaymentRequestEnabled(settings, true) has been called.
When this setting is disabled, the PaymentRequest.hasEnrolledInstrument() method always returns false.
This method should only be called if isFeatureSupported returns true for PAYMENT_REQUEST.
| Parameters | |
|---|---|
@NonNull WebSettings settings |
Settings retrieved from |
boolean enabled |
Whether |
setHyperlinkContextMenuItems
@RequiresFeature(name = WebViewFeature.HYPERLINK_CONTEXT_MENU_ITEMS, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
public static void setHyperlinkContextMenuItems(
@NonNull WebSettings settings,
int hyperlinkMenuItems
)
Sets which items appear in the context menu when a user long-presses a hyperlink. The default value is HyperlinkContextMenuItems.DISABLED which means no menu is shown.
The items are specified using bitwise flags. You can combine multiple items using the bitwise OR operator (`|`). For example:
// Show only "Copy link address" and "Open link". webSettings.setHyperlinkContextMenuItems( WebSettingsCompat.HyperlinkContextMenuItems.COPY_LINK_ADDRESS | WebSettingsCompat.HyperlinkContextMenuItems.OPEN_LINK);
| Parameters | |
|---|---|
@NonNull WebSettings settings |
The |
int hyperlinkMenuItems |
A bitwise combination of the following flags: |
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.
setPaymentRequestEnabled
@RequiresFeature(name = WebViewFeature.PAYMENT_REQUEST, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
public static void setPaymentRequestEnabled(@NonNull WebSettings settings, boolean enabled)
Enables PaymentRequest for the given WebSettings.
This feature requires adding the following snippet to the AndroidManifest.xml:
<queries>
<intent>
<action android:name="org.chromium.intent.action.PAY"/>
</intent>
<intent>
<action android:name="org.chromium.intent.action.IS_READY_TO_PAY"/>
</intent>
<intent>
<action android:name="org.chromium.intent.action.UPDATE_PAYMENT_DETAILS"/>
</intent>
</queries>This is needed to allow WebView to query the device for the user's payment applications that are implemented according to the Android payment app developers guide.
- The
org.chromium.intent.action.PAYintent is necessary to let users make payments. - The
org.chromium.intent.action.IS_READY_TO_PAYintent is necessary to let the merchant website know if the user is ready to pay. - The
org.chromium.intent.action.UPDATE_PAYMENT_DETAILSintent is necessary for payment apps to support updating the price dynamically in response to the user selecting a different shipping address or option.
This method should only be called if isFeatureSupported returns true for PAYMENT_REQUEST.
| Parameters | |
|---|---|
@NonNull WebSettings settings |
Settings retrieved from |
boolean enabled |
Whether PaymentRequestshould be enabled for this |
setRequestedWithHeaderOriginAllowList
@RequiresFeature(name = WebViewFeature.REQUESTED_WITH_HEADER_ALLOW_LIST, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
public static voidsetRequestedWithHeaderOriginAllowList(
@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 |
@NonNull Set<String> allowList |
Set of origins to allow-list. |
| Throws | |
|---|---|
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 | |
|---|---|
@NonNull WebSettings settings |
The WebSettings object to update. |
boolean enabled |
Whether Safe Browsing is enabled. |
setSpeculativeLoadingStatus
@RequiresFeature(name = WebViewFeature.SPECULATIVE_LOADING, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
@WebSettingsCompat.ExperimentalSpeculativeLoading
public static void setSpeculativeLoadingStatus(
@NonNull WebSettings settings,
int speculativeLoadingStatus
)
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 | |
|---|---|
@NonNull WebSettings settings |
Settings retrieved from |
int speculativeLoadingStatus |
The new status for the speculative loading. It will to be one of |
setUserAgentMetadata
@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 |
@NonNull UserAgentMetadata metadata |
the WebView's user-agent metadata. |
setWebAuthenticationSupport
@RequiresFeature(name = WebViewFeature.WEB_AUTHENTICATION, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
@UiThread
public static void setWebAuthenticationSupport(@NonNull WebSettings settings, int support)
Sets the support level for the given WebSettings.
This method should only be called if isFeatureSupported returns true for WEB_AUTHENTICATION.
| Parameters | |
|---|---|
@NonNull WebSettings settings |
Settings retrieved from |
int support |
The new support level which this WebView will use. |
setWebViewMediaIntegrityApiStatus
@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.