Admob with Xamarin Android Part 2: InterstitialAd
Interested in Interstital ads, but not banner? Thats ok, but I recommend your read my first post about banner ads. The first steps, installing Google Play Services, altering your permissions, adding to your manifests files, and reviewing my github demo are located in that tutorial.
The very basic amount of code to do Interstitial Ad’s are below
The problem with the above code is it tries to show the ad right away, but the ad may not be loaded already. The unique way interstitial ads work, are you must call
Show(), after the banner is downloaded. So we must have an event listener that hears when the ad is loaded, before we can show it.
Doing things better
Back to my github code, the Adwrapper class contains build methods for full page ad aka Interstitial.
The custom build extension method works the same as the banner ad method, and has not been customized in this template.
The InterstitialAd class does not come with native event listeners. To implement listeners you must create a class that inherits from AdListner and then define the event listeners. We will want to do this so we can show the ad once it is downloaded to the phone, and not have to keep checking to see if it has downloaded. The demo code AdEventListener.cs file in the ad folder of AndroidPhone project contains an implementaiton of this.
Once you have these componants in place your activity should end up looking like this.
To walk you through the code…after
SetContentView()….We make a full page Ad (and pass it our AD id), we create an event listener based on the ad listener class. We set the event listener to trigger an annonymous function that will show the ad. we make the Ad’s event listener to be the event listener we made, then we run CustomBuild which builds the Ad and starts loading the Ad. Once the ad loads it will call the event handler, and Boom! Show the ad to the user.
Obviously this is a basic implementation, and showing the ad when the app first starts may, or may not be the best stratergy for You.