Rewarded Ads

Prerequisites

  • Google Mobile Ads SDK 19.7.0 or higher.
  • Follow the Get Started guide to import the Google Mobile Ads SDK and update your Android manifest.

Always test with test ads

When building and testing your apps, make sure you use test ads rather than live, production ads. Failure to do so can lead to suspension of your account.

The easiest way to load test ads is to use our dedicated test ad unit ID for Android rewarded ads:

/6499/example/rewarded

It's been specially configured to return test ads for every request, and you're free to use it in your own apps while coding, testing, and debugging. Just make sure you replace it with your own ad unit ID before publishing your app.

For more information about how the Mobile Ads SDK's test ads work, see Test Ads.

Load a rewarded ad object

Rewarded ads are loaded by calling the static load() method on the RewardedAd class and passing in a RewardedAdLoadCallback. This is usually done in the onCreate() method of an Activity. Notice that like other format load callbacks, RewardedAdLoadCallback leverages LoadAdError to provide higher fidelity error details.

Java

import com.google.android.gms.ads.rewarded.RewardedAd;

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

  @Override
  protected void onCreate(Bundle savedInstanceState) {

    AdManagerAdRequest adRequest = new AdManagerAdRequest.Builder().build()

    RewardedAd.load(this, "/6499/example/rewarded",
      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, "onAdFailedToLoad");
        }
    });
  }
}

Kotlin

class MainActivity : AppCompatActivity() {

  private var mRewardedAd: RewardedAd? = null
  private final var TAG = 'MainActivity'

  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    var adRequest = AdManagerAdRequest.Builder().build()

    RewardedAd.load(this,"/6499/example/rewarded", adRequest, object : RewardedAdLoadCallback() {
      override fun onAdFailedToLoad(adError: LoadAdError) {
        Log.d(TAG, adError?.message)
        mRewardedAd = null
      }

      override fun onAdLoaded(rewardedAd: RewardedAd) {
        Log.d(TAG, 'Ad was loaded.')
        mRewardedAd = rewardedAd
       }
    })
  }
}

Set the FullScreenContentDelegate

The FullScreenContentCallback handles events related to displaying your RewardedAd. Before you show your RewardedAd, make sure to set the callback like so:

Java

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

  @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.
    // Don't forget to set the ad reference to null so you
    // don't show the ad a second time.
    Log.d(TAG, 'Ad was dismissed.');
  }
});

Kotlin

mRewardedAd?.fullScreenContentCallback = object: FullScreenContentCallback() {
  override fun onAdDismissedFullScreenContent() {
    Log.d(TAG, 'Ad was dismissed.')
  }

  override fun onAdFailedToShowFullScreenContent(adError: AdError?) {
    Log.d(TAG, 'Ad failed to show.')
  }

  override fun onAdShowedFullScreenContent() {
    Log.d(TAG, 'Ad showed fullscreen content.')
    // Called when ad is dismissed.
    // Don't set the ad reference to null to avoid showing the ad a second time.
    mRewardedAd = null
  }
}

Show the ad

When you show a rewarded ad, you will use an OnUserEarnedRewardListener object to handle reward events.

Java

if (mRewardedAd != null) {
  Activity activityContext = MainActivity.this
  mRewardedAd.show(activityContext, 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();
    }
  });
} else {
  Log.d("TAG", "The rewarded ad wasn't ready yet.");
}

Kotlin

if (mRewardedAd != null){
  mRewardedAd?.show(this, OnUserEarnedRewardListener() {
    fun onUserEarnedReward(rewardItem: RewardItem) {
      var rewardAmount = rewardItem.getReward()
      var rewardType = rewardItem.getType()
      Log.d("TAG", "User earned the reward.")
    }
  })
} else {
  Log.d("TAG", "The rewarded ad wasn't ready yet.")
}

Next steps

  • Create your own rewarded video ad unit in the Ad Manager UI.

  • Try another ad format: