Integrate Admob Rewarded Video Ads in Android

Sovary July 23, 2022 634
5 minutes read

Rewarded ad is full screen video around 15 to 60 seconds. Users have to play video ad in full length to get rewarded back. This concept is usually found in mobile game with different purpose such as: get extra points diamon or coins, unlock new level or items...etc

To earn the revenue by integrating Google AdMob ads in android applications, we need to follow below steps.

If you are beginner before jump to step below please do read Integrate Android Google AdMob SDK Tutorial.
This article is well tested in Google Mobile Ads SDK version 20.0.0 and 20.6.0 which contained many breaking changes from early version.

So, I will implement a Rewarded Ad in Android App very quick and you can apply to your real project

Design Simple Layout

We will add a button to perform click and show the video ad after ad is loaded.

<?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">
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="Show Reward VDO"
        android:id="@+id/btn"/>
</RelativeLayout>

Initiailize Ad in Java Class

Note: Make all calls to the Mobile Ads SDK on the main thread.

If you read how to implement banner ad, you have to adding Adview in layout xml file where you want display ad. For Rewarded Ad is like full screen ad but video playing overlay on screen with specific duration. Every Admob implementation must start with initialize Ad SDK at first place.

Open main activity java file MainActivity.java in app -> java -> package(com.cambotutorial.sovary.adtest) -> MainActivity.java write the code as shown below.

package com.cambotutorial.sovary.adtest

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.rewarded.RewardedAd;

public class MainActivity extends AppCompatActivity 
{

    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
       // Call initialize only one time
        MobileAds.initialize(this, new OnInitializationCompleteListener() 
        {
            @Override
            public void onInitializationComplete(InitializationStatus initializationStatus) {
            }
        });
    }
}

Load Rewarded Ad Object

Just like every ad implement we load the ad into AdRequest object. R.string.RdRewarded where we have place in strings.xml you can read this article Integrate Android Google AdMob SDK Tutorial.

AdRequest adRequest = new AdRequest.Builder().build();

RewardedAd.load(this, getString(R.string.AdRewarded), adRequest, new RewardedAdLoadCallback() 
{
    @Override
    public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) 
    {
        // Handle the error.
        Log.d(TAG, loadAdError.getMessage());
        mRewardedAd = null;
    }

    @Override
    public void onAdLoaded(@NonNull RewardedAd rewardedAd) 
    {
        mRewardedAd = rewardedAd;
        Log.d(TAG, "Ad was loaded.");
    }
});

Overrided Methods:

  • onAdFailedToLoad(@NonNull LoadAdError loadAdError) - Called when any error occured we can know type of error via the parameter  which is of type LoadAdError .
  • onAdLoaded(@NonNull RewardedAd rewardedAd) - Called when an Rewarded ad is loaded. We can update the UI or do other stuff.

Set Callback Listener

Before showing RewardedAd, make sure to set the callback FullScreenContentCallback which is handles events related to displaying your RewardedAd

mRewardedAd.setFullScreenContentCallback(new FullScreenContentCallback() 
{
  @Override
  public void onAdShowedFullScreenContent() 
  {
    // Called when ad is shown.
    Log.d(TAG, "Ad was shown.");
  }

  @Override
  public void onAdFailedToShowFullScreenContent(AdError adError) 
  {
    // Called when ad fails to show.
    Log.d(TAG, "Ad failed to show.");
  }

  @Override
  public void onAdDismissedFullScreenContent() 
  {
    // Called when ad is dismissed.
    // Set the ad reference to null so you don't show the ad a second time.
    Log.d(TAG, "Ad was dismissed.");
    mRewardedAd = null;
  }
});

Overrided methods: 

  • onAdDismissedFullScreenContent() - Called when user closed the rewarded ad content.
  • onAdFailedToShowFullScreenContent(AdError adError) - Called when the ad failed to show rewarded ad content.
  • onAdShowedFullScreenContent() - Called when the ad showed the full screen content overlay on activity or fragment.

Ready to Display Ad

Alright, everything is implemented last step to do is to show ad on screen. you will use an OnUserEarnedRewardListener object to handle reward after user watch full ad video.

if (mRewardedAd != null) 
{
  mRewardedAd.show(MainActivity.this, new OnUserEarnedRewardListener() 
  {
    @Override
    public void onUserEarnedReward(@NonNull RewardItem rewardItem) 
    {
      // Handle the reward.
      Log.d(TAG, "The user earned the reward.");
      int rewardAmount = rewardItem.getAmount();
      String rewardType = rewardItem.getType();
      // TODO
    }
  });
} 
else 
{
  Log.d(TAG, "The rewarded ad wasn't ready yet.");
}

 Overrided methods: 

  • onUserEarnedReward(@NonNull RewardItem rewardItem) - Called when user watch video ad without exit before duration video finished.

Full Snippet Code

Check this below snippet make sure you don't miss any syntax. If your app not work make sure you read this Integrate Android Google AdMob SDK Tutorial at first place. 

package com.cambotutorial.sovary.adtest

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.rewarded.RewardedAd;

public class MainActivity extends AppCompatActivity 
{
    private final String TAG = "MainActivity";
    private RewardedAd mRewardedAd;

    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // Call initialize only one time
        MobileAds.initialize(this, new OnInitializationCompleteListener() {
            @Override
            public void onInitializationComplete(InitializationStatus initializationStatus) {}
        });
        AdRequest adRequest = new AdRequest.Builder().build();
        RewardedAd.load(this, getString(string.AdRewarded), adRequest, new RewardedAdLoadCallback() 
        {
            @Override
            public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) 
            {
                // Handle the error.
                Log.d(TAG, loadAdError.getMessage());
                mRewardedAd = null;
            }
            @Override
            public void onAdLoaded(@NonNull RewardedAd rewardedAd) 
            {
                mRewardedAd = rewardedAd;
                Log.d(TAG, "Ad was loaded.");
            }
        });

        // Find button by id in xml layout file
        Button btn = findViewById(R.id.btn);
        // Perform click to show ad with call back listener
        btn.setOnClickListener(v -> 
        {
            if (mInterstitialAd != null) 
            {
                mRewardedAd.setFullScreenContentCallback(new FullScreenContentCallback() 
                {
                    @Override
                    public void onAdShowedFullScreenContent() 
                    {
                        // Called when ad is shown.
                        Log.d(TAG, "Ad was shown.");
                    }
                    @Override
                    public void onAdFailedToShowFullScreenContent(AdError adError) 
                    {
                        // Called when ad fails to show.
                        Log.d(TAG, "Ad failed to show.");
                    }
                    @Override
                    public void onAdDismissedFullScreenContent() 
                    {
                        // Called when ad is dismissed.
                        // Set the ad reference to null so you don't show the ad a second time.
                        Log.d(TAG, "Ad was dismissed.");
                        mRewardedAd = null;
                    }
                });
                mRewardedAd.show(MainActivity.this);
            }
            else 
            {
                Log.d(TAG, "The interstitial ad wasn't ready yet.");
            }
        });
    }
}

Hope this article would help you. Have a nice day! 

Android  Java  Admob 
Author

Founder of CamboTutorial.com, I am happy to share my knowledge related to programming that can help other people. I love write tutorial related to PHP, Laravel, Python, Java, Android Developement, all published post are make simple and easy to understand for beginner. Follow him    

Search