Deploy Containers Close to Your Users

This Engineering Education (EngEd) Program is supported by Section.

Instantly deploy containers globally. Section is affordable, simple and powerful.

Get Started for Free.

Working with AdMob Interstitial Google Ads in Android using Kotlin

June 23, 2022

An interstitial ad is a dialogue that pops up on the phone screen when browsing the internet or playing a game.

Its primary role is to advertise a product that the application owner has made a fee contract with the production industry.

Typically, they appear during points of transition in an app’s flow, such as between tasks or game stages.

When an interstitial ad appears in an app, the user can either tap on it to continue to the advertised store or exit and return to the app using a cancel button on the ad.

This tutorial will be a step-by-step guide on how to integrate AdMob interstitial google ads in an Android app.


To follow along, you need:

  1. An account with Google AdMob. Create an account here
  2. Some basic knowledge of Kotlin programming.
  3. A virtual device(emulator) in your IDE for app testing purposes.

Table of contents

Adding an app to the AdMob google ads and creating an ad unit

After creating an account with Admob, sign here to add your app to Google.

On the left side of the Google AdMob home screen, click Apps -> Add app. It will help us to register our app to google Admob.

In the set up new app screen, select the category your app lies in, i.e., Android or iOS. Below the platform, you need to check one of the radio buttons depending on whether the app store supports your app or not.

Next, enter the app name, and add the app. In this case, we will call it Interstitial ads. By now, we have our app id that will be essential when publishing our app to the Play Store.

We need to create an ads unit for our app. Our ad will have this id, and we will use it while writing the code.

Select our app and add an ad unit under the app on the home screen. Then, create an ad unit by selecting the ad format. In our case, we will choose the Interstitial ad unit because that’s our primary objective.

To configure the ad unit, we need to give it a name. Leave the advanced settings as default and select create ad unit to finish up. Click done to finish setting up the app.

Adding required dependencies

To use google ads in our application, we need to add the following dependency.

//Google mobile ads
implementation ''

Setting up the App Id in the manifest file

To set the app id that we generated in the google AdMob ads, navigate to the manifest file in your IDE and add the code below.

The metadata will contain the application id and the values of that id, as shown below:


To get the app id, navigate to your Google AdMob account. In the app -> app setting, copy the id and paste it into the values section.

Working with XML layout

We need to design the UI where we will trigger some events such as click listeners for loading our interstitial ads.

Add the following code in the activity_main.xml layout:

<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat xmlns:android=""

        android:textStyle="bold" />

        android:textColor="@color/black" />

For the button’s background colour, we add a new drawable resource file named custom_button:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="">
        <shape android:shape="rectangle">
            <solid android:color="@color/purple_500" />
            <corners android:radius="16dp" />

Writing Kotlin code for the application

Before we jump to the MainActivity code, we need to ensure that we use Vviewbinding features throughout the whole application.

Add the following code in your app-level build.gradle:

        viewBinding true

Remember to sync the project after adding the code above.

In the MainActivity.kt file, we will be performing several actions to our ad, including loading it to the cache before the user performs any actions and showing the ad after an event is triggered, such as when a button is clicked.

To achieve that, write the following code to your MainActivity.kt file.

The best practice to use interstitial ads during testing is using the test Ad ID (like the one in the activity below).

It is recommended because your AdMob account can be closed during the test attempt. Google assumes that you are using the ad to generate money, and the app is not yet deployed to the Play Store.

class MainActivity : AppCompatActivity() {
    private lateinit var mBinding: ActivityMainBinding
    private var interAd: InterstitialAd? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        mBinding = ActivityMainBinding.inflate(layoutInflater)
        // set on click listener on our button using binding
        mBinding.btnInterstitial.setOnClickListener {

    private fun showIntAd() {
        // First we ensure the Interstitial ad is not nullable

        if (interAd != null) {
            interAd?.fullScreenContentCallback = object : FullScreenContentCallback() {
                override fun onAdFailedToShowFullScreenContent(p0: AdError) {

                override fun onAdShowedFullScreenContent() {
                    //Input your code here

                // When you exit the ad using the cancel button, the next activity is displayed.

                override fun onAdDismissedFullScreenContent() {
                    val intent = Intent(this@MainActivity,

                override fun onAdImpression() {
                    // input your code here

                // What will happen when the ad is clicked

                override fun onAdClicked() {
                    //Input your code here
        } else {
            // If the Ad is not loaded, a toast will be displayed and the intent will help to 
            // navigate to the second activity

            Toast.makeText(baseContext, "Ad was not loaded", Toast.LENGTH_SHORT).show()
            val intent = Intent(this,

    private fun loadIntAd() {
        val adRequest = AdRequest.Builder().build()
        InterstitialAd.load(this, "ca-app-pub-3940256099942544/1033173712", adRequest,
            object : InterstitialAdLoadCallback() {
                override fun onAdFailedToLoad(adError: LoadAdError) {
                    interAd = null

                override fun onAdLoaded(interstitialAd: InterstitialAd) {
                    interAd = interstitialAd

Create the second activity, which we will navigate to when we exit the ad or when the ad is not ready to be displayed. Name the empty activity as NextActivity.

The NextActivity will be displaying a textview with a congratulatory emoji. We need to create a string resource to hold the string, as shown below:

<string name="emoji">Congratulation &#x1F44F;</string>

Add the following code in the next_activity.xml layout to design its UI:

        app:layout_constraintTop_toTopOf="parent" />

In most cases, when the NextActivity.kt is loaded and the user tries to navigate back to the MainActivity.kt, the application exits.

To achieve successful back navigation, we need to tell the NextActivity.kt that it has a parent, which is the MainActivity.kt.

Therefore in the manifest.xml file, we need to write the following code snippet in the activity blocks.


To test the application, you can use a physical device or the emulator installed in your IDE to test the application.


In this tutorial, we have discussed how to create a Google AdMob account, add your app to Google AdMob, as well as create an ad unit id.

We have also discussed how to implement Admob Interstitial Ad in your application and earn money in your account whenever the ad is clicked.

Happy coding!

Peer Review Contributions by: Wanja Mike