How to Integrate Google Admob Rewarded Video Ads in Android?

In order to earn money from the Android app or game, there are many ways such as in-App Purchases, Sponsorship, Advertisements, and many more. But there is another popular method to earn money from the Android app is by integrating an advertisement e.g known as Google AdMob. Google AdMob is designed with developers in mind, AdMob helps to earn more app revenue, deliver better user experience, and surface actionable insights all with automated tools that do the hard work for you.

Why Google Admob Network?

  • Minimum Payout is $100
  • Wide Range of Ad Formats
  • Maximum Fill Rates
  • High eCPM(Effective Cost Per Mille)
  • Quality Ads
  • Personalized Ads

Formats of Google AdMob

There are mainly four types of flexible, high-performing format available in Google AdMob

  • Native: Ads that you design to fit the app, seamlessly
  • Interstitial: Full-screen ads that capture attention and become part of the experience.
  • Banner: Traditional formats in a variety of placements.
  • Rewarded Video: An immersive, user-initiated video ad that rewards users for watching.

In this article let’s integrate Google Admob SDK to show Rewarded Video Ad in the Android app.

Rewarded Video:

  •  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.



Approach

Step 1: Creating a New Project

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that choose Java as language though we are going to implement this project in Java language.

Step 2: Before going to the coding section first do some pre-task

  • Go to app -> res -> values -> colors.xml file and set the colors for the app.

colors.xml

filter_none

edit
close

play_arrow

link
brightness_4
code

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimary">#0F9D58</color>
    <color name="colorPrimaryDark">#0F9D58</color>
    <color name="colorAccent">#05af9b</color>
</resources>

chevron_right


  • Go to Gradle Scripts -> build.gradle (Module: app) section and import following dependencies and click the “sync now” on the above pop up.

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

<uses-permission android:name=”android.permission.INTERNET”/>

  • Go to app->manifests->AndroidManifest.xml section and add meta-data tag inside <application> tag.

<meta-data

            android:name=”com.google.android.gms.ads.APPLICATION_ID” 

            android:value=”ca-app-pub-3940256099942544~3347511713″

/>



Step 3: Designing the UI 

In the activity_main.xml add a Button so when the user will click the Button the Rewarded Video will be played on the screen.

activity_main.xml

filter_none

edit
close

play_arrow

link
brightness_4
code

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
  
    <!-- Button to Show Rewarded Video Ad By Clicking it -->
    <Button
        android:id="@+id/showVideoBtn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_margin="8dp"
        android:background="@color/colorPrimary"
        android:padding="16dp"
        android:text="Show  Rewarded  Video Ad "
        android:textColor="#ffff"
        android:textSize="20dp" />
  
</RelativeLayout>

chevron_right


Step 4: Working with MainActivity.java file

  • Open the MainActivity.java file there within the class, first of all, create the object of the Button class.

// Creating a object of Button class

Button showVideoAdBtn;

  • Now inside the onCreate() method, link those objects with their respective IDs that are given in activity_main.xml file.

 // link those objects with their respective id’s that we have given in activity_main.xml file

 showVideoAdBtn=(Button)findViewById(R.id.showVideoBtn);

  • Now inside onCreate() method, initialize the Google Admob SDK

 // initializing the Google Admob  SDK

 MobileAds.initialize(this);

  • Create an object of RewardedVideoAd inside MainActivity.java class

// creating object of RewardedVideoAd

private RewardedVideoAd AdMobrewardedVideoAd;



  • Create AdMob Rewarded Video Ad Id inside MainActivity.java class

  // AdMob Rewarded Video Ad Id

  private String AdId=”ca-app-pub-3940256099942544/5224354917″;

  • Next create a private void loadRewardedVideoAd() method outside onCreate() method and define it.

  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());

}



  • Call the loadRewardedVideoAd() inside oncreate() method after initializing the SDK.

