Implement interstitial ads in Android
Implement interstitial ads in your Android app. Load ad content, display it through Java code, and use a listener to manage ad events.
Read time 1 minuteLast updated 9 days ago
Interstitial ad load example
Use LoadConfiguration.Builder
to construct a load request with the placement ID and mediation metadata, optionally including ad markup for bidding. Pass the built configuration to along with a . The callback receives the loaded instance on success, or a on failure. Retain the ad instance, as it's required to show the ad later.
Ads expire a set time after they load if they aren't shown. Set on the loaded ad instance to detect this and clear your reference. Calling on an expired ad fails. Do this in the same block where you store the ad, then use the callback to discard the stale reference and trigger a new call so a valid ad is ready the next time you need one.
InterstitialAd.load(loadConfig, (interstitialAd, error) -> { if (interstitialAd != null) { // Ad loaded successfully, ready to be shown this.interstitialAd = interstitialAd; interstitialAd.setOnAdExpired(expiredAd -> { this.interstitialAd = null; // reload here }); } else { // Handle load error }});
InterstitialAd.load(loadConfig, { interstitialAd, error -> if (interstitialAd != null) { // Ad loaded successfully, ready to be shown this.interstitialAd = interstitialAd interstitialAd.onAdExpired = AdExpiredListener { _ -> this.interstitialAd = null // reload here } } else { // Handle load error }})
Interstitial ad show example
Call on the retained instance, passing the activity, a , and an . The listener provides callbacks for each stage of the ad's lifecycle: when the ad displays, when the user interacts with it, when it closes, and if an error occurs — with the error details available via the argument.
loadedInterstitialAd.show(activity, new ShowConfiguration.Builder().build(), new InterstitialShowListener() { @Override public void onStarted(@NonNull InterstitialAd ad) { /* displayed */ } @Override public void onClicked(@NonNull InterstitialAd ad) { /* clicked */ } @Override public void onCompleted(@NonNull InterstitialAd ad, @NonNull ShowFinishState state) { /* hidden */ } @Override public void onFailed(@NonNull InterstitialAd ad, @NonNull UnityAdsError error) { /* failed */ } } );
loadedInterstitialAd.show(activity, ShowConfiguration.Builder().build(), object : InterstitialShowListener { override fun onStarted(ad: InterstitialAd) { /* displayed */ } override fun onClicked(ad: InterstitialAd) { /* clicked */ } override fun onCompleted(ad: InterstitialAd, state: ShowFinishState) { /* hidden */ } override fun onFailed(ad: InterstitialAd, error: UnityAdsError) { /* failed */ }})
Next steps: To improve your implementation, refer to the Implement rewarded ads in Android documentation.