Interstitial ads are full-screen ads that cover the app interface. You display these ads at natural transition points in your app, such as between activities or during pauses in game levels.
When your app shows an interstitial ad, your users can either tap on the ad and continue, or close the ad and return to the app.
This guide explains how to integrate interstitial ads into an Android app.
Before you begin
Before you continue, do the following:
- Set up GMA Next-Gen SDK.
- Use the test interstitial ad
unit ID
ca-app-pub-3940256099942544/1033173712.- When building and testing your app, make sure that you use test ads rather than live, production ads. If you don't use the test ad unit ID, Google can suspend your account.
- Before you publish your app, replace this ID with your ad unit ID.
- For details on GMA Next-Gen SDK test ads, see Enable test ads.
Load an ad
The following example loads a single ad:
Kotlin
// Load ads after you initialize MobileAds. InterstitialAd.load( AdRequest.Builder(adUnitId).build(), object : AdLoadCallback<InterstitialAd> { override fun onAdLoaded(ad: InterstitialAd) { // Interstitial ad loaded. interstitialAd = ad } override fun onAdFailedToLoad(adError: LoadAdError) { // Interstitial ad failed to load. Log.e(TAG, "Interstitial ad failed to load: ${adError.message}") interstitialAd = null } }, )
Java
// Load ads after you initialize MobileAds. InterstitialAd.load( new AdRequest.Builder(adUnitId).build(), new AdLoadCallback<InterstitialAd>() { @Override public void onAdLoaded(@NonNull InterstitialAd ad) { // Interstitial ad loaded. interstitialAd = ad; } @Override public void onAdFailedToLoad(@NonNull LoadAdError adError) { // Interstitial ad failed to load. Log.e(TAG, "Interstitial ad failed to load: " + adError.getMessage()); interstitialAd = null; } });
When testing, use the test ad unit ID:
ca-app-pub-3940256099942544/1033173712.
Show the ad
To show an interstitial ad, call theshow method.
The following example displays the ad:
Kotlin
private fun showAd(interstitialAd: InterstitialAd, activity: Activity) { // Show the ad. interstitialAd.show(activity) }
Java
private void showAd(InterstitialAd interstitialAd, Activity activity) { // Show the ad. interstitialAd.show(activity); }
Listen to ad events
Before showing the ad, listen to ad lifecycle events. The following example registers callbacks for ad events:
Kotlin
// Listen for ad events. val ad = interstitialAd if (ad == null) { Log.e(TAG, "Interstitial ad is not ready yet.") return } ad.adEventCallback = object : InterstitialAdEventCallback { override fun onAdShowedFullScreenContent() { // Interstitial ad did show. } override fun onAdDismissedFullScreenContent() { // Interstitial ad did dismiss. interstitialAd = null } override fun onAdFailedToShowFullScreenContent( fullScreenContentError: FullScreenContentError ) { // Interstitial ad failed to show. Log.e(TAG, "Interstitial ad failed to show: ${fullScreenContentError.message}") } override fun onAdImpression() { // Interstitial ad did record an impression. } override fun onAdClicked() { // Interstitial ad did record a click. } }
Java
// Listen for ad events. if (interstitialAd == null) { Log.e(TAG, "Interstitial ad is not ready yet."); return; } interstitialAd.setAdEventCallback( new InterstitialAdEventCallback() { @Override public void onAdShowedFullScreenContent() { // Interstitial ad did show. } @Override public void onAdDismissedFullScreenContent() { // Interstitial ad did dismiss. interstitialAd = null; } @Override public void onAdFailedToShowFullScreenContent( @NonNull FullScreenContentError fullScreenContentError) { // Interstitial ad failed to show. Log.e(TAG, "Interstitial ad failed to show: " + fullScreenContentError.getMessage()); } @Override public void onAdImpression() { // Interstitial ad did record an impression. } @Override public void onAdClicked() { // Interstitial ad did record a click. } });