Create Admob Interstitial Ad on Android

Tiempo de lectura: 2 minutos

Today we’re going to learn how to implement an Interstitial ad on Android using Java.

First, we’ll implement in our build.gradle:

implementation ‘com.google.android.gms:play-services-ads:23.0.0’

Then, we need to initialize the ads in our MainActivity:

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

And add in the manifest (below </application>):

<meta-data
android:name=”com.google.android.gms.ads.AD_MANAGER_APP”
android:value=”true” />

And also inside <application> </application>:

&lt;meta-data
    android:name="com.google.android.gms.ads.APPLICATION_ID"
    android:value="YOUR_ADMOB_APP_ID" />

Replace YOUR_ADMOB_APP_ID with your Admob APP id.

Now let’s create our interstitial ad:

private InterstitialAd mInterstitialAd;

public void loadInterstitialAd() {
try {
AdRequest adRequest = new AdRequest.Builder().build();

    InterstitialAd.load(this, "your_admob_interstitial_ad_id", 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");
                    if (mInterstitialAd != null) {
                        mInterstitialAd.show((Activity) getApplicationContext());
                    } else {
                        Log.d("TAG", "The interstitial ad wasn't ready yet.");
                    }

                     mInterstitialAd.setFullScreenContentCallback(new FullScreenContentCallback() {
        @Override
        public void onAdClicked() {
            // Called when a click is recorded for an ad.
            Log.d("TAG", "Ad was clicked.");
        }

        @Override
        public void onAdDismissedFullScreenContent() {
            // Called when ad is dismissed.
             //Ad loaded, you can perform the action you want
            // Set the ad reference to null so you don't show the ad a second time.
            Log.d("TAG", "Ad dismissed fullscreen content.");
            mInterstitialAd = null;

        }

        @Override
        public void onAdFailedToShowFullScreenContent(AdError adError) {
            // Called when ad fails to show.
            Log.e("TAG", "Ad failed to show fullscreen content.");
            mInterstitialAd = null;
        }

        @Override
        public void onAdImpression() {
            // Called when an impression is recorded for an ad.
            Log.d("TAG", "Ad recorded an impression.");
        }

        @Override
        public void onAdShowedFullScreenContent() {

        }
    });

                }

                @Override
                public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
                    // Handle the error
                    Log.d("ad", loadAdError.toString());
                    mInterstitialAd = null;


                }
            });


} catch (Exception e) {
    Log.e("ad", "failure1");
}

}

In the part of

public void onAdDismissedFullScreenContent() {

//Ad loaded, you can perform the action you want

you can indicate the action you want to perform once the ad is loaded.

Remember to indicate your ad id here: InterstitialAd.load(this, “your_admob_interstitial_ad_id”, adRequest,html
Copy code
adRequest,

Here: «your_admob_interstitial_ad_id» you will indicate your rewarded ad id.

And here you can perform the necessary action with the rewarded ad once viewed: //Perform the necessary actions when the rewarded ad is viewed

With this, we will show the rewarded ad.

Leave a Comment