Migrate interstitial ads

  • This page highlights the differences in loading and showing an interstitial ad between the current and Google Mobile Ads SDK (beta).

  • The examples demonstrate the code for loading an interstitial ad in both the current and Google Mobile Ads SDK (beta) using Kotlin and Java.

  • The examples demonstrate the code for showing an interstitial ad, which remains consistent between the current and Google Mobile Ads SDK (beta) in both Kotlin and Java.

This page covers the differences in loading and showing an interstitial ad between the current and Google Mobile Ads SDK (beta).

Load an ad

The following examples load an interstitial ad:

Current

Kotlin

InterstitialAd.load(
  this@InterstitialActivity,
  "AD_UNIT_ID",
  AdRequest.Builder().build(),
  object : InterstitialAdLoadCallback() {
    override fun onAdLoaded(ad: InterstitialAd) {
      // Called when an ad has loaded.
      ad.fullScreenContentCallback =
        object : FullScreenContentCallback() {
        }
      interstitialAd = ad
    }

    override fun onAdFailedToLoad(loadAdError: LoadAdError) {
      // Called when ad fails to load.
    }
  }
)

Java

InterstitialAd.load(
  this,
  "AD_UNIT_ID",
  new AdRequest.Builder().build(),
  new InterstitialAdLoadCallback() {
    @Override
    public void onAdLoaded(@NonNull InterstitialAd ad) {
      // Called when an ad has loaded.
      ad.setFullScreenContentCallback(new FullScreenContentCallback() {});
      interstitialAd = ad;
    }

    @Override
    public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
      // Called when ad fails to load.
    }
  }
);
Google Mobile Ads SDK (beta)

Kotlin

InterstitialAd.load(
  AdRequest.Builder("AD_UNIT_ID").build(),
  object : AdLoadCallback<InterstitialAd> {
    override fun onAdLoaded(ad: InterstitialAd) {
      // Called when an ad has loaded.
      ad.adEventCallback =
        object : InterstitialAdEventCallback {
        }
      interstitialAd = ad
    }

    override fun onAdFailedToLoad(loadAdError: LoadAdError) {
      // Called when ad fails to load.
    }
  }
)

Java

InterstitialAd.load(
    new AdRequest.Builder("AD_UNIT_ID").build(),
    new AdLoadCallback<InterstitialAd>() {
      @Override
      public void onAdLoaded(@NonNull InterstitialAd ad) {
        // Called when an ad has loaded.
        ad.setAdEventCallback(new InterstitialAdEventCallback() {});
        interstitialAd = ad;
      }

      @Override
      public void onAdFailedToLoad(@NonNull LoadAdError adError) {
        // Called when ad fails to load.
      }
    });

Show an ad

The following examples show an interstitial ad:

Current

Kotlin

interstitialAd?.show(this@InterstitialActivity)

Java

interstitialAd.show(this);
Google Mobile Ads SDK (beta)

Kotlin

interstitialAd?.show(this@InterstitialActivity)

Java

interstitialAd.show(this);