Intergrating Rewarded Video Ad In Android App

Rewarded Video ad is full-screen ads that cover the whole UI of the app. The eCPM (Effective Cost Per Mille) of Rewarded Video ads are relatively higher than banner and Interstitial ads and also leads to higher CTR(Click Through Rate) which results in more earning from your app.

The user gets an in-App reward when they watch the Rewarded Video from start to end. This type of ad is mostly used in games and also can be used in the app.

STEPS

1.Create a new project in android studio

2.Add the following dependancy in your gradle script,app level

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

3.In AndroidManifests.xml add the following Permissions

<uses-permission android:name="android.permission.INTERNET2/>

4.Add the following meta data inside <application> tag

<meta-data
          android:name="com.google.android.gms.ads.APPLICATION_ID" 
          android:value="ca-app-pub-3940256099942544~3347511713"/>

5.Add a button in activity_main.xml so that when the user clicks on it it shows the ad

Your activity_main.xml code should appear as below:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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/btnShowAdd"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Show Rewarded Ad"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

6.In MainActivity.java do the following:

public class MainActivity extends AppCompatActivity {

    private Button btnShowAd;
    // creating object of RewardedVideoAd
    private RewardedVideoAd AdMobrewardedVideoAd;

    // AdMob Rewarded Video Ad Id
    private String AdId="ca-app-pub-3940256099942544/5224354917";

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

        btnShowAd = findViewById(R.id.btnShowAdd);
        MobileAds.initialize(this);
        loadRewardedVideoAd();

        btnShowAd.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                   showRewardedVideoAd();
                }
           });

    }

    void loadRewardedVideoAd() {

        // initializing RewardedVideoAd Object

        // RewardedVideoAd Constructor Takes Context as its  Argument
        AdMobrewardedVideoAd = MobileAds.getRewardedVideoAdInstance(this);

        // Loading Rewarded Video Ad

        //AdMobrewardedVideoAd.loadAd(AdId, new AdRequest().Builder().build());

    }

    public void showRewardedVideoAd(){

        if(AdMobrewardedVideoAd.isLoaded()){
            // showing Video Ad
            AdMobrewardedVideoAd.show();
        }else{
            // Loading Rewarded Video Ad
            AdMobrewardedVideoAd.loadAd(AdId, new AdRequest.Builder().build());
        }

        // Rewarded Video Ad Listener
        AdMobrewardedVideoAd.setRewardedVideoAdListener(new RewardedVideoAdListener() {

            @Override
            public void onRewardedVideoAdLoaded() {

                // Showing Toast Message
                Toast.makeText(getApplicationContext(), "onRewardedVideoAdLoaded", Toast.LENGTH_SHORT).show();

            }

            @Override
            public void onRewardedVideoAdOpened() {

                // Showing Toast Message
                Toast.makeText(getApplicationContext(),"onRewardedVideoAdOpened", Toast.LENGTH_SHORT).show();

            }

            @Override
            public void onRewardedVideoStarted() {

                // Showing Toast Message
                Toast.makeText(getApplicationContext(), "Reward video started", Toast.LENGTH_SHORT).show();

            }

            @Override
            public void onRewardedVideoAdClosed() {

                // Showing Toast Message
                Toast.makeText(getApplicationContext(), "Reward video closed", Toast.LENGTH_SHORT).show();

            }

            @Override
            public void onRewarded(com.google.android.gms.ads.reward.RewardItem rewardItem) {

            }

            @Override
            public void onRewardedVideoAdLeftApplication() {

                // Showing Toast Message
                Toast.makeText(getApplicationContext(), "onRewardedVideoAdLeftApplication", Toast.LENGTH_SHORT).show();

            }

            @Override
            public void onRewardedVideoAdFailedToLoad(int i) {

                // Showing Toast Message
                Toast.makeText(getApplicationContext(), "Failed To Load", Toast.LENGTH_SHORT).show();

            }

            @Override

            public void onRewardedVideoCompleted() {
                // Showing Toast Message
                Toast.makeText(getApplicationContext(), "Rewarded", Toast.LENGTH_SHORT).show();

            }

        });

    }
}

NOTE:We have used test ids for admob,create your own app id at https://admob.google.com/home/.

Run your code and everything should running.

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

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