Open In App

How to Integrate Facebook Audience Network (FAN) Rewarded Video 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 Rewarded Video ads in the Android app.

Rewarded Video: 



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:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
  
    <!-- Button to Show Rewarded Video Ad By Clicking it -->
    <Button
        android:id="@+id/showVideoBtn"
        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  Rewarded  Video Ad "
        android:textColor="#ffff"
        android:textSize="20dp" />
  
</RelativeLayout>

Step 4: Working with MainActivity.java file

 // Creating an object of Button class

Button showVideoAdBtn;

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

 showVideoAdBtn = (Button) findViewById(R.id.showVideoBtn);

// initializing the Audience Network SDK

AudienceNetworkAds.initialize(this);

// creating object of RewardedVideoAd

private RewardedVideoAd fbRewardedVideoAd;

void loadRewardedVideoAd()

{

 // initializing RewardedVideoAd Object

// RewardedVideoAd Constructor Takes 2 Arguments

// 1)Context

// 2)Placement Id

fbRewardedVideoAd = new RewardedVideoAd(this, “YOUR_PLACEMENT_ID”);

// loading Ad

fbRewardedVideoAd.loadAd();

}

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

 public void showRewardedVideoAd()

 {

  // Checking If Ad is Loaded or Not

   if(fbRewardedVideoAd.isAdLoaded())

 { 

  // showing Video Ad

   fbRewardedVideoAd.show();

 }

else

{

  // Loading Video Ad If it  is Not Loaded

  fbRewardedVideoAd.loadAd();

 }

}

// click listener to show Rewarded Video Ad

showVideoAdBtn.setOnClickListener(new View.OnClickListener() {

           @Override

           public void onClick(View view) {

               showRewardedVideoAd();

           }

   });

 // RewardedVideoAd AdListener

 fbRewardedVideoAd.setAdListener(new RewardedVideoAdListener() {

           @Override

           public void onError(Ad ad, AdError error) {

               // 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();

           }

           @Override

           public void onRewardedVideoCompleted() {

               // Showing Toast Message

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

           }

           @Override

           public void onRewardedVideoClosed() {

               // Showing Toast Message

               Toast.makeText(MainActivity.this, “onRewardedVideoClosed”, 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.RewardedVideoAd;
import com.facebook.ads.RewardedVideoAdListener;
  
public class MainActivity extends AppCompatActivity {
  
    // Creating a object of Button class
    Button showVideoAdBtn;
  
    @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
        showVideoAdBtn
            = (Button)findViewById(R.id.showVideoBtn);
  
        // initializing the Audience Network SDK
        AudienceNetworkAds.initialize(this);
  
        // initializing and loading rewarded video ad
        loadRewardedVideoAd();
  
        // click listener to show Rewarded Video  Ad
        showVideoAdBtn.setOnClickListener(
            new View.OnClickListener() {
                @Override public void onClick(View view)
                {
                    showRewardedVideoAd();
                }
            });
    }
  
    // creating object of RewardedVideoAd
    private RewardedVideoAd fbRewardedVideoAd;
  
    void loadRewardedVideoAd()
    {
        // initializing RewardedVideoAd Object
        // RewardedVideoAd  Constructor Takes 2 Arguments
        // 1)Context
        // 2)Placement Id
        fbRewardedVideoAd = new RewardedVideoAd(
            this, "YOUR_PLACEMENT_ID");
  
        // RewardedVideoAd AdListener
        fbRewardedVideoAd.setAdListener(
            new RewardedVideoAdListener() {
                @Override
                public void onError(Ad ad, AdError error)
                {
                    // 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();
                }
  
                @Override
                public void onRewardedVideoCompleted()
                {
                    // Showing Toast Message
                    Toast
                        .makeText(
                            MainActivity.this,
                            "onRewardedVideoCompleted",
                            Toast.LENGTH_SHORT)
                        .show();
                }
  
                @Override
                public void onRewardedVideoClosed()
                {
                    // Showing Toast Message
                    Toast
                        .makeText(MainActivity.this,
                                  "onRewardedVideoClosed",
                                  Toast.LENGTH_SHORT)
                        .show();
                }
            });
  
        // loading Ad
        fbRewardedVideoAd.loadAd();
    }
  
    public void showRewardedVideoAd()
    {
        // Checking If Ad is Loaded
        // or Not
        if (fbRewardedVideoAd.isAdLoaded()) {
            // showing Video Ad
            fbRewardedVideoAd.show();
        }
        else {
            // Loading Video Ad If it  is Not Loaded
            fbRewardedVideoAd.loadAd();
        }
    }
}

Output: Run on Emulator


Article Tags :