Open In App

How to Integrate Facebook Audience Network (FAN) Interstitial Ads in Android?

In order to earn money from the Android app or game, there are many ways such as in-App Purchases, Sponsorship, Advertisements, and many more. But there is another popular method to earn money from the Android app is by integrating a third party advertisement e.g known as Facebook Audience Network (FAN). Facebook Audience Network is designed to help monetize with the user experience in mind. By using high-value formats, quality ads, and innovative publisher tools it helps to grow the business while keeping people engaged.

Why Facebook Audience Network?

Formats of Facebook Audience Network

There are mainly five types of flexible, high-performing format available in Facebook Audience Network



In this article let’s integrate Facebook Audience Network Interstitial ads in the Android app.

Interstitial Ad: Interstitial ad is a full-screen ad that covers the whole UI of the app. The eCPM (Effective Cost Per Mille) of Interstitial ads are relatively higher than banner ads and also leads to higher CTR(Click Through Rate) which results in more earning from the app.



Approach

Step 1: Creating a New Project

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that choose Java as language though we are going to implement this project in Java language.

Step 2: Before going to the coding section first do some pre-task




<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimary">#0F9D58</color>
    <color name="colorPrimaryDark">#0F9D58</color>
    <color name="colorAccent">#05af9b</color>
</resources>

 implementation ‘com.facebook.android:audience-network-sdk:5.+’

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

Step 3: Designing the UI 

In the activity_main.xml file add only one Button, so whenever the user clicks the Button the Rewarded video ad will be played.




<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
  
    <!-- Button to Show Interstitial Ad By Clicking it -->
    <Button
        android:id="@+id/showInterBtn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_margin="8dp"
        android:background="@color/colorPrimary"
        android:padding="16dp"
        android:text="Show  Interstitial "
        android:textColor="#ffff"
        android:textSize="20dp" />
  
</RelativeLayout>

Step 4: Working with MainActivity.java file

// Creating an object of Button class

Button showInterstitialBtn;

// link those objects with their respective id’s that we have given in activity_main.xml file 

showInterstitialBtn=(Button)findViewById(R.id.showInterBtn);

// initializing the Audience Network SDK

AudienceNetworkAds.initialize(this);

 // creating object of InterstitialAd

 InterstitialAd fbInterstitialAd;

 private void showInterstitial()

    { 

        // initializing InterstitialAd Object

        // InterstitialAd Constructor Takes 2 Arguments

        // 1)Context

        // 2)Placement Id

        fbInterstitialAd = new InterstitialAd(this, “IMG_16_9_APP_INSTALL#YOUR_PLACEMENT_ID”);

        // loading Ad

        fbInterstitialAd.loadAd();       

   }

Note: Replace “IMG_16_9_APP_INSTALL#YOUR_PLACEMENT_ID” with your own placement id to show real ads.

 void showInterstitial()

   {

       // Checking If Ad is Loaded or Not

       if(fbInterstitialAd.isAdLoaded())

       {

           // showing Ad

           fbInterstitialAd.show();

       }

      else

      {

          // Loading Ad If Ad is Not Loaded

         fbInterstitialAd.loadAd();

       }

 }

  // click listener to show Interstitial  Ad

 showInterstitialBtn.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View view) {

                showInterstitial();

            }

  });

 // Interstitial AdListener

 fbInterstitialAd.setAdListener(new InterstitialAdListener() {

            @Override

            public void onInterstitialDisplayed(Ad ad) {

                // Showing Toast Message

                Toast.makeText(MainActivity.this, “onInterstitialDisplayed”, Toast.LENGTH_SHORT).show();

            }

            @Override

            public void onInterstitialDismissed(Ad ad) {

                // Showing Toast Message

                Toast.makeText(MainActivity.this, “onInterstitialDismissed”, Toast.LENGTH_SHORT).show();

            }

            @Override

            public void onError(Ad ad, AdError adError) {

                // Showing Toast Message

                Toast.makeText(MainActivity.this, “onError”, Toast.LENGTH_SHORT).show();

            }

            @Override

            public void onAdLoaded(Ad ad) {

                // Showing Toast Message

                Toast.makeText(MainActivity.this, “onAdLoaded”, Toast.LENGTH_SHORT).show();

            }

            @Override

            public void onAdClicked(Ad ad) {

                // Showing Toast Message

                Toast.makeText(MainActivity.this, “onAdClicked”, Toast.LENGTH_SHORT).show();

            }

            @Override

            public void onLoggingImpression(Ad ad) {

                // Showing Toast Message

                Toast.makeText(MainActivity.this, “onLoggingImpression”, Toast.LENGTH_SHORT).show();

            }

     });




package org.geeksforgeeks.project;
  
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
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 {
  
    // Creating a object of Button class
    Button showInterstitialBtn;
    // creating object of InterstitialAd
    InterstitialAd fbInterstitialAd;
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
  
        // link those objects with their respective id's that we have given in activity_main.xml file
        showInterstitialBtn = (Button) findViewById(R.id.showInterBtn);
  
  
        // initializing the Audience Network SDK
        AudienceNetworkAds.initialize(this);
  
        loadInterstitial();
  
        // click listener to show Interstitial  Ad
        showInterstitialBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showInterstitial();
            }
        });
    }
  
    void showInterstitial() {
        // Checking If Ad is Loaded or Not
        if (fbInterstitialAd.isAdLoaded()) {
            // showing Ad
            fbInterstitialAd.show();
        } else {
            // Loading Ad If Ad is Not Loaded
            fbInterstitialAd.loadAd();
        }
    }
  
    private void loadInterstitial() {
        // initializing InterstitialAd Object
        // InterstitialAd Constructor Takes 2 Arguments
        // 1)Context
        // 2)Placement Id
        fbInterstitialAd = new InterstitialAd(this, "IMG_16_9_APP_INSTALL#YOUR_PLACEMENT_ID");
  
        // Interstitial AdListener
        fbInterstitialAd.setAdListener(new InterstitialAdListener() {
            @Override
            public void onInterstitialDisplayed(Ad ad) {
                // Showing Toast Message
                Toast.makeText(MainActivity.this, "onInterstitialDisplayed", Toast.LENGTH_SHORT).show();
            }
  
            @Override
            public void onInterstitialDismissed(Ad ad) {
                // Showing Toast Message
                Toast.makeText(MainActivity.this, "onInterstitialDismissed", Toast.LENGTH_SHORT).show();
            }
  
            @Override
            public void onError(Ad ad, AdError adError) {
                // Showing Toast Message
                Toast.makeText(MainActivity.this, "onError", Toast.LENGTH_SHORT).show();
            }
  
            @Override
            public void onAdLoaded(Ad ad) {
                // Showing Toast Message
                Toast.makeText(MainActivity.this, "onAdLoaded", Toast.LENGTH_SHORT).show();
            }
  
            @Override
            public void onAdClicked(Ad ad) {
                // Showing Toast Message
                Toast.makeText(MainActivity.this, "onAdClicked", Toast.LENGTH_SHORT).show();
            }
  
            @Override
            public void onLoggingImpression(Ad ad) {
                // Showing Toast Message
                Toast.makeText(MainActivity.this, "onLoggingImpression", Toast.LENGTH_SHORT).show();
            }
        });
  
        // loading Ad
        fbInterstitialAd.loadAd();
    }
}

Output: Run on Emulator


Article Tags :