Search code examples
javaandroidandroid-studioadmob-rewardedvideoadrewardedvideoad

Rewarded Video Ads version 20.0.0 above in Adapter


I'm new in Java and I want to implement Reward Video Ads version 20.0.0 or above in Adapter.class. Everything runs well as I read in this link https://developers.google.com/admob/android/rewarded, till I found the codes to show reward ads. The codes look like this if in MainActivity.class

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.");
}

My question is, how to implement the codes above in Adapter.class?

Thanks for your answers. It will help me so much.

Edit : To make the question clearer, I want to implement Rewarded Video Ad in every list of RecyclerView


Solution

  • I have found a complete tutorial from this link. As DarShan's answer, I have to adjust the codes for valid activity context. And All runs well. Here are my codes from the tutorial. I change MainActivity.this to valid activity

    if (mRewardedAd != null) {
      Activity activityContext = activity;
      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.");
    }