To display Interstitial Ads you have to - just like with banner ads - first create an Ad Unit ID. So sign in on the AdMob website, select + Monetize new app from the menu bar, click on the name of your app and click on new ad unit and this time create an Interstitial ad unit. Copy the Ad Unit ID and go back to the AndroidLauncher class and create another string for the interstitial ad unit id (right next to where you created your banner ad unit string):
Interstitial Ads function slightly different compared to Banner Ads. One thing is that Interstitial ads will be displayed full screen and thus I won'tneed to run them in a seperate activity like with banner ads.
I also want my ad to be displayed until the user touches an exit button on the ad.
To implement this functionality the Google Play Services library has a class called InterstitialAd which has an AdListener method that we will use. What the adListener does is basically to wait for the user to close the ad by touching the button before going on to run the code that we tell the AdListener to run on closing the ad.
Declare a variable to refer to Interstitial ad next to where you declared the variable for your AdView:
setupAds() method I have created my bannerAd and set it's adUnitID.
I'm going to do the same for my Interstitial Ad. I also already want to make an adRequest and load my ad into memory so that when I actually want to display the ad
I only have to call the InterstitialAd's show method.
Ad the following lines of code to the
This is all the code necessary to load the Ad (into memory). Now we need a method we can call when we want to show the Interstitial Ad:
Ad the following line of code to the AdsController interface:
This time we want to pass some code that we put in a Runnable object to the
showInterstitialAd method in AndroidLauncher, because in this method we're also going to define
which code - that is the code contained in this Runnable object - is going to be run after the user closes the ad.
In AndroidLauncher ad the following lines of code to the
Inside the method we see that - just like with banner ads - we define some code that will be run on the UI Thread. In the run method there first is a check to see
if there is any code to run (defined in the Runnable then that's passed to the method) for the AdListener when the Ad closes. If this is true (then != null) then an AdListener is set to the interstitialAd which runs the code specified in
plus creates a new AdRequest when the Ad gets closed. And finally the intestitialAd is shown with
Now all that remains to do is call the method from our Game class, specify what is going to happen after we close the Ad and check on our device if everything is working
Add the following lines of code to the render method of MyGdxGame.java (if you add this code in the create method the ad will most likely not be shown because it hasn't had enough time to get downloaded from AdMob:
Now run your Android project and check if it's working. Before you can run the project you should probably ad
as a parameter to the
showInterstitialAds() method of your DummyAdsController.
You might have noticed in the logCat output that when the ad couldn't be shwon "Interstitial ad not (yet) loaded" got printed a couple of times before the ad got displayed.
A better way to check if your ad has finished loading is to use InterstitialAd class's
isLoaded() method. But I'll leave that up to you to figure out if and how you want to use this method.
Here's a link to the AdMob Android Guides:
And that's it! Well done. You now know how to display both Banner and Interstitial ad.
If you thought this tutorial was valuable for you please leave a comment, share the tutorial and/or like us on Facebook