Open In App

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?

Formats of Google AdMob

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



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

Rewarded Video:



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




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

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

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

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




<?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>

Step 4: Working with MainActivity.java file

// Creating a object of Button class

Button showVideoAdBtn;

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

// creating object of RewardedVideoAd

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

     // Loading Rewarded Video Ad

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

}

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

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

       }

   }

// click listener to show Rewarded Video  Ad

showVideoAdBtn.setOnClickListener(new View.OnClickListener() {

          @Override

          public void onClick(View view) {

              showRewardedVideoAd();

          }

 });

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

           }

});




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

Step 5: AndroidManifest.xml file

Below is the complete code for AndroidManifest.xml file.




<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    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>

Output: Run on Emulator


Article Tags :