Load AdMob Interstitial Ads in Apps

  • Post comments:0 Comments

Interstitial Ads are full screen Ads. Usually, it appears between activities and during pauses between levels in the game. Interstitial Ads are displayed full screen in mobile.

We will practically understand AdMob test Ads. In the previous post, we already know about AdMob banner Ads. There has to add the same dependency and app id inside the manifest.

You know that most of the apps show interstitial Ads on button click, some apps show interstitial Ads automatically during launch activity or between activities.

Nowadays, Most apps put interstitial video Ads on click buttons.

So friends, today we are discussing different types of AdMob interstitial Ads with examples.

AdMob Interstitial Ads with Examples

Here, we are discussing about interstitial Ads. You should know about it.

Such as

  • Full screen interstitial Ads,
  • Interstitial video Ads,
  •  And automatically show interstitial Ads on starting any activity.

Generally we are discussing three types of Interstitial Ads here.

Show interstitial Ads when starting an activity.

Most of these types of interstitial Ads are displayed at a specific time of launch activity or other current activity.

Show interstitial ad on click button

When the clicks on the button the interstitial ad shows up and there has the option to tap on the ad and close it and return to the app.

Show interstitial video ad on click button

Most apps integrate interstitial video Ads. When button click show interstitial video ad, then close button appears after some time there is time of about 5 seconds later you can terminate Ad or you can go by clicking on Ad.

AdMob Interstitial Ad Example

Let us discuss about interstitial Ads step by step with proper example.

Step 1:

Start a new project in android studio

Step 2:

After gradle is finished, open gradle.build(:app) and add Google Mobile Ads SDK 19.7.0 or higher, Which was previously shown in banner Ads. Or also add app id inside manifest.

gradle.build and add the below dependency.

implementation 'com.google.android.gms:play-services-ads:20.4.0'

In the manifest file, add test app Id metadata and internet permission.

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

Metadata between <application>and </application>

        <meta-data
            android:name="com.google.android.gms.ads.APPLICATION_ID"
            android:value="@string/appId"/>

Step 3:

First we want to display interstitial Ads before the launch activity. So we don’t need xml UI design.

Go to the main activity and call the interstitial static load method and pass in an interstitial AdLoadCallbac to get the loaded ad and possible errors.

First of Ads call MobileAds.intitialize().

MobileAds.initialize(this, new OnInitializationCompleteListener() {
            @Override
            public void onInitializationComplete(InitializationStatus initializationStatus) {
            }
        });

To load interstitial ad

InterstitialAd.load(this,getString(R.string.pubId), adRequest,
                new InterstitialAdLoadCallback() {
                    @Override
                    public void onAdLoaded(@NonNull InterstitialAd interstitialAd) {
                        // The mInterstitialAd reference will be null until
                        // an ad is loaded.
                        mInterstitialAd = interstitialAd;
                        Log.i(TAG, "onAdLoaded");
                        LoadAds();
                    }
 @Override
                    public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
                        // Handle the error
                        Log.i(TAG, loadAdError.getMessage());
                        mInterstitialAd = null;
                    }
                });

To display interstitial Ads, use the show() method.

For that create a new method inside onAdLoaded.

private void LoadAds() {
        if (mInterstitialAd != null) {
            mInterstitialAd.show(MainActivity.this);
        } else {
            Log.d("TAG", "The interstitial ad wasn't ready yet.");
        }
    }

The complete code of MainActivity.java file is given below.

MainActivity.java

package com.example.interstitialad;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;

import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.LoadAdError;
import com.google.android.gms.ads.interstitial.InterstitialAd;
import com.google.android.gms.ads.interstitial.InterstitialAdLoadCallback;

import static android.content.ContentValues.TAG;

public class MainActivity extends AppCompatActivity {
    private InterstitialAd mInterstitialAd;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        AdRequest adRequest = new AdRequest.Builder().build();

        InterstitialAd.load(this,getString(R.string.pubId), adRequest,
                new InterstitialAdLoadCallback() {
                    @Override
                    public void onAdLoaded(@NonNull InterstitialAd interstitialAd) {
                        // The mInterstitialAd reference will be null until
                        // an ad is loaded.
                        mInterstitialAd = interstitialAd;
                        Log.i(TAG, "onAdLoaded");
                        LoadAds();
                    }

                    @Override
                    public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
                        // Handle the error
                        Log.i(TAG, loadAdError.getMessage());
                        mInterstitialAd = null;
                    }
                });
    }

    private void LoadAds() {
        if (mInterstitialAd != null) {
            mInterstitialAd.show(MainActivity.this);
        } else {
            Log.d("TAG", "The interstitial ad wasn't ready yet.");
        }
    }
}

Run the project and the output will look like the screenshot below.

Show  AdMob interstitial ad

Show interstitial Ads on button click

If you want to display interstitial ad on button click then modify java and xml file.

Step 1:

Go to activity_main.xml and add button and pass ID.

The code of actiivyt_main.xml is given below.

Activity_main.xml

