Signup/Sign In

How to add Admob Interstitial Ad in Android App

Posted in Programming   LAST UPDATED: JUNE 10, 2023

    Monetizing mobile applications has become an essential aspect of app development, and integrating ads is a popular revenue-generating strategy. AdMob, Google's mobile advertising platform, offers a wide range of ad formats, including interstitial ads that appear at natural transition points within an app.

    If you're looking to enhance your app's monetization potential, adding AdMob interstitial ads is an effective way to generate revenue while maintaining a seamless user experience.

    In this article, we will learn how to integrate Google Admob Interstitial Ad in our Android app, so before going to the coding part we will first learn some basics and features of Interstitial Ads. We have already covered how to add banner ads in android app.

    What are Interstitial Ads?

    Interstitial ads are full-screen ad that covers the entire screen of the user,it is usually shown when a certain task is completed such as when the user completed the level of the game, or when the user wanted to download the image from the app, or transition between the activities of the app. it totally depends on you when to show the Interstitial Ad

    Feature of Interstitial Ads

    • Show both text ads, video ads, and image ads

    • Support frequency cap means you can decide how many ads are shown to the user daily or hourly

    • Users can interact with the ad as it also contains playable ads

    • The eCPM is high as compared to Banner Ads, so it leads to more revenue

    • support both Mobile phones and Tablets

    • fill rate is maximum

    So let's implement a simple Admob Interstitial Ad in our Android app.

    How to add Admob Interstitial Ad in Android App

    Step 1: Create a new Project

    1. Open Your Android Studio Click on "Start a new Android Studio project"(Learn how to set up Android Studio and create your first Android project)

    2. Choose "Empty Activity" from the project template window and click Next

    3. Enter the App Name, Package name, save location, language(Java/Kotlin, we use Java for this tutorial ), and minimum SDK(we are using API 19: Android 4.4 (KitKat) )

    4. Next, click on the Finish button after filling in the above details

    5. Now wait for the project to finish building.

    Step 2: Adding the Mobile Ads SDK

    To show the ads in our app we have to first implement the Admob sdk in our app, to do so

    Go to Gradle Scripts->build.gradle (Module: app) section and import the below dependencies and click the "sync Now" shown at the top

    dependencies {
         // adding Admob SDK
         implementation 'com.google.android.gms:play-services-ads:19.4.0'
    }

    Now our build.gradle file look like this,

    apply plugin: 'com.android.application'
    
    android {
        compileSdkVersion 29
        buildToolsVersion "29.0.3"
    
        defaultConfig {
            applicationId "com.studytonight.project"
            minSdkVersion 19
            targetSdkVersion 29
            versionCode 1
            versionName "1.0"
    
            testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        }
    
        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
            }
        }
    }
    
    dependencies {
        implementation fileTree(dir: "libs", include: ["*.jar"])
        implementation 'androidx.appcompat:appcompat:1.2.0'
        implementation 'androidx.constraintlayout:constraintlayout:2.0.2'
        testImplementation 'junit:junit:4.12'
        androidTestImplementation 'androidx.test.ext:junit:1.1.2'
        androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
    
        // adding Admob SDK
        implementation 'com.google.android.gms:play-services-ads:19.4.0'
    
    }

    Step 3: Modify AndroidManifest.xml

    Go to app->manifests->AndroidManifest.xml file and add the internet permission to the Android Manifest file

    <manifest>
    <uses-permission android:name= "android.permission.INTERNET"/> 
    </manifest>

    Now we add our AdMob AppId to your in the AndroidManifest file by adding the <meta-data> tag inside the <application> tag

    <manifest>
        <application>
    
               <meta-data
                    android:name= "com.google.android.gms.ads.APPLICATION_ID"
                    android:value= "ca-app-pub-3940256099942544~3347511713"/> 
    
       </application>
    
    </manifest>     

    To show video ads inside the Interstitial ad views, hardware acceleration must be turned on. In Android the hardware acceleration is enabled by default, we can also enable and disable it for the entire or for each activity separately in our Android manifest file as shown below:

    <!-- for the entire app --> 
    <application
        android:hardwareAccelerated="true"></application>
    <!-- for the each activity -->
    <application>
            <activity
               android:hardwareAccelerated="true"> </activity>
    </application>

    The Complete code of AndroidManifest.xml file is shown below:

    <?xml version= "1.0" encoding= "utf-8"?>
    <manifest xmlns:android= "http://schemas.android.com/apk/res/android"
        package= "com.studytonight.project">
    
        <!-- adding internet permission to show allow app to use internet to load and show ads -->
        <uses-permission android:name= "android.permission.INTERNET"/>
    
        <!-- hardware Acceleration is turned on for the entire app -->
        <application
            android:hardwareAccelerated= "true"
            android:allowBackup= "true"
            android:icon= "@mipmap/ic_launcher"
            android:label= "@string/app_name"
            android:roundIcon= "@mipmap/ic_launcher_round"
            android:supportsRtl= "true"
            android:theme= "@style/AppTheme">
            <activity
                android:screenOrientation="portrait"
                android:name= ".MainActivity">
                <intent-filter>
                    <action android:name= "android.intent.action.MAIN" />
    
                    <category android:name= "android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
    
            <!-- Adding AdMob App Id -->
            <meta-data
                android:name= "com.google.android.gms.ads.APPLICATION_ID"
                android:value= "ca-app-pub-3940256099942544~3347511713"/>
    
        </application>
    
    </manifest>

    Step 4: Modify activity_main.xml

    We will now create 2 Buttons to load and show the Interstitial ads inside a vertical linearLayout:

    <!-- vertical linear layout with 2  button -->
    <LinearLayout
        android:layout_centerInParent= "true"
        android:layout_margin= "16dp"
        android:orientation= "vertical"
        android:layout_width= "match_parent"
        android:layout_height= "wrap_content">
    
        <!-- Simple Buttons to Load Interstitial Ad when clicked -->
        <Button
            android:textSize= "24dp"
            android:layout_margin= "16dp"
            android:id= "@+id/loadInterstitialBtn"
            android:text= "Load Interstitial  Ad"
            android:fontFamily= "serif"
            android:textStyle= "bold"
            android:textColor= "#ffffff"
            android:background= "@color/colorPrimary"
            android:layout_width= "match_parent"
            android:layout_height= "60dp"/>
    
        <!-- Simple Buttons to Show  Interstitial Ad it is is loaded when clicked -->
        <Button
            android:textSize= "24dp"
            android:layout_margin= "16dp"
            android:id= "@+id/showInterstitialBtn"
            android:text= "Show Interstitial  Ad"
            android:fontFamily= "serif"
            android:textStyle= "bold"
            android:textColor= "#ffffff"
            android:background= "@color/colorPrimary"
            android:layout_width= "match_parent"
            android:layout_height= "60dp"/>
    
    </LinearLayout>

    The complete code of activity_main.xml is shown below:

    <?xml version=  "1.0" encoding=  "utf-8"?>
    <RelativeLayout xmlns:android=  "http://schemas.android.com/apk/res/android"
        xmlns:app=  "http://schemas.android.com/apk/res-auto"
        xmlns:tools=  "http://schemas.android.com/tools"
        android:layout_width=  "match_parent"
        android:layout_height=  "match_parent"
        tools:context=  ".MainActivity">
    
        <!-- vertical linear layout with 2  button -->
        <LinearLayout
            android:layout_centerInParent= "true"
            android:layout_margin= "16dp"
            android:orientation= "vertical"
            android:layout_width= "match_parent"
            android:layout_height= "wrap_content">
    
            <!-- Simple Buttons to Load Interstitial Ad when clicked -->
            <Button
                android:textSize= "24dp"
                android:layout_margin= "16dp"
                android:id= "@+id/loadInterstitialBtn"
                android:text= "Load Interstitial  Ad"
                android:fontFamily= "serif"
                android:textStyle= "bold"
                android:textColor= "#ffffff"
                android:background= "@color/colorPrimary"
                android:layout_width= "match_parent"
                android:layout_height= "60dp"/>
    
            <!-- Simple Buttons to Show  Interstitial Ad it is is loaded when clicked -->
            <Button
                android:textSize= "24dp"
                android:layout_margin= "16dp"
                android:id= "@+id/showInterstitialBtn"
                android:text= "Show Interstitial  Ad"
                android:fontFamily= "serif"
                android:textStyle= "bold"
                android:textColor= "#ffffff"
                android:background= "@color/colorPrimary"
                android:layout_width= "match_parent"
                android:layout_height= "60dp"/>
    
        </LinearLayout>
    
    </RelativeLayout>

    Step 5: MainActivity.java file

    First, we have to import the library inside the ActivityMain.java

    //library for Button, View and Toast
    import android.view.View;
    import android.widget.Button;
    import android.widget.Toast;
    
    // important library for Google adMob
    import com.google.android.gms.ads.AdListener;
    import com.google.android.gms.ads.AdRequest;
    import com.google.android.gms.ads.InterstitialAd;
    import com.google.android.gms.ads.LoadAdError;
    import com.google.android.gms.ads.MobileAds;
    import com.google.android.gms.ads.initialization.InitializationStatus;
    import com.google.android.gms.ads.initialization.OnInitializationCompleteListener;

    Now inside the MainActivity class, we create an object of InterstitialAd, Button (loadAdBtn, showAdBtn)

    //creating Object of InterstitialAd
    private InterstitialAd interstitialAd;
    
    
    //creating Object of Buttons
    private Button loadAdBtn;
    private Button showAdBtn;

    Now Inside the onCreate we initialize the MobileAds and show a simple toast message when initialization is completed using the below code:

    //initializing the Google Admob SDK
    MobileAds.initialize(this, new OnInitializationCompleteListener() {
        @Override
        public void onInitializationComplete(InitializationStatus initializationStatus) {
    
            //Showing a simple Toast Message to the user when The Google AdMob Sdk Initialization is Completed
    
          Toast.makeText (MainActivity.this, "AdMob Sdk Initialize "+ initializationStatus.toString(), Toast.LENGTH_LONG).show();
    
        }
    });

    Next, we will initialize the object inside the oncreate method after initializing the SDK

    //Initializing the InterstitialAd  objects
    interstitialAd = new InterstitialAd (MainActivity.this) ;
    
    // Initializing the Button  objects to their respective views from activity_main.xml file
    loadAdBtn = (Button) findViewById(R.id.loadInterstitialBtn);
    showAdBtn = (Button) findViewById(R.id.showInterstitialBtn);
    

    Now we set the ad ID to the interstitialAd, for this tutorial, we are using only test ads that are provided by Google Admob, and if you want to earn revenue you can change it to your own ad id:

    //setting Ad Unit Id to the interstitialAd
    interstitialAd.setAdUnitId ( "ca-app-pub-3940256099942544/1033173712" ) ;

    Now we will create a simple method loadInterstitialAd() to load the Interstitial Ad inside MainActivity class as shown below:

    private void loadInterstitialAd()
    {
        // Creating  a Ad Request
        AdRequest adRequest = new AdRequest.Builder().build() ;
    
        // load Ad with the Request
        interstitialAd.loadAd(adRequest) ;
    
        // Showing a simple Toast message to user when an ad is Loading
        Toast.makeText ( MainActivity.this, "Interstitial Ad is loading ", Toast.LENGTH_LONG).show() ;
    }

    Next, we will create one more method private void showInterstitialAd() to show the Interstitial Ad to the user if it is loaded and if it is not loaded we will load the ad using the above method loadInterstitialAd() as shown below:

    private void showInterstitialAd()
    {
        if ( interstitialAd.isLoaded() )
        {
            //showing the ad Interstitial Ad if it is loaded
            interstitialAd.show() ;
    
            // Showing a simple Toast message to user when an Interstitial ad is shown to the user
            Toast.makeText ( MainActivity.this, "Interstitial is loaded and showing ad  ", Toast.LENGTH_LONG) .show();
        }
        else
        {
            //Load the Interstitial ad if it is not loaded
            loadInterstitialAd() ;
    
            // Showing a simple Toast message to user when an ad is not loaded
            Toast.makeText ( MainActivity.this, "Interstitial Ad is not Loaded ", Toast.LENGTH_LONG).show() ;
        }
    }

    Now we create a click listener inside onCreate method so that the above function is executed when the buttons are clicked, which we have created in our activity_main.xml file:

    //click listeners for buttons
    loadAdBtn.setOnClickListener( new View.OnClickListener() {
        @Override
        public void onClick( View view) {
            //calling the loadInterstitialAd method to load Interstitial Ad
            loadInterstitialAd() ;
        }
    });
    
    showAdBtn.setOnClickListener( new View.OnClickListener() {
        @Override
        public void onClick( View view) {
            //calling the showInterstitialAd method to show Interstitial Ad
            showInterstitialAd() ;
        }
    });

    To know the status of the Interstitial ad we will add the AdListener to our interstitialAd object inside the onCreate method and we will call the loadInterstitialAd() when the ad is closed,

    // creating different AdListener for Interstitial Ad with some Override methods
    
    interstitialAd.setAdListener( new AdListener() {
        @Override
        public void onAdLoaded() {
    
            // Showing a simple Toast message to user when an ad is loaded
            Toast.makeText ( MainActivity.this, "Interstitial Ad is Loaded", Toast.LENGTH_LONG).show() ;
        }
    
        @Override
        public void onAdFailedToLoad( LoadAdError adError) {
    
            // Showing a simple Toast message to user when and ad is failed to load
            Toast.makeText ( MainActivity.this, "Interstitial Ad Failed to Load ", Toast.LENGTH_LONG).show() ;
        }
    
        @Override
        public void onAdOpened() {
    
            // Showing a simple Toast message to user when an ad opens and overlay and covers the device screen
            Toast.makeText ( MainActivity.this, "Interstitial Ad Opened", Toast.LENGTH_LONG).show() ;
        }
    
        @Override
        public void onAdClicked() {
    
            // Showing a simple Toast message to user when a user clicked the ad
            Toast.makeText ( MainActivity.this, "Interstitial Ad Clicked", Toast.LENGTH_LONG).show() ;
        }
    
        @Override
        public void onAdLeftApplication() {
    
            // Showing a simple Toast message to user when the user left the application
            Toast.makeText ( MainActivity.this, "Interstitial Ad Left the Application", Toast.LENGTH_LONG).show() ;
        }
    
        @Override
        public void onAdClosed() {
    
            //loading new interstitialAd when the ad is closed
            loadInterstitialAd() ;
    
            // Showing a simple Toast message to user when the user interacted with ad and got the other app and then return to the app again
            Toast.makeText ( MainActivity.this, "Interstitial Ad is Closed", Toast.LENGTH_LONG).show() ;
    
        }
    });

    Overridable methods of AdListener:

    Here is a list of overridable methods of AdListener:

    public void onAdLoaded()

    The onAdLoaded the method is executed when an ad is loaded. we can update the UI or do other stuff we the ad is loaded such as give the user more lies or coins in case of game or provide premium content to the user for showing an ad
    public void onAdFailedToLoad(LoadAdError adError)

    The onAdFailedToLoad method is the only one which includes a parameter. The adError parameter is of type LoadAdError that describes which type of error is occurred
    public void onAdOpened() 

    The onAdOpened() method is executed when the user clicks on the Interstitial ad and the ad is opened
    public void onAdClicked()

    The onAdClicked() method is executed when the user clicks on the Interstitial ad
    public void onAdLeftApplication()

    This method is executed after onAdOpened(), when a user click to opens another app(such as click the ad to install the game or app), backgrounding the currently running app
    public void onAdClosed()

    When the user returns back to the app after viewing an Ad or play store for install app or game, onAdClosed() method is executed

    The completed code of MainActivity.java is shown below:

    package com.studytonight.project;
    
    import androidx.appcompat.app.AppCompatActivity;
    import android.os.Bundle;
    
    //library for Button, View and Toast
    import android.view.View;
    import android.widget.Button;
    import android.widget.Toast;
    
    // important library for Google adMob
    import com.google.android.gms.ads.AdListener;
    import com.google.android.gms.ads.AdRequest;
    import com.google.android.gms.ads.InterstitialAd;
    import com.google.android.gms.ads.LoadAdError;
    import com.google.android.gms.ads.MobileAds;
    import com.google.android.gms.ads.initialization.InitializationStatus;
    import com.google.android.gms.ads.initialization.OnInitializationCompleteListener;
    
    public class MainActivity extends AppCompatActivity {
    
        //creating Object of InterstitialAd
        private InterstitialAd interstitialAd;
    
        //creating Object of Buttons
        private Button loadAdBtn;
        private Button showAdBtn;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            //initializing the Google Admob SDK
            MobileAds.initialize (this, new OnInitializationCompleteListener() {
                @Override
                public void onInitializationComplete(InitializationStatus initializationStatus) {
    
                    //Showing a simple Toast Message to the user when The Google AdMob Sdk Initialization is Completed
    
                  Toast.makeText (MainActivity.this, "AdMob Sdk Initialize "+ initializationStatus.toString(), Toast.LENGTH_LONG).show();
    
                }
            });
    
    
            //Initializing the InterstitialAd  objects
            interstitialAd = new InterstitialAd (MainActivity.this) ;
    
            // Initializing the Button  objects to their respective views from activity_main.xml file
            loadAdBtn = (Button) findViewById(R.id.loadInterstitialBtn);
            showAdBtn = (Button) findViewById(R.id.showInterstitialBtn);
    
            //setting Ad Unit Id to the interstitialAd
            interstitialAd.setAdUnitId ( "ca-app-pub-3940256099942544/1033173712" ) ;
    
            //click listeners for buttons
            loadAdBtn.setOnClickListener( new View.OnClickListener() {
                @Override
                public void onClick( View view) {
                    //calling the loadInterstitialAd method to load Interstitial Ad
                    loadInterstitialAd() ;
                }
            });
    
            showAdBtn.setOnClickListener( new View.OnClickListener() {
                @Override
                public void onClick( View view) {
                    //calling the showInterstitialAd method to show Interstitial Ad
                    showInterstitialAd() ;
                }
            });
    
            // creating different AdListener for Interstitial Ad with some Override methods
    
            interstitialAd.setAdListener( new AdListener() {
                @Override
                public void onAdLoaded() {
    
                    // Showing a simple Toast message to user when an ad is loaded
                    Toast.makeText ( MainActivity.this, "Interstitial Ad is Loaded", Toast.LENGTH_LONG).show() ;
    
                }
    
                @Override
                public void onAdFailedToLoad( LoadAdError adError) {
    
                    // Showing a simple Toast message to user when and ad is failed to load
                    Toast.makeText ( MainActivity.this, "Interstitial Ad Failed to Load ", Toast.LENGTH_LONG).show() ;
    
                }
    
                @Override
                public void onAdOpened() {
    
                    // Showing a simple Toast message to user when an ad opens and overlay and covers the device screen
                    Toast.makeText ( MainActivity.this, "Interstitial Ad Opened", Toast.LENGTH_LONG).show() ;
    
                }
    
                @Override
                public void onAdClicked() {
    
                    // Showing a simple Toast message to user when a user clicked the ad
                    Toast.makeText ( MainActivity.this, "Interstitial Ad Clicked", Toast.LENGTH_LONG).show() ;
    
                }
    
                @Override
                public void onAdLeftApplication() {
    
                    // Showing a simple Toast message to user when the user left the application
                    Toast.makeText ( MainActivity.this, "Interstitial Ad Left the Application", Toast.LENGTH_LONG).show() ;
    
                }
    
                @Override
                public void onAdClosed() {
    
                    //loading new interstitialAd when the ad is closed
                    loadInterstitialAd() ;
    
                    // Showing a simple Toast message to user when the user interacted with ad and got the other app and then return to the app again
                    Toast.makeText ( MainActivity.this, "Interstitial Ad is Closed", Toast.LENGTH_LONG).show() ;
    
                }
            });
        }
    
        private void loadInterstitialAd()
        {
            // Creating  a Ad Request
            AdRequest adRequest = new AdRequest.Builder().build() ;
    
            // load Ad with the Request
            interstitialAd.loadAd(adRequest) ;
    
            // Showing a simple Toast message to user when an ad is Loading
            Toast.makeText ( MainActivity.this, "Interstitial Ad is loading ", Toast.LENGTH_LONG).show() ;
    
        }
    
        private void showInterstitialAd()
        {
            if ( interstitialAd.isLoaded() )
            {
                //showing the ad Interstitial Ad if it is loaded
                interstitialAd.show() ;
    
                // Showing a simple Toast message to user when an Interstitial ad is shown to the user
                Toast.makeText ( MainActivity.this, "Interstitial is loaded and showing ad  ", Toast.LENGTH_LONG).show() ;
    
            }
            else
            {
                //Load the Interstitial ad if it is not loaded
                loadInterstitialAd() ;
    
                // Showing a simple Toast message to user when an ad is not loaded
                Toast.makeText ( MainActivity.this, "Interstitial Ad is not Loaded ", Toast.LENGTH_LONG).show() ;
    
            }
    
        }
    }

    Output

    In the below snapshots, you can see how the Interstitial Ad will look in the Android application.

    AdMob Interstitial Ad

    When Ad is shown to the user,

    Admob Interstitial

    Conclusion

    Integrating AdMob interstitial ads into your Android app opens up a world of revenue opportunities while ensuring a smooth and engaging user experience.

    By strategically placing ads at natural transition points within your app, you can capture users' attention and generate revenue without interrupting their overall experience. AdMob's advanced targeting capabilities and powerful analytics provide valuable insights into ad performance, allowing you to optimize your monetization strategy.

    In just 5 simple steps we have integrated and shown yous the basic example for creating a Google Admob Interstitial Ad. If you face any issues while doing this, please share them in the comment section below and we will be happy to help.

    Frequently Asked Questions(FAQs)

    1. What are interstitial ads?

    Interstitial ads are full-screen ads that appear at natural transition points within an app, such as between game levels or during app navigation. They offer an immersive and impactful advertising experience, capturing users' attention without disrupting the app's flow.

    2. How do I integrate AdMob interstitial ads into my Android app?

    To integrate AdMob interstitial ads into your Android app, you need to follow a few steps: create an AdMob account, set up an ad unit, implement the AdMob SDK into your app, load and display interstitial ads at appropriate moments, and handle ad events and callbacks.

    3. Can I control the frequency of interstitial ads in my app?

    Yes, you have control over the frequency of interstitial ads in your app. AdMob provides options to set ad display intervals, implement frequency capping, and define ad load thresholds. It's important to strike a balance between monetization and user experience to ensure ads are not too intrusive.

    4. How can I ensure interstitial ads are displayed at the right moments?

    Displaying interstitial ads at the right moments is crucial for maximizing user engagement and revenue. Consider integrating interstitial ads during natural breakpoints, such as between activities or levels, when users are more receptive to ads. You can also utilize event triggers or user actions to determine the optimal timing for ad display.

    5. Are there any design considerations for interstitial ads?

    Designing interstitial ads that seamlessly blend with your app's UI is essential. AdMob provides guidelines and best practices for ad design, including adhering to brand and content policies, maintaining a cohesive visual experience, and ensuring smooth transitions between the ad and app content. Customization options allow you to match ad styles to your app's aesthetic while maintaining a consistent user experience.

    You may also like:

    About the author:
    K S Lohan is an accomplished author and expert in technical writing on Android language and develop android App. He is highly skilled at communicating complex programming concepts in a clear and concise manner.
    Tags:adsadmob
    IF YOU LIKE IT, THEN SHARE IT
     

    RELATED POSTS