A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://developers.google.com/admob/android/webview below:

Set up WebView | Android

Set up WebView

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

If your app utilizes WebView to display web content, it's recommended to configure it so that content can be optimally monetized with ads.

This guide shows you how to provide information about how to configure a WebView object.

Important: To properly set up and optimize WebView, apply all of the following recommendations to each WebView instance in your app. To help identify each web view, toggle the "highlight-all-webviews" flag in WebView DevTools to highlight the web views in your app with a yellow tint. Enable third-party cookies

To improve your user's ad experience and be consistent with Chrome's cookie policy, enable third-party cookies on your WebView instance.

Java
CookieManager.getInstance().setAcceptThirdPartyCookies(webView, true);
Kotlin
CookieManager.getInstance().setAcceptThirdPartyCookies(webView, true)
Web settings

Default WebView settings are not optimized for ads. Use the WebSettings APIs to configure your WebView for:

Java
import android.webkit.CookieManager;
import android.webkit.WebView;

public class MainActivity extends AppCompatActivity {
  private WebView webView;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    webView = findViewById(R.id.webview);

    // Let the web view accept third-party cookies.
    CookieManager.getInstance().setAcceptThirdPartyCookies(webView, true);
    // Let the web view use JavaScript.
    webView.getSettings().setJavaScriptEnabled(true);
    // Let the web view access local storage.
    webView.getSettings().setDomStorageEnabled(true);
    // Let HTML videos play automatically.
    webView.getSettings().setMediaPlaybackRequiresUserGesture(false);
  }
}
Kotlin
import android.webkit.CookieManager
import android.webkit.WebView

class MainActivity : AppCompatActivity() {
  lateinit var webView: WebView

  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    webView = findViewById(R.id.webview)

    // Let the web view accept third-party cookies.
    CookieManager.getInstance().setAcceptThirdPartyCookies(webView, true)
    // Let the web view use JavaScript.
    webView.settings.javaScriptEnabled = true
    // Let the web view access local storage.
    webView.settings.domStorageEnabled = true
    // Let HTML videos play automatically.
    webView.settings.mediaPlaybackRequiresUserGesture = false
  }
}
Load web view content

Cookies and page URLs are important for web view monetization and only function as expected when loadUrl() is used with a network-based URL. For optimized WebView performance, load web content directly from network-based URLs. Avoid using WebViewAssetLoader, loading assets from the device, or generating web content dynamically.

Java
import android.webkit.CookieManager;
import android.webkit.WebView;

public class MainActivity extends AppCompatActivity {
  private WebView webView;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    webView = findViewById(R.id.webview);

    // Let the web view accept third-party cookies.
    CookieManager.getInstance().setAcceptThirdPartyCookies(webView, true);
    // Let the web view use JavaScript.
    webView.getSettings().setJavaScriptEnabled(true);
    // Let the web view access local storage.
    webView.getSettings().setDomStorageEnabled(true);
    // Let HTML videos play automatically.
    webView.getSettings().setMediaPlaybackRequiresUserGesture(false);

    // Load the URL for optimized web view performance.
    webView.loadUrl("https://webview-api-for-ads-test.glitch.me");
  }
}
Kotlin
import android.webkit.CookieManager
import android.webkit.WebView

class MainActivity : AppCompatActivity() {
  lateinit var webView: WebView

  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    webView = findViewById(R.id.webview)

    // Let the web view accept third-party cookies.
    CookieManager.getInstance().setAcceptThirdPartyCookies(webView, true)
    // Let the web view use JavaScript.
    webView.settings.javaScriptEnabled = true
    // Let the web view access local storage.
    webView.settings.domStorageEnabled = true
    // Let HTML videos play automatically.
    webView.settings.mediaPlaybackRequiresUserGesture = false

    // Load the URL for optimized web view performance.
    webView.loadUrl("https://webview-api-for-ads-test.glitch.me")
  }
}
Test the web view

During app development, we recommend that you load this test URL:

https://webview-api-for-ads-test.glitch.me#webview-settings-tests

to verify these settings have the intended effect on ads. The test URL has success criteria for a complete integration if the following are observed:

Web view settings Video ad

After testing is complete, substitute the test URL with the URL the web view intends to load.

Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025-05-10 UTC.

[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-05-10 UTC."],[[["This guide explains how to configure Android `WebView` for optimal ad monetization by enabling features like third-party cookies, JavaScript, and local storage access."],["It is crucial to apply all recommendations to every `WebView` instance in your app for consistent ad performance."],["Developers should use `loadUrl()` with network-based URLs directly to ensure proper functionality of cookies and optimized web view performance."],["A provided test URL helps verify the effectiveness of your settings for ad integration during development."],["For production, replace the test URL with the intended URL for your `WebView` to load."]]],[]]


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.3