WebViewCompat


public class WebViewCompat


Compatibility version of android.webkit.WebView

Summary

Nested types

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

Denotes that the startUpWebView API surface is experimental.

Callback interface supplied to postVisualStateCallback for receiving notifications about the visual state.

This listener receives messages sent on the JavaScript object which was injected by addWebMessageListener.

Callback interface for startUpWebView.

Public methods

static @NonNull ScriptHandler
@UiThread
@RequiresFeature(name = WebViewFeature.DOCUMENT_START_SCRIPT, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
addDocumentStartJavaScript(
    @NonNull WebView webview,
    @NonNull String script,
    @NonNull Set<String> allowedOriginRules
)

Adds a JavaScript script to the WebView which will be executed in any frame whose origin matches allowedOriginRules when the document begins to load.

static void
@UiThread
@RequiresFeature(name = WebViewFeature.WEB_MESSAGE_LISTENER, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
addWebMessageListener(
    @NonNull WebView webView,
    @NonNull String jsObjectName,
    @NonNull Set<String> allowedOriginRules,
    @NonNull WebViewCompat.WebMessageListener listener
)

Adds a WebMessageListener to the WebView and injects a JavaScript object into each frame that the WebMessageListener will listen on.

static @NonNull WebMessagePortCompat[]
@UiThread
@RequiresFeature(name = WebViewFeature.CREATE_WEB_MESSAGE_CHANNEL, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
createWebMessageChannel(@NonNull WebView webview)

Creates a message channel to communicate with JS and returns the message ports that represent the endpoints of this message channel.

static @Nullable PackageInfo

If WebView has already been loaded into the current process this method will return the package that was used to load it.

static @NonNull Profile
@UiThread
@RequiresFeature(name = WebViewFeature.MULTI_PROFILE, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
getProfile(@NonNull WebView webView)

Gets the Profile associated with this WebView.

static @NonNull Uri
@AnyThread
@RequiresFeature(name = WebViewFeature.SAFE_BROWSING_PRIVACY_POLICY_URL, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
getSafeBrowsingPrivacyPolicyUrl()

Returns a URL pointing to the privacy policy for Safe Browsing reporting.

static @NonNull String
@AnyThread
@RequiresFeature(name = WebViewFeature.GET_VARIATIONS_HEADER, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
getVariationsHeader()

Gets the WebView variations encoded to be used as the X-Client-Data HTTP header.

static @Nullable WebChromeClient
@UiThread
@RequiresFeature(name = WebViewFeature.GET_WEB_CHROME_CLIENT, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
getWebChromeClient(@NonNull WebView webview)

Gets the WebChromeClient.

static @NonNull WebViewClient
@UiThread
@RequiresFeature(name = WebViewFeature.GET_WEB_VIEW_CLIENT, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
getWebViewClient(@NonNull WebView webview)

Gets the WebViewClient for the WebView argument.

static @Nullable WebViewRenderProcess
@UiThread
@RequiresFeature(name = WebViewFeature.GET_WEB_VIEW_RENDERER, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
getWebViewRenderProcess(@NonNull WebView webview)

Gets the WebView renderer associated with this WebView.

static @Nullable WebViewRenderProcessClient
@UiThread
@RequiresFeature(name = WebViewFeature.WEB_VIEW_RENDERER_CLIENT_BASIC_USAGE, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
getWebViewRenderProcessClient(@NonNull WebView webview)

Gets the renderer client object associated with this WebView.

static boolean
@UiThread
@RequiresFeature(name = WebViewFeature.MUTE_AUDIO, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
isAudioMuted(@NonNull WebView webView)

Returns whether this WebView is muted.

static boolean
@AnyThread
@RequiresFeature(name = WebViewFeature.MULTI_PROCESS, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
isMultiProcessEnabled()

Returns true if WebView is running in multi process mode.

static void
@UiThread
@RequiresFeature(name = WebViewFeature.VISUAL_STATE_CALLBACK, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
postVisualStateCallback(
    @NonNull WebView webview,
    long requestId,
    @NonNull WebViewCompat.VisualStateCallback callback
)

Posts a VisualStateCallback, which will be called when the current state of the WebView is ready to be drawn.

static void
@UiThread
@RequiresFeature(name = WebViewFeature.POST_WEB_MESSAGE, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
postWebMessage(
    @NonNull WebView webview,
    @NonNull WebMessageCompat message,
    @NonNull Uri targetOrigin
)

Post a message to main frame.

static void
@UiThread
@RequiresFeature(name = WebViewFeature.WEB_MESSAGE_LISTENER, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
removeWebMessageListener(
    @NonNull WebView webview,
    @NonNull String jsObjectName
)

Removes the WebMessageListener associated with jsObjectName.

static void
@UiThread
@RequiresFeature(name = WebViewFeature.MUTE_AUDIO, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
setAudioMuted(@NonNull WebView webView, boolean mute)

Mute or un-mute this WebView.

static void
@UiThread
@RequiresFeature(name = WebViewFeature.MULTI_PROFILE, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
setProfile(@NonNull WebView webView, @NonNull String profileName)

Sets the Profile with its name as the current Profile for this WebView.

static void
@AnyThread
@RequiresFeature(name = WebViewFeature.SAFE_BROWSING_ALLOWLIST, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
setSafeBrowsingAllowlist(
    @NonNull Set<String> hosts,
    @Nullable ValueCallback<Boolean> callback
)

Configures a set of hosts (domain names/IP addresses) that are exempt from SafeBrowsing checks.

static void
@AnyThread
@RequiresFeature(name = WebViewFeature.SAFE_BROWSING_WHITELIST, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
setSafeBrowsingWhitelist(
    @NonNull List<String> hosts,
    @Nullable ValueCallback<Boolean> callback
)

This method is deprecated.

Please use setSafeBrowsingAllowlist instead.

static void
@UiThread
@RequiresFeature(name = WebViewFeature.WEB_VIEW_RENDERER_CLIENT_BASIC_USAGE, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
setWebViewRenderProcessClient(
    @NonNull WebView webview,
    @Nullable WebViewRenderProcessClient webViewRenderProcessClient
)

Sets the renderer client object associated with this WebView.

static void
@UiThread
@RequiresFeature(name = WebViewFeature.WEB_VIEW_RENDERER_CLIENT_BASIC_USAGE, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
setWebViewRenderProcessClient(
    @NonNull WebView webview,
    @NonNull Executor executor,
    @NonNull WebViewRenderProcessClient webViewRenderProcessClient
)

Sets the renderer client object associated with this WebView.

static void
@AnyThread
@RequiresFeature(name = WebViewFeature.START_SAFE_BROWSING, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
startSafeBrowsing(
    @NonNull Context context,
    @Nullable ValueCallback<Boolean> callback
)

This method is deprecated.

In WebView version 122.0.6174.0 and later, this initialization is done automatically, so there is no need to call this API.

static void

Asynchronously trigger WebView startup.

Public methods

addDocumentStartJavaScript

Added in 1.9.0
@UiThread
@RequiresFeature(name = WebViewFeature.DOCUMENT_START_SCRIPT, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
public static @NonNull ScriptHandler addDocumentStartJavaScript(
    @NonNull WebView webview,
    @NonNull String script,
    @NonNull Set<String> allowedOriginRules
)

Adds a JavaScript script to the WebView which will be executed in any frame whose origin matches allowedOriginRules when the document begins to load.

Note that the script will run before any of the page's JavaScript code and the DOM tree might not be ready at this moment. It will block the loading of the page until it's finished, so should be kept as short as possible.

The injected object from addWebMessageListener API will be injected first and the script can rely on the injected object to send messages to the app.

The script will only run in frames which begin loading after the call returns, therefore it should typically be called before making any loadUrl(), loadData() or loadDataWithBaseURL() call to load the page.

This method can be called multiple times to inject multiple scripts. If more than one script matches a frame's origin, they will be executed in the order they were added.

See addWebMessageListener for the rules of the allowedOriginRules parameter.

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

Parameters
@NonNull WebView webview

The WebView instance that we are interacting with.

@NonNull String script

The JavaScript script to be executed.

@NonNull Set<String> allowedOriginRules

A set of matching rules for the allowed origins.

Returns
@NonNull ScriptHandler

the ScriptHandler, which is a handle for removing the script.

Throws
java.lang.IllegalArgumentException

If one of the allowedOriginRules is invalid.

addWebMessageListener

Added in 1.3.0
@UiThread
@RequiresFeature(name = WebViewFeature.WEB_MESSAGE_LISTENER, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
public static void addWebMessageListener(
    @NonNull WebView webView,
    @NonNull String jsObjectName,
    @NonNull Set<String> allowedOriginRules,
    @NonNull WebViewCompat.WebMessageListener listener
)

Adds a WebMessageListener to the WebView and injects a JavaScript object into each frame that the WebMessageListener will listen on.

The injected JavaScript object will be named jsObjectName in the global scope. This will inject the JavaScript object in any frame whose origin matches allowedOriginRules for every navigation after this call, and the JavaScript object will be available immediately when the page begins to load.

Each allowedOriginRules entry must follow the format SCHEME "://" [ HOSTNAME_PATTERN [ ":" PORT ] ], each part is explained in the below table:

Rule Description Example
http/https with hostname SCHEME is http or https; HOSTNAME_PATTERN is a regular hostname; PORT is optional, when not present, the rule will match port 80 for http and port 443 for https.
  • https://foobar.com:8080 - Matches https:// URL on port 8080, whose normalized host is foobar.com.
  • https://www.example.com - Matches https:// URL on port 443, whose normalized host is www.example.com.
http/https with pattern matching SCHEME is http or https; HOSTNAME_PATTERN is a sub-domain matching pattern with a leading *.; PORT is optional, when not present, the rule will match port 80 for http and port 443 for https.
  • https://*.example.com - Matches https://calendar.example.com and https://foo.bar.example.com but not https://example.com.
  • https://*.example.com:8080 - Matches https://calendar.example.com:8080
http/https with IP literal SCHEME is https or https; HOSTNAME_PATTERN is IP literal; PORT is optional, when not present, the rule will match port 80 for http and port 443 for https.
  • https://127.0.0.1 - Matches https:// URL on port 443, whose IPv4 address is 127.0.0.1
  • https://[::1] or https://[0:0::1]- Matches any URL to the IPv6 loopback address with port 443.
  • https://[::1]:99 - Matches any https:// URL to the IPv6 loopback on port 99.
Custom scheme SCHEME is a custom scheme; HOSTNAME_PATTERN and PORT must not be present.
  • my-app-scheme:// - Matches any my-app-scheme:// URL.
* Wildcard rule, matches any origin.
  • *

Note that this is a powerful API, as the JavaScript object will be injected when the frame's origin matches any one of the allowed origins. The HTTPS scheme is strongly recommended for security; allowing HTTP origins exposes the injected object to any potential network-based attackers. If a wildcard "*" is provided, it will inject the JavaScript object to all frames. A wildcard should only be used if the app wants any third party web page to be able to use the injected object. When using a wildcard, the app must treat received messages as untrustworthy and validate any data carefully.

This method can be called multiple times to inject multiple JavaScript objects.

Let's say the injected JavaScript object is named myObject. We will have following methods on that object once it is available to use:

// Web page (in JavaScript)
// message needs to be a JavaScript String or ArrayBuffer, MessagePorts is an optional
// parameter.
myObject.postMessage(message[, MessagePorts])

          

// To receive messages posted from the app side, assign a function to the "onmessage" // property. This function should accept a single "event" argument. "event" has a "data" // property, which is the message String or ArrayBuffer from the app side. myObject.onmessage = function(event) { ... }

// To be compatible with DOM EventTarget's addEventListener, it accepts type and listener // parameters, where type can be only "message" type and listener can only be a JavaScript // function for myObject. An event object will be passed to listener with a "data" property, // which is the message String or ArrayBuffer from the app side. myObject.addEventListener(type, listener)

// To be compatible with DOM EventTarget's removeEventListener, it accepts type and listener // parameters, where type can be only "message" type and listener can only be a JavaScript // function for myObject. myObject.removeEventListener(type, listener)

We start the communication between JavaScript and the app from the JavaScript side. In order to send message from the app to JavaScript, it needs to post a message from JavaScript first, so the app will have a JavaScriptReplyProxy object to respond. Example:

// Web page (in JavaScript)
myObject.onmessage = function(event) {
  // prints "Got it!" when we receive the app's response.
  console.log(event.data);
}
myObject.postMessage("I'm ready!");
// App (in Java)
WebMessageListener myListener = new WebMessageListener() {
  @Override
  public void onPostMessage(WebView view, WebMessageCompat message, Uri sourceOrigin,
           boolean isMainFrame, JavaScriptReplyProxy replyProxy) {
    // do something about view, message, sourceOrigin and isMainFrame.
    replyProxy.postMessage("Got it!");
  }
};
if (WebViewFeature.isFeatureSupported(WebViewFeature.WEB_MESSAGE_LISTENER)) {
  WebViewCompat.addWebMessageListener(webView, "myObject", rules, myListener);
}

Suppose the communication is already setup, to send ArrayBuffer from the app to web, it needs to check feature flag(WEB_MESSAGE_ARRAY_BUFFER). Here is a example to send file content from app to web:

// App (in Java)
WebMessageListener myListener = new WebMessageListener() {
  @Override
  public void onPostMessage(WebView view, WebMessageCompat message, Uri sourceOrigin,
           boolean isMainFrame, JavaScriptReplyProxy replyProxy) {
    // Communication is setup, send file data to web.
    if (WebViewFeature.isFeatureSupported(WebViewFeature.WEB_MESSAGE_ARRAY_BUFFER)) {
      // Suppose readFileData method is to read content from file.
      byte[] fileData = readFileData("myFile.dat");
      replyProxy.postMessage(fileData);
    }
  }
}
// Web page (in JavaScript)
myObject.onmessage = function(event) {
  if (event.data instanceof ArrayBuffer) {
    const data = event.data;  // Received file content from app.
    const dataView = new DataView(data);
    // Consume file content by using JavaScript DataView to access ArrayBuffer.
  }
}
myObject.postMessage("Setup!");

Suppose the communication is already setup, and feature flag WEB_MESSAGE_ARRAY_BUFFER is check. Here is a example to download image in WebView, and send to app:

// Web page (in JavaScript)
const response = await fetch('example.jpg');
if (response.ok) {
    const imageData = await response.arrayBuffer();
    myObject.postMessage(imageData);
}
// App (in Java)
WebMessageListener myListener = new WebMessageListener() {
  @Override
  public void onPostMessage(WebView view, WebMessageCompat message, Uri sourceOrigin,
           boolean isMainFrame, JavaScriptReplyProxy replyProxy) {
    if (message.getType() == WebMessageCompat.TYPE_ARRAY_BUFFER) {
      byte[] imageData = message.getArrayBuffer();
      // do something like draw image on ImageView.
    }
  }
};

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

Parameters
@NonNull WebView webView

The WebView instance that we are interacting with.

@NonNull String jsObjectName

The name for the injected JavaScript object for this .

@NonNull Set<String> allowedOriginRules

A set of matching rules for the allowed origins.

@NonNull WebViewCompat.WebMessageListener listener

The WebMessageListener to handle postMessage() calls on the JavaScript object.

Throws
java.lang.IllegalArgumentException

If one of the allowedOriginRules is invalid.

createWebMessageChannel

Added in 1.1.0
@UiThread
@RequiresFeature(name = WebViewFeature.CREATE_WEB_MESSAGE_CHANNEL, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
public static @NonNull WebMessagePortCompat[] createWebMessageChannel(@NonNull WebView webview)

Creates a message channel to communicate with JS and returns the message ports that represent the endpoints of this message channel. The HTML5 message channel functionality is described here

The returned message channels are entangled and already in started state.

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

Returns
@NonNull WebMessagePortCompat[]

an array of size two, containing the two message ports that form the message channel.

getCurrentWebViewPackage

Added in 1.1.0
@AnyThread
public static @Nullable PackageInfo getCurrentWebViewPackage(@NonNull Context context)

If WebView has already been loaded into the current process this method will return the package that was used to load it. Otherwise, the package that would be used if the WebView was loaded right now will be returned; this does not cause WebView to be loaded, so this information may become outdated at any time. The WebView package changes either when the current WebView package is updated, disabled, or uninstalled. It can also be changed through a Developer Setting. If the WebView package changes, any app process that has loaded WebView will be killed. The next time the app starts and loads WebView it will use the new WebView package instead.

Returns
@Nullable PackageInfo

the current WebView package, or null if there is none.

getProfile

Added in 1.9.0
@UiThread
@RequiresFeature(name = WebViewFeature.MULTI_PROFILE, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
public static @NonNull Profile getProfile(@NonNull WebView webView)

Gets the Profile associated with this WebView.

Gets the profile object set on this WebView using setProfile, or the default profile if it has not been changed.

Parameters
@NonNull WebView webView

the WebView to get the profile object associated with.

Returns
@NonNull Profile

the profile object set to this WebView.

Throws
java.lang.IllegalStateException

if the WebView has been destroyed.

getSafeBrowsingPrivacyPolicyUrl

Added in 1.1.0
@AnyThread
@RequiresFeature(name = WebViewFeature.SAFE_BROWSING_PRIVACY_POLICY_URL, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
public static @NonNull Uri getSafeBrowsingPrivacyPolicyUrl()

Returns a URL pointing to the privacy policy for Safe Browsing reporting.

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

Returns
@NonNull Uri

the url pointing to a privacy policy document which can be displayed to users.

getVariationsHeader

Added in 1.5.0
@AnyThread
@RequiresFeature(name = WebViewFeature.GET_VARIATIONS_HEADER, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
public static @NonNull String getVariationsHeader()

Gets the WebView variations encoded to be used as the X-Client-Data HTTP header.

The app is responsible for adding the X-Client-Data header to any request that may use variations metadata, such as requests to Google web properties. The returned string will be a base64 encoded ClientVariations proto: https://source.chromium.org/chromium/chromium/src/+/main:components/variations/proto/client_variations.proto

Returns
@NonNull String

the variations header. The string may be empty if the header is not available.

See also
loadUrl

getWebChromeClient

Added in 1.1.0
@UiThread
@RequiresFeature(name = WebViewFeature.GET_WEB_CHROME_CLIENT, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
public static @Nullable WebChromeClient getWebChromeClient(@NonNull WebView webview)

Gets the WebChromeClient.

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

Returns
@Nullable WebChromeClient

the WebChromeClient, or null if not yet set

getWebViewClient

Added in 1.1.0
@UiThread
@RequiresFeature(name = WebViewFeature.GET_WEB_VIEW_CLIENT, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
public static @NonNull WebViewClient getWebViewClient(@NonNull WebView webview)

Gets the WebViewClient for the WebView argument.

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

Returns
@NonNull WebViewClient

the WebViewClient, or a default client if not yet set

getWebViewRenderProcess

Added in 1.1.0
@UiThread
@RequiresFeature(name = WebViewFeature.GET_WEB_VIEW_RENDERER, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
public static @Nullable WebViewRenderProcess getWebViewRenderProcess(@NonNull WebView webview)

Gets the WebView renderer associated with this WebView.

In Android O and above, WebView may run in "multiprocess" mode. In multiprocess mode, rendering of web content is performed by a sandboxed renderer process separate to the application process. This renderer process may be shared with other WebViews in the application, but is not shared with other application processes.

If WebView is running in multiprocess mode, this method returns a handle to the renderer process associated with the WebView, which can be used to control the renderer process.

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

Returns
@Nullable WebViewRenderProcess

the WebViewRenderProcess renderer handle associated with this android.webkit.WebView, or null if WebView is not running in multiprocess mode.

getWebViewRenderProcessClient

Added in 1.1.0
@UiThread
@RequiresFeature(name = WebViewFeature.WEB_VIEW_RENDERER_CLIENT_BASIC_USAGE, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
public static @Nullable WebViewRenderProcessClient getWebViewRenderProcessClient(@NonNull WebView webview)

Gets the renderer client object associated with this WebView.

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

Returns
@Nullable WebViewRenderProcessClient

the WebViewRenderProcessClient object associated with this WebView, if one has been set via setWebViewRenderProcessClient or null otherwise.

isAudioMuted

Added in 1.11.0
@UiThread
@RequiresFeature(name = WebViewFeature.MUTE_AUDIO, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
public static boolean isAudioMuted(@NonNull WebView webView)

Returns whether this WebView is muted.

Parameters
@NonNull WebView webView

the WebView for which to check mute status.

Returns
boolean

true if the WebView is muted, false otherwise.

isMultiProcessEnabled

Added in 1.3.0
@AnyThread
@RequiresFeature(name = WebViewFeature.MULTI_PROCESS, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
public static boolean isMultiProcessEnabled()

Returns true if WebView is running in multi process mode.

In Android O and above, WebView may run in "multiprocess" mode. In multiprocess mode, rendering of web content is performed by a sandboxed renderer process separate to the application process. This renderer process may be shared with other WebViews in the application, but is not shared with other application processes.

postVisualStateCallback

Added in 1.1.0
@UiThread
@RequiresFeature(name = WebViewFeature.VISUAL_STATE_CALLBACK, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
public static void postVisualStateCallback(
    @NonNull WebView webview,
    long requestId,
    @NonNull WebViewCompat.VisualStateCallback callback
)

Posts a VisualStateCallback, which will be called when the current state of the WebView is ready to be drawn.

Because updates to the DOM are processed asynchronously, updates to the DOM may not immediately be reflected visually by subsequent onDraw invocations. The VisualStateCallback provides a mechanism to notify the caller when the contents of the DOM at the current time are ready to be drawn the next time the WebView draws.

The next draw after the callback completes is guaranteed to reflect all the updates to the DOM up to the point at which the VisualStateCallback was posted, but it may also contain updates applied after the callback was posted.

The state of the DOM covered by this API includes the following:

  • primitive HTML elements (div, img, span, etc..)
  • images
  • CSS animations
  • WebGL
  • canvas
It does not include the state of:
  • the video tag

To guarantee that the WebView will successfully render the first frame after the onComplete method has been called a set of conditions must be met:

When using this API it is also recommended to enable pre-rasterization if the is off screen to avoid flickering. See setOffscreenPreRaster for more details and do consider its caveats.

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

Parameters
@NonNull WebView webview

The WebView to post to.

long requestId

An id that will be returned in the callback to allow callers to match requests with callbacks.

@NonNull WebViewCompat.VisualStateCallback callback

The callback to be invoked.

postWebMessage

Added in 1.1.0
@UiThread
@RequiresFeature(name = WebViewFeature.POST_WEB_MESSAGE, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
public static void postWebMessage(
    @NonNull WebView webview,
    @NonNull WebMessageCompat message,
    @NonNull Uri targetOrigin
)

Post a message to main frame. The embedded application can restrict the messages to a certain target origin. See HTML5 spec for how target origin can be used.

A target origin can be set as a wildcard ("*"). However this is not recommended. See the page above for security issues.

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

When posting a WebMessageCompat with type TYPE_ARRAY_BUFFER, this method should check if isFeatureSupported returns true for WEB_MESSAGE_ARRAY_BUFFER. Example:

if (message.getType() == WebMessageCompat.TYPE_ARRAY_BUFFER) {
    if (WebViewFeature.isFeatureSupported(WebViewFeature.WEB_MESSAGE_ARRAY_BUFFER) {
        // ArrayBuffer message is supported, send message here.
        WebViewCompat.postWebMessage(webview, message, ...);
    }
}
Parameters
@NonNull WebView webview

The WebView to post to.

@NonNull WebMessageCompat message

the WebMessage

@NonNull Uri targetOrigin

the target origin.

removeWebMessageListener

Added in 1.3.0
@UiThread
@RequiresFeature(name = WebViewFeature.WEB_MESSAGE_LISTENER, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
public static void removeWebMessageListener(
    @NonNull WebView webview,
    @NonNull String jsObjectName
)

Removes the WebMessageListener associated with jsObjectName.

Note that after this call, the injected JavaScript object is still in the JavaScript context, however any message sent after this call won't reach the WebMessageListener.

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

Parameters
@NonNull WebView webview

The WebView object to remove from.

@NonNull String jsObjectName

The JavaScript object's name that was previously passed to addWebMessageListener.

setAudioMuted

Added in 1.11.0
@UiThread
@RequiresFeature(name = WebViewFeature.MUTE_AUDIO, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
public static void setAudioMuted(@NonNull WebView webView, boolean mute)

Mute or un-mute this WebView.

Parameters
@NonNull WebView webView

the WebView for which to control muting.

boolean mute

true to mute the WebView; false to un-mute the WebView.

setProfile

Added in 1.9.0
@UiThread
@RequiresFeature(name = WebViewFeature.MULTI_PROFILE, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
public static void setProfile(@NonNull WebView webView, @NonNull String profileName)

Sets the Profile with its name as the current Profile for this WebView.

  • This should be called before doing anything else with WebView other than attaching it to the view hierarchy.
  • This should be only called if WebView is to use a Profile other than the default.
  • This method will create the profile if it doesn't exist.
Parameters
@NonNull WebView webView

the WebView to modify.

@NonNull String profileName

the name of the profile to use in the passed webView.

Throws
java.lang.IllegalStateException

if the WebView has been destroyed.

java.lang.IllegalStateException

if the previous profile has been accessed via a call to getProfile.

java.lang.IllegalStateException

if the profile has already been set previously via this method.

java.lang.IllegalStateException

if evaluateJavascript is called on the WebView before this method.

java.lang.IllegalStateException

if the WebView has previously navigated to a web page.

setSafeBrowsingAllowlist

Added in 1.4.0
@AnyThread
@RequiresFeature(name = WebViewFeature.SAFE_BROWSING_ALLOWLIST, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
public static void setSafeBrowsingAllowlist(
    @NonNull Set<String> hosts,
    @Nullable ValueCallback<Boolean> callback
)

Configures a set of hosts (domain names/IP addresses) that are exempt from SafeBrowsing checks. The set is global for all the WebViews.

Each rule should take one of these:

Rule Example Matches Subdomain
HOSTNAME example.com Yes
.HOSTNAME .example.com No
IPV4_LITERAL 192.168.1.1 No
IPV6_LITERAL_WITH_BRACKETS [10:20:30:40:50:60:70:80] No

All other rules, including wildcards, are invalid.

The correct syntax for hosts is defined by RFC 3986.

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

Parameters
@NonNull Set<String> hosts

the set of hosts for which to skip Safe Browsing checks

@Nullable ValueCallback<Boolean> callback

will be called with true if hosts are successfully added to the allowlist, false if any hosts are malformed. The callback will be run on the UI thread

setSafeBrowsingWhitelist

Added in 1.1.0
Deprecated in 1.4.0
@AnyThread
@RequiresFeature(name = WebViewFeature.SAFE_BROWSING_WHITELIST, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
public static void setSafeBrowsingWhitelist(
    @NonNull List<String> hosts,
    @Nullable ValueCallback<Boolean> callback
)

Sets the list of hosts (domain names/IP addresses) that are exempt from SafeBrowsing checks. The list is global for all the WebViews.

Each rule should take one of these:

Rule Example Matches Subdomain
HOSTNAME example.com Yes
.HOSTNAME .example.com No
IPV4_LITERAL 192.168.1.1 No
IPV6_LITERAL_WITH_BRACKETS [10:20:30:40:50:60:70:80] No

All other rules, including wildcards, are invalid.

The correct syntax for hosts is defined by RFC 3986.

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

Parameters
@NonNull List<String> hosts

the list of hosts

@Nullable ValueCallback<Boolean> callback

will be called with true if hosts are successfully added to the allowlist. It will be called with false if any hosts are malformed. The callback will be run on the UI thread

setWebViewRenderProcessClient

Added in 1.1.0
@UiThread
@RequiresFeature(name = WebViewFeature.WEB_VIEW_RENDERER_CLIENT_BASIC_USAGE, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
public static void setWebViewRenderProcessClient(
    @NonNull WebView webview,
    @Nullable WebViewRenderProcessClient webViewRenderProcessClient
)

Sets the renderer client object associated with this WebView.

See setWebViewRenderProcessClient for details, with the following differences:

Callbacks will execute directly on the thread on which this WebView was instantiated.

Passing null for webViewRenderProcessClient will clear the renderer client object for this WebView.

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

Parameters
@NonNull WebView webview

the WebView on which to monitor responsiveness.

@Nullable WebViewRenderProcessClient webViewRenderProcessClient

the WebViewRenderProcessClient to set for callbacks.

setWebViewRenderProcessClient

Added in 1.1.0
@UiThread
@RequiresFeature(name = WebViewFeature.WEB_VIEW_RENDERER_CLIENT_BASIC_USAGE, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
public static void setWebViewRenderProcessClient(
    @NonNull WebView webview,
    @NonNull Executor executor,
    @NonNull WebViewRenderProcessClient webViewRenderProcessClient
)

Sets the renderer client object associated with this WebView.

The renderer client encapsulates callbacks relevant to WebView renderer state. See WebViewRenderProcessClient for details.

Although many WebView instances may share a single underlying renderer, and renderers may live either in the application process, or in a sandboxed process that is isolated from the application process, instances of WebViewRenderProcessClient are set per-WebView. Callbacks represent renderer events from the perspective of this WebView, and may or may not be correlated with renderer events affecting other WebViews.

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

Parameters
@NonNull WebView webview

the WebView on which to monitor responsiveness.

@NonNull Executor executor

the Executor that will be used to execute callbacks.

@NonNull WebViewRenderProcessClient webViewRenderProcessClient

the WebViewRenderProcessClient to set for callbacks.

startSafeBrowsing

Added in 1.1.0
Deprecated in 1.13.0-alpha01
@AnyThread
@RequiresFeature(name = WebViewFeature.START_SAFE_BROWSING, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
public static void startSafeBrowsing(
    @NonNull Context context,
    @Nullable ValueCallback<Boolean> callback
)

Starts Safe Browsing initialization.

URL loads are not guaranteed to be protected by Safe Browsing until after callback is invoked with true. Safe Browsing is not fully supported on all devices. For those devices callback will receive false.

This should not be called if Safe Browsing has been disabled by manifest tag or setSafeBrowsingEnabled. This prepares resources used for Safe Browsing.

This should be called with the Application Context (and will always use the Application context to do its work regardless).

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

Parameters
@NonNull Context context

Application Context.

@Nullable ValueCallback<Boolean> callback

will be called on the UI thread with true if initialization is successful, false otherwise.

startUpWebView

@WebViewCompat.ExperimentalAsyncStartUp
@AnyThread
public static void startUpWebView(
    @NonNull WebViewStartUpConfig config,
    @NonNull WebViewCompat.WebViewStartUpCallback callback
)

Asynchronously trigger WebView startup.

WebView startup is a time-consuming process that is normally triggered during the first usage of WebView related APIs. WebView startup happens once per process. For example, the first call to `new WebView()` can take longer to complete than future calls due to WebView startup being triggered. The Android UI thread remains blocked till the startup completes.

This method allows callers to trigger WebView startup at a time of their choosing.

There are performance improvements this API provides. This method ensures that the portions of WebView startup which are able to run in the background will do so. Other portions of startup will still run on the UI thread.

Any APIs in `android.webkit` and `androidx.webkit` (including WebViewFeature MUST only be called after the callback is invoked in order to ensure the maximum benefit. There is no feature check or call to WebViewFeature required for using this method.

This API can be called multiple times. The callback will be called promptly if startup has already completed.

This is an experimental API and unsuitable for non-experimental use. This method can be removed in future versions of the library.

Parameters
@NonNull WebViewStartUpConfig config

configuration for startup.

@NonNull WebViewCompat.WebViewStartUpCallback callback

the callback triggered when WebView startup is complete. This will be called on the main looper (Looper.getMainLooper()).