There are a number of important events in the lifecycle of a banner or
interstitial ad: loading, opening, closing, and so on. The Google Mobile Ads SDK
allows developers to listen for these through the
AdListener
class.
This guide shows you how to use
AdListener
with your banner ads and interstitial ads
as well as some common use cases
for each of its available methods.
To see banner ad events in action, download the Android API Demo app.
Prerequisites
Complete the Get Started in Android Studio guide.
Use AdListener with banner ads
To use an
AdListener
with an
AdView
object, simply call the
setAdListener()
method. Here's an example using an anonymous inner class:
Set up an ad listener
mAdView.setAdListener(new AdListener() { @Override public void onAdLoaded() { // Code to be executed when an ad finishes loading. } @Override public void onAdFailedToLoad(int errorCode) { // Code to be executed when an ad request fails. } @Override public void onAdOpened() { // Code to be executed when an ad opens an overlay that // covers the screen. } @Override public void onAdLeftApplication() { // Code to be executed when the user has left the app. } @Override public void onAdClosed() { // Code to be executed when when the user is about to return // to the app after tapping on an ad. } });
Each of the overridable methods in
AdListener
corresponds to an event in the lifecycle of an ad:
onAdLoaded()
The onAdLoaded()
method is executed when an ad has finished loading. If you want to delay adding
the AdView
to your activity or fragment until you're sure an ad will be loaded, for
example, you can do so here. If you're using a third-party analytics package to
track impressions, this is also where you can place the call to record them.
onAdFailedToLoad()
The onAdFailedToLoad()
method is the only one that includes a parameter. The errorCode
parameter indicates what type of failure occurred. The possible values are
defined as constants in the
AdRequest
class:
- ERROR_CODE_INTERNAL_ERROR
- Something happened internally; for instance, an invalid response was received from the ad server.
- ERROR_CODE_INVALID_REQUEST
- The ad request was invalid; for instance, the ad unit ID was incorrect.
- ERROR_CODE_NETWORK_ERROR
- The ad request was unsuccessful due to network connectivity.
- ERROR_CODE_NO_FILL
- The ad request was successful, but no ad was returned due to lack of ad inventory.
onAdOpened()
For banner ads, this method is invoked when the user taps on an ad. If you're using an analytics package to track clickthroughs, this is a good place to record one.
onAdLeftApplication()
This method is invoked after
onAdOpened()
,
when the activity launched by Android to handle the destination URL has been created.
onAdClosed()
When a user returns to the app after viewing an ad's destination URL, this method is invoked. Your app can use it to resume suspended activities or perform any other work necessary to make itself ready for interaction.
See the AdMob AdListener example for an implementation of the ad listener methods in the Android API Demo app.
Use AdListener with interstitial ads
The InterstitialAd
class provided by the SDK has a
setAdListener()
method identical to the one used for banner ads. You use it in the same way and
with the same events. There are some important things to note for two of the
methods, though:
onAdOpened
- With banner ads, this method is invoked when the user taps on an ad. With interstitial ads, however, it's executed when the ad is displayed, covering the device's screen.
onAdClosed
-
This method is invoked when the interstitial ad is closed due to the user tapping on the close icon or using the back button.
If your app paused its audio output or game loop, this is a great place to resume it. In addition, if you plan to show more than one interstitial ad, you can call
loadAd()
with a new request object here to begin loading the next one.
Further reading
For more details on specific methods, see the class reference for
AdListener
.
FAQ
- My ad units are set up to use mediation. Will I still be able to use
AdListener
? - Yes, mediation adapters are required to send all of these events, and they are forwarded to your app.
- Can I make the method call to show an interstitial ad from within the
onAdLoaded()
method? - It's not a good idea. Interstitial ads are meant to be loaded in advance and shown when the app has reached a natural pause, such as between levels in a game. It's better to let the ad wait until the user is ready, rather than the reverse.