Android App Development

Facebook Interstitial ads in Android

Facebook Interstitial ads
Facebook Interstitial ads

The Audience Network allows you to monetize your Android apps with Facebook Interstitial ads. An interstitial ad is a full-screen ad that you can show in your app. Typically interstitial ads are shown when there is a transition in your app. For example: after finishing a level in a game or after loading a story in a news app.

This Tutorial Helps You to Integrate Facebook Interstitial Ads into your App. You Can Check our tutorial Facebook Banner Ads in Android to Implement Banner Ads in your App.

First Initialize Audience Network in your App. Paste the Below Code in your app’s Application Class

AudienceNetworkAds.initialize(this); 

Now Initialize Interstitial Ads in your MainActivity.java

private InterstitialAd interstitialAd;

Now Instantiate an InterstitialAd Objet

interstitialAd = new InterstitialAd(this, "CAROUSEL_IMG_SQUARE_LINK#YOUR_PLACEMENT_ID");

Don’t Forget to Replace “CAROUSEL_IMG_SQUARE_LINK#YOUR_PLACEMENT_ID” with your real AdUnit.

Now We Start Showing Interstitial Ads. We have to create anĀ InterstitialAdListener, load the Ad and show the Ad immediately it is loaded successfully.

// Create listeners for the Interstitial Ad
        InterstitialAdListener interstitialAdListener = new InterstitialAdListener() {
            @Override
            public void onInterstitialDisplayed(Ad ad) {
                // Interstitial ad displayed callback
                Log.e(TAG, "Interstitial ad displayed.");
            }

            @Override
            public void onInterstitialDismissed(Ad ad) {
                // Interstitial dismissed callback
                Log.e(TAG, "Interstitial ad dismissed.");
            }

            @Override
            public void onError(Ad ad, AdError adError) {
                // Ad error callback
                Log.e(TAG, "Interstitial ad failed to load: " + adError.getErrorMessage());
            }

            @Override
            public void onAdLoaded(Ad ad) {
                // Interstitial ad is loaded and ready to be displayed
                Log.d(TAG, "Interstitial ad is loaded and ready to be displayed!");
                // Show the ad
                interstitialAd.show();
            }

            @Override
            public void onAdClicked(Ad ad) {
                // Ad clicked callback
                Log.d(TAG, "Interstitial ad clicked!");
            }

            @Override
            public void onLoggingImpression(Ad ad) {
                // Ad impression logged callback
                Log.d(TAG, "Interstitial ad impression logged!");
            }
        };

        // For auto play video ads, it's recommended to load the ad
        // at least 30 seconds before it is shown
        interstitialAd.loadAd(
                interstitialAd.buildLoadAdConfig()
                        .withAdListener(interstitialAdListener)
                        .build());

Add the Following Code to release Displayed Ad

@Override
    protected void onDestroy() {
        if (interstitialAd != null) {
            interstitialAd.destroy();
        }
        super.onDestroy();
    }

We Finished Code to Load and Show Interstitial Ad. Now Let’s See the Full Code of MaonActivity.java

MainActivity.java

package com.example.facebookadssampleapp;

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

import androidx.appcompat.app.AppCompatActivity;

import com.facebook.ads.Ad;
import com.facebook.ads.AdError;
import com.facebook.ads.AudienceNetworkAds;
import com.facebook.ads.InterstitialAd;
import com.facebook.ads.InterstitialAdListener;


public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";
    private InterstitialAd interstitialAd;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        AudienceNetworkAds.initialize(this);
        interstitialAd = new InterstitialAd(this, "CAROUSEL_IMG_SQUARE_LINK#YOUR_PLACEMENT_ID");
        // Create listeners for the Interstitial Ad
        InterstitialAdListener interstitialAdListener = new InterstitialAdListener() {
            @Override
            public void onInterstitialDisplayed(Ad ad) {
                // Interstitial ad displayed callback
                Log.e(TAG, "Interstitial ad displayed.");
            }

            @Override
            public void onInterstitialDismissed(Ad ad) {
                // Interstitial dismissed callback
                Log.e(TAG, "Interstitial ad dismissed.");
            }

            @Override
            public void onError(Ad ad, AdError adError) {
                // Ad error callback
                Log.e(TAG, "Interstitial ad failed to load: " + adError.getErrorMessage());
            }

            @Override
            public void onAdLoaded(Ad ad) {
                // Interstitial ad is loaded and ready to be displayed
                Log.d(TAG, "Interstitial ad is loaded and ready to be displayed!");
                // Show the ad
                interstitialAd.show();
            }

            @Override
            public void onAdClicked(Ad ad) {
                // Ad clicked callback
                Log.d(TAG, "Interstitial ad clicked!");
            }

            @Override
            public void onLoggingImpression(Ad ad) {
                // Ad impression logged callback
                Log.d(TAG, "Interstitial ad impression logged!");
            }
        };

        // For auto play video ads, it's recommended to load the ad
        // at least 30 seconds before it is shown
        interstitialAd.loadAd(
                interstitialAd.buildLoadAdConfig()
                        .withAdListener(interstitialAdListener)
                        .build());


    }

    @Override
    protected void onDestroy() {
        if (interstitialAd != null) {
            interstitialAd.destroy();
        }
        super.onDestroy();
    }

}

So Using the Above Code we can now Load and Display Interstitial ads. Facebook Interstitial Ad displays both Image Ads, Video Ads, and CAROUSEL Ads. So you can Earn more Revenue Easily. Just Create AdUnit and it Automatically Configurations to Show Video Ads and CAROUSEL Ads. Integration of Interstitial Ads into your app is Also Very Easy . Check Facebook Audience Network Docs For More Information

Follow the above tutorial and implement interstitial ads in your App. If you Face Any Problem in Implentation, Comment Below your Problem. I’ll try to Solve it.

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button