Note: Replace “AdId” with your own app ad id to show real ads.

  • Next create a void showRewardedVideoAd() method outside onCreate() method which we call later to show ad.

  public void showRewardedVideoAd()

   {

       if(AdMobrewardedVideoAd.isLoaded())//Checking If Ad is Loaded or Not

       {

           // showing Video Ad

           AdMobrewardedVideoAd.show();

       }

       else

       {



           // Loading Rewarded Video Ad

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

       }

   }

  • So the next thing is to call the showRewardedVideoAd() method. Now in oncreate() method create a ClickListener for the button and call showRewardedVideoAd().

// click listener to show Rewarded Video  Ad

showVideoAdBtn.setOnClickListener(new View.OnClickListener() {

          @Override

          public void onClick(View view) {

              showRewardedVideoAd();

          }



 });

  • Now add RewardedVideoAdListener for Rewarded Video Ad, so that user will know the status of the ads.
  • To add RewardedVideoAdListener open loadRewardedVideoAd() method and add the below code before AdMobrewardedVideoAd.loadAd(AdId, new AdRequest.Builder().build());

 // Rewarded Video Ad Listener

 AdMobrewardedVideoAd.setRewardedVideoAdListener(new RewardedVideoAdListener() {

           @Override

           public void onRewardedVideoAdLoaded() {

               // Showing Toast Message

              Toast.makeText(MainActivity.this, “onRewardedVideoAdLoaded”, Toast.LENGTH_SHORT).show();

           }

           @Override

           public void onRewardedVideoAdOpened() {



               // Showing Toast Message

              Toast.makeText(MainActivity.this, “onRewardedVideoAdOpened”, Toast.LENGTH_SHORT).show();

           }

           @Override

           public void onRewardedVideoStarted() {

              // Showing Toast Message

              Toast.makeText(MainActivity.this, “onRewardedVideoStarted”, Toast.LENGTH_SHORT).show();

           }

           @Override

           public void onRewardedVideoAdClosed() {



               // Showing Toast Message

               Toast.makeText(MainActivity.this, “onRewardedVideoAdClosed”, Toast.LENGTH_SHORT).show();

           }

           @Override

           public void onRewarded(RewardItem rewardItem) {

               // Showing Toast Message

               Toast.makeText(MainActivity.this, “onRewarded”, Toast.LENGTH_SHORT).show();

           }

           @Override

           public void onRewardedVideoAdLeftApplication() {

               // Showing Toast Message

               Toast.makeText(MainActivity.this, “onRewardedVideoAdLeftApplication”, Toast.LENGTH_SHORT).show();

           }

           @Override

           public void onRewardedVideoAdFailedToLoad(int i) {

               // Showing Toast Message

               Toast.makeText(MainActivity.this, “onRewardedVideoAdFailedToLoad”, Toast.LENGTH_SHORT).show();

           }

           @Override

           public void onRewardedVideoCompleted() {



               // Showing Toast Message

               Toast.makeText(MainActivity.this, “onRewardedVideoCompleted”, Toast.LENGTH_SHORT).show();

           }

});

  • And inside RewardedVideoAdListener Override methods show a toast message so that users know the status of the ad. Below is the complete code for the MainActivity.Java file.

MainActivity.Java

filter_none

edit
close

play_arrow

link
brightness_4
code

package org.geeksforgeeks.project;
  
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.MobileAds;
import com.google.android.gms.ads.reward.RewardItem;
import com.google.android.gms.ads.reward.RewardedVideoAd;
import com.google.android.gms.ads.reward.RewardedVideoAdListener;
  
public class MainActivity extends AppCompatActivity {
  
    // Creating a object of Button class
    Button showVideoAdBtn;
  
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
  
        // link those objects with their respective id's
        // that we have given in activity_main.xml file
        showVideoAdBtn
            = (Button)findViewById(R.id.showVideoBtn);
  
        // initializing the Google Admob  SDK
        MobileAds.initialize(this);
  
        // loading Video Ad
        loadRewardedVideoAd();
  
        // click listener to show Rewarded Video  Ad
        showVideoAdBtn.setOnClickListener(
            new View.OnClickListener() {
                @Override public void onClick(View view)
                {
                    // showing Ad
                    showRewardedVideoAd();
                }
            });
    }
  
    // creating RewardedVideoAd object
    private RewardedVideoAd AdMobrewardedVideoAd;
  
    // AdMob Rewarded Video Ad Id
    private String AdId
        = "ca-app-pub-3940256099942544/5224354917";
  
    void loadRewardedVideoAd()
    {
        // initializing RewardedVideoAd Object
        // RewardedVideoAd  Constructor Takes Context as its
        // Argument
        AdMobrewardedVideoAd
            = MobileAds.getRewardedVideoAdInstance(this);
  
        // Rewarded Video Ad Listener
        AdMobrewardedVideoAd.setRewardedVideoAdListener(
            new RewardedVideoAdListener() {
                @Override
                public void onRewardedVideoAdLoaded()
                {
                    // Showing Toast Message
                    Toast
                        .makeText(MainActivity.this,
                                  "onRewardedVideoAdLoaded",
                                  Toast.LENGTH_SHORT)
                        .show();
                }
  
                @Override
                public void onRewardedVideoAdOpened()
                {
                    // Showing Toast Message
                    Toast
                        .makeText(MainActivity.this,
                                  "onRewardedVideoAdOpened",
                                  Toast.LENGTH_SHORT)
                        .show();
                }
  
                @Override
                public void onRewardedVideoStarted()
                {
                    // Showing Toast Message
                    Toast
                        .makeText(MainActivity.this,
                                  "onRewardedVideoStarted",
                                  Toast.LENGTH_SHORT)
                        .show();
                }
  
                @Override
                public void onRewardedVideoAdClosed()
                {
                    // Showing Toast Message
                    Toast
                        .makeText(MainActivity.this,
                                  "onRewardedVideoAdClosed",
                                  Toast.LENGTH_SHORT)
                        .show();
                }
  
                @Override
                public void onRewarded(
                    RewardItem rewardItem)
                {
                    // Showing Toast Message
                    Toast
                        .makeText(MainActivity.this,
                                  "onRewarded",
                                  Toast.LENGTH_SHORT)
                        .show();
                }
  
                @Override
                public void
                onRewardedVideoAdLeftApplication()
                {
                    // Showing Toast Message
                    Toast
                        .makeText(
                            MainActivity.this,
                            "onRewardedVideoAdLeftApplication",
                            Toast.LENGTH_SHORT)
                        .show();
                }
  
                @Override
                public void onRewardedVideoAdFailedToLoad(
                    int i)
                {
                    // Showing Toast Message
                    Toast
                        .makeText(
                            MainActivity.this,
                            "onRewardedVideoAdFailedToLoad",
                            Toast.LENGTH_SHORT)
                        .show();
                }
  
                @Override
                public void onRewardedVideoCompleted()
                {
                    // Showing Toast Message
                    Toast
                        .makeText(
                            MainActivity.this,
                            "onRewardedVideoCompleted",
                            Toast.LENGTH_SHORT)
                        .show();
                }
            });
  
        // Loading Rewarded Video Ad
        AdMobrewardedVideoAd.loadAd(
            AdId, new AdRequest.Builder().build());
    }
  
    public void showRewardedVideoAd()
    {
        // Checking If Ad is Loaded or Not
        if (AdMobrewardedVideoAd.isLoaded()) {
            // showing Video Ad
            AdMobrewardedVideoAd.show();
        }
        else {
            // Loading Rewarded Video Ad
            AdMobrewardedVideoAd.loadAd(
                AdId, new AdRequest.Builder().build());
        }
    }
}

chevron_right


Step 5: AndroidManifest.xml file

Below is the complete code for AndroidManifest.xml file.

AndroidManifest.xml

filter_none

edit
close

play_arrow

link
brightness_4
code

<?xml version="1.0" encoding="utf-8"?>
    package="org.geeksforgeeks.project">
  
    <uses-permission android:name="android.permission.INTERNET"/>
  
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
  
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
          
        <!-- meta data  tag for admob Note:replace below app id 
             value to your own app id -->
        <meta-data
            android:name="com.google.android.gms.ads.APPLICATION_ID"
            android:value="ca-app-pub-3940256099942544~3347511713"/>
  
    </application>
  
</manifest>

chevron_right


Output: Run on Emulator


Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


2


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.