<?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">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Show Ads"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:id="@+id/ads"/>

</RelativeLayout>

Step 2:

Open MainActivity.java and Initialize button.

Crete a method named showAds();

Add below code inside showAds() method.

private void showAds() {
        AdRequest adRequest = new AdRequest.Builder().build();

        InterstitialAd.load(this,"ca-app-pub-3940256099942544/1033173712", adRequest,
                new InterstitialAdLoadCallback() {
                    @Override
                    public void onAdLoaded(@NonNull InterstitialAd interstitialAd) {
                        // The mInterstitialAd reference will be null until
                        // an ad is loaded.
                        mInterstitialAd = interstitialAd;
                        Log.i(TAG, "onAdLoaded");
                    }

                    @Override
                    public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
                        // Handle the error
                        Log.i(TAG, loadAdError.getMessage());
                        mInterstitialAd = null;
                    }
                });
    }

When interstitial Ads are displayed on button click, so here we use setOnClickListener and inside setOnClickListener we set FullScreenContentCallback handle event.

 mInterstitialAd.setFullScreenContentCallback(new FullScreenContentCallback(){
                   @Override
                   public void onAdDismissedFullScreenContent() {
                       // Called when fullscreen content is dismissed.
                       Log.d("TAG", "The ad was dismissed.");
                       startActivity(new Intent(MainActivity.this,MainActivity2.class));
                       mInterstitialAd=null;
                       showAds();
                   }

                   @Override
                   public void onAdFailedToShowFullScreenContent(AdError adError) {
                       // Called when fullscreen content failed to show.
                       Log.d("TAG", "The ad failed to show.");
                   }

                   @Override
                   public void onAdShowedFullScreenContent() {
                       // Called when fullscreen content is shown.
                       // Make sure to set your reference to null so you don't
                       // show it a second time.
                       mInterstitialAd = null;
                       Log.d("TAG", "The ad was shown.");
                   }
               });

The complete code of MainActivity.java is given below.

MainActivity.java

package com.example.interstitialadsclick;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.google.android.gms.ads.AdError;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.FullScreenContentCallback;
import com.google.android.gms.ads.LoadAdError;
import com.google.android.gms.ads.interstitial.InterstitialAd;
import com.google.android.gms.ads.interstitial.InterstitialAdLoadCallback;

import static android.content.ContentValues.TAG;

public class MainActivity extends AppCompatActivity {
    private InterstitialAd mInterstitialAd;
Button ads;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ads=(Button)findViewById(R.id.ads);
        //create method 
        showAds();
        //button on click show interstitial ads
        ads.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
           if (mInterstitialAd!=null){
               mInterstitialAd.show(MainActivity.this);

               mInterstitialAd.setFullScreenContentCallback(new FullScreenContentCallback(){
                   @Override
                   public void onAdDismissedFullScreenContent() {
                       // Called when fullscreen content is dismissed.
                       Log.d("TAG", "The ad was dismissed.");
                       startActivity(new Intent(MainActivity.this,MainActivity2.class));
                       mInterstitialAd=null;
                       showAds();
                   }

                   @Override
                   public void onAdFailedToShowFullScreenContent(AdError adError) {
                       // Called when fullscreen content failed to show.
                       Log.d("TAG", "The ad failed to show.");
                   }

                   @Override
                   public void onAdShowedFullScreenContent() {
                       // Called when fullscreen content is shown.
                       // Make sure to set your reference to null so you don't
                       // show it a second time.
                       mInterstitialAd = null;
                       Log.d("TAG", "The ad was shown.");
                   }
               });
           }else {
               Log.d("TAG","the interstitial ad wasn't ready at");
           }
            }
        });
    }

    private void showAds() {
        AdRequest adRequest = new AdRequest.Builder().build();

        InterstitialAd.load(this,"ca-app-pub-3940256099942544/1033173712", adRequest,
                new InterstitialAdLoadCallback() {
                    @Override
                    public void onAdLoaded(@NonNull InterstitialAd interstitialAd) {
                        // The mInterstitialAd reference will be null until
                        // an ad is loaded.
                        mInterstitialAd = interstitialAd;
                        Log.i(TAG, "onAdLoaded");
                    }

                    @Override
                    public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
                        // Handle the error
                        Log.i(TAG, loadAdError.getMessage());
                        mInterstitialAd = null;
                    }
                });
    }
}

Step 3:

Create a new empty activity because when the button is clicked the Ads will appear then after close the Ad and should start the second activity.

and add modified strings file and adUinitId. if you want different values inside ⇾strings.xml.

Your interstitial on click button Ads ready.

The interstitials will appear on the button click as in the screenshot below.

Show AdMob interstitial on button click
Show interstitial ads in app on click button

How to show interstitial video Ads?

You must have shown this type of Ad. Interstitial video Ads show up when the button is clicked, in this type interstitial video Ad close button shows 5 seconds later.

If you want to show interstitial video Ads just go to string.xml and change interstitial ad unit id. And run your project.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments