Android App Development

Admob Rewarded ads in Android v-20.1.0

Admob Rewarded ads
Admob Rewarded ads

Admob Rewarded ads are Used to Reward Users in a Game or in an App. for Skipping Levels in a Game, For Finishing Levels with Some Extra Coins or Gifts that are to be used in that level and reward Ads are also given for Credits and the Credits are Used for Some Specific actions like Downloading Content or Foe Performing Some Premium Tasks inside the App. In Some Apps, they can be Disabled by Taking Premium plans also.

Admob Reward Ads are Very Useful for Developers for Increasing their Revenue. The CPC of these Ads is Relatively Very High. Small Scale Developers can Earn MoreRevenue using these Reward Ads.

Check Our Banner Ads Tutorial

In this Tutorial we See how to develop Reward Ads in an App. Let’s go and See the Code

Initialize the Variable

 private RewardedAd mRewardedAd;

Here we Use a Button to show the Reward Ad. So we have to design our activity_main.xml file with a Button.

activity_main.xml:

<?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:id="@+id/reward_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"
        android:layout_centerInParent="true"
        android:textSize="20sp"/>

    <com.google.android.gms.ads.AdView
        xmlns:ads="http://schemas.android.com/apk/res-auto"
        android:id="@+id/adView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_alignParentBottom="true"
        ads:adSize="BANNER"
        ads:adUnitId="ca-app-pub-3940256099942544/6300978111">
    </com.google.android.gms.ads.AdView>

</RelativeLayout>

In the Above Layout we also Display Banner Ad.

Here is the Code where Reward Ad id Loaded

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

    RewardedAd.load(this, "ca-app-pub-3940256099942544/5224354917",
      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.");
        }
    });

To Handle Events we Use  FullScreenContentCallback by Using below Code

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

Now We Loaded ad and We also Coded for Handiling Events So nw Let’s See how to Show the Loaded Ads.

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

The above code describe how to show loaded Reward Ads. Now Let’s See the Full Code of MainActivity.java

MainActivity.java:

package com.example.admobsampleapp;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.google.android.gms.ads.AdError;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.FullScreenContentCallback;
import com.google.android.gms.ads.LoadAdError;
import com.google.android.gms.ads.MobileAds;
import com.google.android.gms.ads.OnUserEarnedRewardListener;
import com.google.android.gms.ads.initialization.InitializationStatus;
import com.google.android.gms.ads.initialization.OnInitializationCompleteListener;
import com.google.android.gms.ads.interstitial.InterstitialAd;
import com.google.android.gms.ads.interstitial.InterstitialAdLoadCallback;
import com.google.android.gms.ads.rewarded.RewardItem;
import com.google.android.gms.ads.rewarded.RewardedAd;
import com.google.android.gms.ads.rewarded.RewardedAdLoadCallback;

public class MainActivity extends AppCompatActivity {

    private AdView mAdView;
    private Button reward_button;
    private RewardedAd mRewardedAd;
    private static final String TAG = "MainActivity";

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        reward_button = findViewById(R.id.reward_button);
        MobileAds.initialize(this, new OnInitializationCompleteListener() {
            @Override
            public void onInitializationComplete(InitializationStatus initializationStatus) {
            }
        });

        mAdView = findViewById(R.id.adView);
        AdRequest adRequest = new AdRequest.Builder().build();
        mAdView.loadAd(adRequest);



        loadRewardVideoAd();


        reward_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showRewardVideoAd();
                if (mRewardedAd == null){
                    loadRewardVideoAd();
                }
            }
        });

    }

    private void loadRewardVideoAd(){
        AdRequest adRequest = new AdRequest.Builder().build();

        RewardedAd.load(this, "ca-app-pub-3940256099942544/5224354917",
                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.");
                        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.");
                            }
                        });
                    }
                });
    }

    private void showRewardVideoAd() {
        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.");
        }
    }
}

Note: Use Only Test AdUnits while Testing your App. There are Dedicated Test ad Units to test your integration. And the Ad Unit used in the Above tutorial is a test AdUnit.

Finally, We Achieved to Show the Reward Video Ads. Remember Rewarded Ads Increase Your Revenue. If you are a smallscale Developer, Develop Apps that Use Rewarded Ads so you can Grow Faster and Earn more Revenue. But Also Follow Admob Policies While Developing Admob Ads in Your Apps.

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button