Android Tutorials

Facebook Rewarded Video Ads in Android

This Tutorial Helps You to Integrate Facebook Rewarded Video Ads into your App. You Can Check our tutorial Facebook Native Ads in Android to Implement Native Ads in your App.

Rewarded video ads are a full-screen experience where users opt-in to view a video ad in exchange for something of value, such as virtual currency, in-app items, exclusive content, and more. The ad experience is 15-30 seconds non-skippable and contains an end card with a call to action. Upon completion of the full video, you will receive a callback to grant the suggested reward to the user.

In this tutorial, we will implement Rewarded Video Ads in Android App. Rewarded Video Ads are Used to exchange virtual currency as Coins in a game for Purchasing Some Virtual Items in a Game or in an App. Rewarded Video Ads are played minimum time of 15 seconds to a maximum of 30 seconds.

Initialize Audience Network

AudienceNetworkAds.initialize(this);

Let’s Initialize Reward Video Ads in MainActivity.java

rewardedVideoAd = new RewardedVideoAd(this, "YOUR_PLACEMENT_ID");
    RewardedVideoAdListener rewardedVideoAdListener = new RewardedVideoAdListener() {
        @Override
        public void onError(Ad ad, AdError error) {
            // Rewarded video ad failed to load
            Log.e(TAG, "Rewarded video ad failed to load: " + error.getErrorMessage());
        }

        @Override
        public void onAdLoaded(Ad ad) {
            // Rewarded video ad is loaded and ready to be displayed  
            Log.d(TAG, "Rewarded video ad is loaded and ready to be displayed!");
        }

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

        @Override
        public void onLoggingImpression(Ad ad) {
            // Rewarded Video ad impression - the event will fire when the 
            // video starts playing
            Log.d(TAG, "Rewarded video ad impression logged!");
        }

        @Override
        public void onRewardedVideoCompleted() {
            // Rewarded Video View Complete - the video has been played to the end.
            // You can use this event to initialize your reward
            Log.d(TAG, "Rewarded video completed!");

            // Call method to give reward
            // giveReward();
        }

        @Override
        public void onRewardedVideoClosed() {
            // The Rewarded Video ad was closed - this can occur during the video
            // by closing the app, or closing the end card.
            Log.d(TAG, "Rewarded video ad closed!");
        }
    };
    rewardedVideoAd.loadAd(
            rewardedVideoAd.buildLoadAdConfig()
                    .withAdListener(rewardedVideoAdListener)
                    .build());

The Ad is Shown After it is Loaded. So we have to write code to show ads in the OnAdLoaded method. Let’s do that Now.

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

We have implemented all the code required to Load and Show Reward Video Ads. Now Let’s See the Full Code of MainActivity.java.

MainActivity.java

package com.example.facebookadssampleapp;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

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 {

    private static final String TAG = "MainActivity";
    private RewardedVideoAd rewardedVideoAd;
    private Button reward_ad_button;
    private TextView reward_text;

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

        AudienceNetworkAds.initialize(this);
        reward_ad_button = findViewById(R.id.reward_ad_button);
        reward_text = findViewById(R.id.reward_text);

        reward_ad_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                rewardedVideoAd = new RewardedVideoAd(MainActivity.this, "PLAYABLE#YOUR_PLACEMENT_ID");
                RewardedVideoAdListener rewardedVideoAdListener = new RewardedVideoAdListener() {
                    @Override
                    public void onError(Ad ad, AdError error) {
                        // Rewarded video ad failed to load
                        Log.e(TAG, "Rewarded video ad failed to load: " + error.getErrorMessage());
                    }

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

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

                    @Override
                    public void onLoggingImpression(Ad ad) {
                        // Rewarded Video ad impression - the event will fire when the
                        // video starts playing
                        Log.d(TAG, "Rewarded video ad impression logged!");
                    }

                    @Override
                    public void onRewardedVideoCompleted() {
                        // Rewarded Video View Complete - the video has been played to the end.
                        // You can use this event to initialize your reward
                        Log.d(TAG, "Rewarded video completed!");
                        reward_text.setText("Reward: 20");


                        // Call method to give reward
                        // giveReward();
                    }

                    @Override
                    public void onRewardedVideoClosed() {
                        // The Rewarded Video ad was closed - this can occur during the video
                        // by closing the app, or closing the end card.
                        Log.d(TAG, "Rewarded video ad closed!");
                    }
                };
                rewardedVideoAd.loadAd(
                        rewardedVideoAd.buildLoadAdConfig()
                                .withAdListener(rewardedVideoAdListener)
                                .build());
            }
        });


    }


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


}

We Achieved to Load and Show Reward Video Ads, But I forgot to design activity_main.xml. Now Let’s See that.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/reward_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Reward: 10"
        android:textColor="@color/black"
        android:textSize="25sp"
        android:layout_centerHorizontal="true"
        android:layout_above="@id/reward_ad_button"
        android:layout_marginBottom="25dp"/>

    <Button
        android:id="@+id/reward_ad_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Show Reward Ad"
        android:layout_centerInParent="true"/>

</RelativeLayout>

Hurray! We Have Finished Everything Required to Load and Show Facebook Rewarded Video Ads. Now Every time a User Seen a Video He will get a Reward of 10 Coins. For More Information Check Facebook Audience Network Docs.

Now Try this in Yourself. You can even implement it very easily.

I hope You and Your Family are Safe. Be happy. Stay Home and Stay Safe.

Don’t Forget to Code 🙂

For More Articles, Please Subscribe to Notifications. Thank you.

KSR

Hi there! I am the Founder of Cyber World Technologies. My skills include Android, Firebase, Python, PHP, and a lot more. If you have a project that you'd like me to work on, please let me know: contact@cyberworldtechnologies.co.in

Related Articles

Leave a Reply

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

Back to top button