> ## Documentation Index
> Fetch the complete documentation index at: https://docs.adrop.io/llms.txt
> Use this file to discover all available pages before exploring further.

# WebView Guide

> How to display backfill ads in WebView on Android

## Overview

Display backfill ads in your WebView by registering it with the Adrop SDK. This allows web content within your app to show ads through the Adrop platform.

***

## AndroidManifest.xml Configuration

Add the following to your `AndroidManifest.xml` to enable backfill ads in WebView:

```xml AndroidManifest.xml theme={null}
<manifest>
    <application>
        <!-- WebView Backfill Ad Integration -->
        <meta-data
            android:name="com.google.android.gms.ads.INTEGRATION_MANAGER"
            android:value="webview"/>
    </application>
</manifest>
```

<Warning>
  This configuration is required for backfill ads to work properly in WebView. Without it, backfill ads may not display.
</Warning>

***

## Register WebView

<CodeGroup>
  ```kotlin Kotlin theme={null}
  import android.webkit.WebView
  import io.adrop.ads.Adrop

  class WebViewActivity : AppCompatActivity() {

      override fun onCreate(savedInstanceState: Bundle?) {
          super.onCreate(savedInstanceState)

          val webView = findViewById<WebView>(R.id.webView)

          // Register WebView for backfill ads support
          // Call as early as possible, before loading content
          Adrop.registerWebView(webView)

          // Load your web content
          webView.loadUrl("https://your-website.com")
      }
  }
  ```

  ```java Java theme={null}
  import android.webkit.WebView;
  import io.adrop.ads.Adrop;

  public class WebViewActivity extends AppCompatActivity {

      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);

          WebView webView = findViewById(R.id.webView);

          // Register WebView for backfill ads support
          // Call as early as possible, before loading content
          Adrop.registerWebView(webView);

          // Load your web content
          webView.loadUrl("https://your-website.com");
      }
  }
  ```
</CodeGroup>

<Note>
  `registerWebView()` automatically configures optimal WebView settings:

  * JavaScript enabled
  * DOM storage enabled
  * Media playback without user gesture
  * Third-party cookies enabled

  Call this method on the main thread, as early as possible (e.g., in `onCreate`).
</Note>

<Warning>
  Backfill ads in WebView requires `adrop-ads-backfill` module. If the module is not installed, `registerWebView()` is silently ignored.
</Warning>

***

## Handling External URLs

If your app opens non-own-domain URLs in an external browser, you must ensure that ad resource requests (e.g., `googleads.g.doubleclick.net`) are not blocked.

Ad resources such as iframes and scripts are loaded automatically by the ad SDK — they are not user-initiated navigations. Only redirect **user-initiated main frame navigations** to an external browser.

<CodeGroup>
  ```kotlin Kotlin theme={null}
  import android.content.Intent
  import android.net.Uri
  import android.webkit.WebResourceRequest
  import android.webkit.WebView
  import android.webkit.WebViewClient

  val allowedHost = "my-domain.com"

  webView.webViewClient = object : WebViewClient() {
      override fun shouldOverrideUrlLoading(view: WebView, request: WebResourceRequest): Boolean {
          val url = request.url
          val host = url.host ?: ""

          // Only redirect user-initiated navigations to external browser
          // Ad resources (e.g., googleads.g.doubleclick.net) load via iframes/scripts,
          // not user gestures — do not block them
          if (!host.contains(allowedHost) && request.hasGesture()) {
              view.context.startActivity(Intent(Intent.ACTION_VIEW, url))
              return true
          }
          return false
      }
  }
  ```

  ```java Java theme={null}
  import android.content.Intent;
  import android.net.Uri;
  import android.webkit.WebResourceRequest;
  import android.webkit.WebView;
  import android.webkit.WebViewClient;

  String allowedHost = "my-domain.com";

  webView.setWebViewClient(new WebViewClient() {
      @Override
      public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
          Uri url = request.getUrl();
          String host = url.getHost() != null ? url.getHost() : "";

          // Only redirect user-initiated navigations to external browser
          // Ad resources (e.g., googleads.g.doubleclick.net) load via iframes/scripts,
          // not user gestures — do not block them
          if (!host.contains(allowedHost) && request.hasGesture()) {
              view.getContext().startActivity(new Intent(Intent.ACTION_VIEW, url));
              return true;
          }
          return false;
      }
  });
  ```
</CodeGroup>

<Warning>
  Do not block or redirect all non-own-domain requests. Ad resources like `googleads.g.doubleclick.net` are loaded automatically during the backfill ad process and must be allowed to load within the WebView.
</Warning>

***

## Related Documentation

<CardGroup cols={2}>
  <Card title="Banner Ads" icon="rectangle-ad" href="/sdk/android/banner">
    Learn how to integrate banner ads
  </Card>

  <Card title="Reference" icon="book" href="/sdk/android/reference">
    API reference
  </Card>

  <Card title="Examples" icon="code" href="/sdk/android/examples">
    Example repository
  </Card>
</CardGroup>
