Kotlin Android Google AdMob Interstitial Ads Example
In this tutorial, we implement the Google AdMob Interstitial Ads in our Android application. To place the Google AdMob in Android application, we need to create the Google Ad Unit Id. The complete reference for creating the Google AdMod account and generate the Ad Unit Id is described at Android Google AdMob.
An Interstitials ad is the full-screen ad that covers the entire activity layout. This ad is displayed at a transition point of an activity. To implement the Google AdMob in the Android application, choose Google AdMob Ads Activity and select the Ad format type as Interstitials.
We are also able to place the Google AdMob Ads on other activities such as Blank Activity.
Add the Google ads dependency 'com.google.android.gms:play-services-ads:17.0.0' in the build.gradle file:
Add your UI code in the activity_main.xml. The Button component is used to load the ad.
Add the created ad unit id in the string.xml file.
Add the following code in MainActivity.kt class. To load the ad on the UI, create the instance of InterstitialAd and initialize the ad unit id on InterstitialAd interstitialAd.adUnitId = getString(R.string.interstitial_ad_unit_id).
Override the InterstitialAd listeners onAdLoaded(), onAdFailedToLoad(), onAdClosed. To load the ad on clicking the button, create the instance of AdRequest and load the ad by calling InterstitialAd!!.loadAd(AdRequest).
Add the following code in AndroidManifest.xml file: