Android Tutorial

Software Setup and Configuration

Android Studio

File Structure

Components

Core Topics

Layout

View

Button

Intent and Intent Filters

Toast

RecyclerView

Fragments

Adapters

Other UI Component

Image Loading Libraries

Date and Time

Material Design

Bars

Working with Google Maps

Chart

Animation

Database

Advance Android

Jetpack

Architecture

App Publish

App Monetization

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

Integrating Facebook Audience Network (FAN) Rewarded Video Ads into your Android application provides users with rewards in exchange for watching video ads. Here's how to integrate FAN Rewarded Video Ads:

1. Setup on Facebook:

Ensure you've registered your app with the Facebook Developers portal and added "Audience Network" to your app.

2. Integrate Facebook SDK:

In your build.gradle (app module):

implementation 'com.facebook.android:facebook-android-sdk:latest_version'
implementation 'com.facebook.android:audience-network-sdk:latest_version'

Sync your project with the Gradle files.

3. Load and Show the Rewarded Video Ad:

In your activity or fragment:

import com.facebook.ads.*;

public class YourActivity extends AppCompatActivity {

    private RewardedVideoAd rewardedVideoAd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.your_layout);

        // Initialize the Audience Network SDK
        AudienceNetworkAds.initialize(this);

        rewardedVideoAd = new RewardedVideoAd(this, "YOUR_PLACEMENT_ID");
        rewardedVideoAd.loadAd(
            rewardedVideoAd.buildLoadAdConfig()
                .withAdListener(new RewardedVideoAdListener() {
                    @Override
                    public void onRewardedVideoCompleted() {
                        // User watched the entire video, reward them!
                    }

                    @Override
                    public void onLoggingImpression(Ad ad) {
                        // Rewarded video impression
                    }

                    @Override
                    public void onRewardedVideoClosed() {
                        // The rewarded video ad was closed
                    }

                    @Override
                    public void onError(Ad ad, AdError error) {
                        // Rewarded video ad failed to load
                    }

                    @Override
                    public void onAdLoaded(Ad ad) {
                        // Rewarded video ad is loaded and ready to be displayed
                        rewardedVideoAd.show();
                    }

                    @Override
                    public void onAdClicked(Ad ad) {
                        // Rewarded video ad clicked
                    }
                })
                .build()
        );
    }

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

Replace YOUR_PLACEMENT_ID with your actual placement ID from the Facebook Developers portal.

4. Testing:

Always test the rewarded video ad integration before releasing it:

  1. Visit the Monetization Manager.
  2. Click on "Integration" > "Test Devices" in the sidebar.
  3. Add your device using its Ad ID.

Your device will now display test ads from the Facebook Audience Network, allowing you to evaluate the rewarded video ad experience without affecting your live users.

Once you've confirmed that everything works as expected, activate your app in the Facebook Developers portal and begin monetizing with real rewarded video ads. Always ensure you provide users with the promised rewards after they've watched the videos. Monitor the performance of your ads and adjust your strategy as needed for an optimal balance between user experience and revenue.

  1. Adding Facebook Audience Network rewarded video ads to Android Studio project:

    • Add FAN rewarded video ads to your Android Studio project by following these steps:
      • Open your build.gradle file and add the FAN SDK dependency.
      implementation 'com.facebook.android:audience-network-sdk:6.0.0'
      
      • Sync your project to fetch the new dependency.
      • Update your activity or fragment code to include the FAN rewarded video ad.
  2. Displaying Facebook Audience Network rewarded videos in Android app:

    • Display FAN rewarded video ads in your Android app by integrating the rewarded video ad and handling reward delivery. Here's a simplified example:
    // MainActivity.java
    import android.os.Bundle;
    import androidx.appcompat.app.AppCompatActivity;
    import com.facebook.ads.Ad;
    import com.facebook.ads.AdError;
    import com.facebook.ads.AdSettings;
    import com.facebook.ads.RewardedVideoAd;
    import com.facebook.ads.RewardedVideoAdListener;
    
    public class MainActivity extends AppCompatActivity {
    
        private RewardedVideoAd rewardedVideoAd;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            // Initialize the Audience Network SDK
            AdSettings.initialize(getApplicationContext());
    
            // Load the rewarded video ad
            rewardedVideoAd = new RewardedVideoAd(this, getString(R.string.facebook_rewarded_video_ad_unit_id));
            rewardedVideoAd.setAdListener(new RewardedVideoAdListener() {
                @Override
                public void onError(Ad ad, AdError adError) {
                    // Handle rewarded video ad error
                }
    
                @Override
                public void onAdLoaded(Ad ad) {
                    // Rewarded video ad loaded
                    if (rewardedVideoAd.isAdLoaded()) {
                        rewardedVideoAd.show();
                    }
                }
    
                @Override
                public void onAdClicked(Ad ad) {
                    // User clicked on the rewarded video ad
                }
    
                @Override
                public void onLoggingImpression(Ad ad) {
                    // Ad impression logged
                }
    
                @Override
                public void onRewardedVideoCompleted() {
                    // User completed watching the rewarded video ad
                    // Grant rewards to the user
                }
    
                @Override
                public void onRewardedVideoClosed() {
                    // Rewarded video ad closed
                }
            });
    
            // Load ad
            rewardedVideoAd.loadAd();
        }
    
        // Other activity lifecycle methods...
    
        @Override
        protected void onDestroy() {
            if (rewardedVideoAd != null) {
                rewardedVideoAd.destroy();
            }
            super.onDestroy();
        }
    }
    

    Make sure to replace "@string/facebook_rewarded_video_ad_unit_id" with your actual FAN rewarded video ad unit ID.