WebSettingsCompat
public
class
WebSettingsCompat
extends Object
java.lang.Object | |
↳ | androidx.webkit.WebSettingsCompat |
Compatibility version of WebSettings
Summary
Constants | |
---|---|
int |
DARK_STRATEGY_PREFER_WEB_THEME_OVER_USER_AGENT_DARKENING
In this mode WebView content will be darkened by a user agent unless web page supports dark theme. |
int |
DARK_STRATEGY_USER_AGENT_DARKENING_ONLY
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. |
int |
DARK_STRATEGY_WEB_THEME_DARKENING_ONLY
In this mode WebView content will always be darkened using dark theme provided by web page. |
int |
FORCE_DARK_AUTO
Enable force dark dependent on the state of the WebView parent view. |
int |
FORCE_DARK_OFF
Disable force dark, irrespective of the force dark mode of the WebView parent. |
int |
FORCE_DARK_ON
Unconditionally enable force dark. |
Public methods | |
---|---|
static
int
|
getDisabledActionModeMenuItems(WebSettings settings)
Gets the action mode menu items that are disabled, expressed in an integer field flag. |
static
int
|
getForceDark(WebSettings settings)
Get the force dark mode for this WebView. |
static
int
|
getForceDarkStrategy(WebSettings settings)
Get how content is darkened for this WebView. |
static
boolean
|
getOffscreenPreRaster(WebSettings settings)
Gets whether this WebView should raster tiles when it is offscreen but attached to a window. |
static
boolean
|
getSafeBrowsingEnabled(WebSettings settings)
Gets whether Safe Browsing is enabled. |
static
void
|
setDisabledActionModeMenuItems(WebSettings settings, int menuItems)
Disables the action mode menu items according to |
static
void
|
setForceDark(WebSettings settings, int forceDarkMode)
Set the force dark mode for this WebView. |
static
void
|
setForceDarkStrategy(WebSettings settings, int forceDarkBehavior)
Set how WebView content should be darkened. |
static
void
|
setOffscreenPreRaster(WebSettings settings, boolean enabled)
Sets whether this WebView should raster tiles when it is offscreen but attached to a window. |
static
void
|
setSafeBrowsingEnabled(WebSettings settings, boolean enabled)
Sets whether Safe Browsing is enabled. |
Inherited methods | |
---|---|
Constants
DARK_STRATEGY_PREFER_WEB_THEME_OVER_USER_AGENT_DARKENING
public static final int DARK_STRATEGY_PREFER_WEB_THEME_OVER_USER_AGENT_DARKENING
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:
Constant Value: 2 (0x00000002)
DARK_STRATEGY_USER_AGENT_DARKENING_ONLY
public static final int DARK_STRATEGY_USER_AGENT_DARKENING_ONLY
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:
Constant Value: 0 (0x00000000)
DARK_STRATEGY_WEB_THEME_DARKENING_ONLY
public static final int DARK_STRATEGY_WEB_THEME_DARKENING_ONLY
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:
Constant Value: 1 (0x00000001)
FORCE_DARK_AUTO
public static final int FORCE_DARK_AUTO
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:
Constant Value: 1 (0x00000001)
FORCE_DARK_OFF
public static final int FORCE_DARK_OFF
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:
Constant Value: 0 (0x00000000)
FORCE_DARK_ON
public static final int FORCE_DARK_ON
Unconditionally enable force dark. In this mode WebView content will always be rendered so as to emulate a dark theme.
See also:
Constant Value: 2 (0x00000002)
Public methods
getDisabledActionModeMenuItems
public static int getDisabledActionModeMenuItems (WebSettings settings)
Gets the action mode menu items that are disabled, expressed in an integer field flag.
The default value is WebSettings.MENU_ITEM_NONE
This method should only be called if
WebViewFeature.isFeatureSupported(String)
returns true for WebViewFeature.DISABLED_ACTION_MODE_MENU_ITEMS
.
Parameters | |
---|---|
settings |
WebSettings |
Returns | |
---|---|
int |
all the disabled menu item flags combined with bitwise OR. |
getForceDark
public static int getForceDark (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
WebViewFeature.isFeatureSupported(String)
returns true for WebViewFeature.FORCE_DARK
.
Parameters | |
---|---|
settings |
WebSettings |
Returns | |
---|---|
int |
the currently set force dark mode. |
See also:
getForceDarkStrategy
public static int getForceDarkStrategy (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
WebViewFeature.isFeatureSupported(String)
returns true for WebViewFeature.FORCE_DARK_STRATEGY
.
Parameters | |
---|---|
settings |
WebSettings |
Returns | |
---|---|
int |
the currently set force dark strategy. |
See also:
getOffscreenPreRaster
public static boolean getOffscreenPreRaster (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
WebViewFeature.isFeatureSupported(String)
returns true for WebViewFeature.OFF_SCREEN_PRERASTER
.
Parameters | |
---|---|
settings |
WebSettings |
Returns | |
---|---|
boolean |
true if this WebView will raster tiles when it is
offscreen but attached to a window.
|
getSafeBrowsingEnabled
public static boolean getSafeBrowsingEnabled (WebSettings settings)
Gets whether Safe Browsing is enabled.
See setSafeBrowsingEnabled(WebSettings, boolean)
.
This method should only be called if
WebViewFeature.isFeatureSupported(String)
returns true for WebViewFeature.SAFE_BROWSING_ENABLE
.
Parameters | |
---|---|
settings |
WebSettings |
Returns | |
---|---|
boolean |
true if Safe Browsing is enabled and false otherwise.
|
setDisabledActionModeMenuItems
public static void setDisabledActionModeMenuItems (WebSettings settings, int menuItems)
Disables the action mode menu items according to menuItems
flag.
This method should only be called if
WebViewFeature.isFeatureSupported(String)
returns true for WebViewFeature.DISABLED_ACTION_MODE_MENU_ITEMS
.
Parameters | |
---|---|
settings |
WebSettings |
menuItems |
int : an integer field flag for the menu items to be disabled.
|
setForceDark
public static void setForceDark (WebSettings settings, int forceDarkMode)
Set the force dark mode for this WebView.
This method should only be called if
WebViewFeature.isFeatureSupported(String)
returns true for WebViewFeature.FORCE_DARK
.
If equals to FORCE_DARK_ON
then setForceDarkStrategy(WebSettings, int)
is used to
specify darkening strategy.
Parameters | |
---|---|
settings |
WebSettings |
forceDarkMode |
int : the force dark mode to set. |
See also:
setForceDarkStrategy
public static void setForceDarkStrategy (WebSettings settings, int forceDarkBehavior)
Set how WebView content should be darkened.
This method should only be called if
WebViewFeature.isFeatureSupported(String)
returns true for WebViewFeature.FORCE_DARK_STRATEGY
.
The specified strategy is only used if force dark mode is on.
See setForceDark(WebSettings, int)
.
Parameters | |
---|---|
settings |
WebSettings |
forceDarkBehavior |
int : the force dark strategy to set. |
See also:
setOffscreenPreRaster
public static void setOffscreenPreRaster (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
WebViewFeature.isFeatureSupported(String)
returns true for WebViewFeature.OFF_SCREEN_PRERASTER
.
Parameters | |
---|---|
settings |
WebSettings |
enabled |
boolean |
setSafeBrowsingEnabled
public static void setSafeBrowsingEnabled (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
WebViewFeature.isFeatureSupported(String)
returns true for WebViewFeature.SAFE_BROWSING_ENABLE
.
Parameters | |
---|---|
settings |
WebSettings |
enabled |
boolean : Whether Safe Browsing is enabled.
|