Create rewarded Admob ad on Android

Tiempo de lectura: 2 minutos

Today we’re going to learn how to add a rewarded Admob ad on Android using Java.

First, we’ll implement in our build.gradle:

implementation 'com.google.android.gms:play-services-ads:23.0.0'

Then we need to initialize the ads in our MainActivity:

MobileAds.initialize(
        this,
        new OnInitializationCompleteListener() {
          @Override
          public void onInitializationComplete(InitializationStatus initializationStatus) {}
        });

And we add in the manifest (below </application>)

<meta-data
        android:name="com.google.android.gms.ads.AD_MANAGER_APP"
        android:value="true" />

And also within <application> </application>

<meta-data
    android:name="com.google.android.gms.ads.APPLICATION_ID"
    android:value="YOUR_ADMOB_APP_ID" />

Change YOUR_ADMOB_APP_ID to your Admob app id.

Now let’s create our rewarded ad:

private RewardedAd rewardedAd;    

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

RewardedAd.load(this, "rewarded_ad_id",
        adRequest, new RewardedAdLoadCallback() {
            @Override
            public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
                // Handle the error.
                Log.d("TAG", loadAdError.toString());
                rewardedAd = null;
            }

            @Override
            public void onAdLoaded(@NonNull RewardedAd ad) {
                rewardedAd = ad;
                Log.d("TAG", "Ad was loaded.");
                rewardedAd.show(this, new OnUserEarnedRewardListener() {
                    @Override
                    public void onUserEarnedReward(@NonNull RewardItem rewardItem) {
                      //Perform necessary actions when rewarded ad is viewed
                    }
                });

rewardedAd.setFullScreenContentCallback(new FullScreenContentCallback() {
    @Override
    public void onAdClicked() {
        // Called when a click is recorded for an ad.
        Log.d("TAG", "Ad was clicked.");
    }

    @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 dismissed fullscreen content.");
        rewardedAd = null;
    }

    @Override
    public void onAdFailedToShowFullScreenContent(AdError adError) {
        // Called when ad fails to show.
        Log.e("TAG", "Ad failed to show fullscreen content.");
        rewardedAd = null;
    }

    @Override
    public void onAdImpression() {
        // Called when an impression is recorded for an ad.
        Log.d("TAG", "Ad recorded an impression.");
    }

    @Override
    public void onAdShowedFullScreenContent() {
        // Called when ad is shown.
        loader.setVisibility(View.GONE);
        Log.d("TAG", "Ad showed fullscreen content.");
    }
});

            }
        });

Here: “rewarded_ad_id” we’ll indicate our rewarded ad id.

And here we can perform the necessary action with the rewarded ad once viewed: //Perform necessary actions when rewarded ad is viewed

With this, we’ll show the rewarded ad.

Leave a Comment