Integrate Admob Rewarded Video Ads in Android

Sovary July 23, 2022 664
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

As the founder and passionate educator behind this platform, I’m dedicated to sharing practical knowledge in programming to help you grow. Whether you’re a beginner exploring Machine Learning, PHP, Laravel, Python, Java, or Android Development, you’ll find tutorials here that are simple, accessible, and easy to understand. My mission is to make learning enjoyable and effective for everyone. Dive in, start learning, and don’t forget to follow along for more tips and insights!. Follow him    

Search