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?
- Facebook Audience Network is one of the best alternatives for Google Admob to monetize the Android or IOS App.
- Minimum Payout is $100
- Wide Range of Ad Formats
- Maximum Fill Rates
- High eCPM(Effective Cost Per Mille)
- Quality Ads
- Personalized Ads
Formats of Facebook Audience Network
There are mainly five types of flexible, high-performing format available in Facebook Audience Network
- Native: Ads that you design to fit the app, seamlessly
- Interstitial: Full-screen ads that capture attention and become part of the experience.
- Banner: Traditional formats in a variety of placements.
- Rewarded Video: An immersive, user-initiated video ad that rewards users for watching.
- Playables: A try-before-you-buy ad experience allowing users to preview a game before installing.
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
- Go to app -> res -> values -> colors.xml file and set the colors for the app.
colors.xml
<? xml version = "1.0" encoding = "utf-8" ?> < resources > < color name = "colorPrimary" >#0F9D58</ color > < color name = "colorPrimaryDark" >#0F9D58</ color > < color name = "colorAccent" >#05af9b</ color > </ resources > |
- Go to Gradle Scripts -> build.gradle (Module: app) section and import following dependencies and click the “sync now” on the above pop up.
implementation ‘com.facebook.android:audience-network-sdk:5.+’
- Go to app -> manifests -> AndroidManifests.xml section and allow “Internet Permission“.
<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.
activity_main.xml
<? xml version = "1.0" encoding = "utf-8" ?> < RelativeLayout 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
- Open the MainActivity.java file there within the class, first create the object of the Button class.
// Creating an object of Button class
Button showInterstitialBtn;
- Now inside the onCreate() method, link those objects with their respective IDs that have given in activity_main.xml file.
// link those objects with their respective id’s that we have given in activity_main.xml file
showInterstitialBtn=(Button)findViewById(R.id.showInterBtn);
- Now inside onCreate() method, initialize the Facebook Audience Network SDK.
// initializing the Audience Network SDK
AudienceNetworkAds.initialize(this);
- Create an object of InterstitialAd inside MainActivity.java class
// creating object of InterstitialAd
InterstitialAd fbInterstitialAd;
- Next create a private void loadInterstitial() method outside onCreate() method and define it.
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();
}
- Call the loadInterstitial() inside onCreate() method after initializing the SDK.
Note: Replace “IMG_16_9_APP_INSTALL#YOUR_PLACEMENT_ID” with your own placement id to show real ads.
- Next create a void showInterstitial() method outside onCreate() method which we call later to show ad.
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();
}
}
- So the next thing is to call the showInterstitial() method when a user clicks a show ad button.
- Now in onCreate() method create a ClickListener for the button and call showInterstitial().
// click listener to show Interstitial Ad
showInterstitialBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
showInterstitial();
}
});
- Now add InterstitialAdListener for Interstitial Ad, so that users will know the status of the ads. To add InterstitialAdListener open loadInterstitial() method and add the below code before fbInterstitialAd.loadAd().
// 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();
}
});
- And inside InterstitialAdListener Override methods show a toast message so that users know the status of ad. Below is the complete code for the MainActivity.java file.
MainActivity.java
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(); } } |
Please Login to comment...