Open In App

Integrate Google Admob Rewarded Video Ads in Android using Kotlin

Last Updated : 03 Jun, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

There are many ways with the help of which we can earn money from our android applications such as by selling products or services inside our applications or by displaying ads. There are several types of ads that we can display within our application to earn money. The several types of ads consist of banner ads, reward video ads, native ads, and others. In this article, we will take a look at the implementation of Reward Video Ad in our android application using Kotlin. 

Google Admob Rewarded Video Ads

 

Note: If you are looking to implement Reward Video ads within your android application. Check out the following article: Google Admob Reward Video Ads in Android using Java

Step by Step Implementation

Step 1: Create a New Project in Android Studio

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Kotlin as the programming language.

Step 2: Adding dependency in build.gradle

Navigate to Gradle scripts > build.gradle file and add the below dependency in the dependencies section. 

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

After adding this dependency simply sync your project to install it. 

Step 3: Working with the activity_main.xml file

Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file. Comments are added inside the code to understand the code in more detail.

XML




<?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"
    android:orientation="vertical"
    tools:context=".MainActivity">
 
    <!--on below line we are creating a simple
         button for opening a reward video ads -->
    <Button
        android:id="@+id/idBtnRewardVideo"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_margin="20dp"
        android:text="Show Reward Video Ad"
        android:textAllCaps="false" />
 
</RelativeLayout>


Step 4: Working with the MainActivity.kt file

Go to the MainActivity.kt file and refer to the following code. Below is the code for the MainActivity.kt file. Comments are added inside the code to understand the code in more detail.

Kotlin




package com.gtappdevelopers.kotlingfgproject
 
import android.os.Bundle
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
 
class MainActivity : AppCompatActivity(), RewardedVideoAdListener {
 
    // on below line we are creating
    // a variable for button.
    lateinit var rewardVideoBtn: Button
 
    // on below line we are creating
    // a variable for reward video ad.
    lateinit var rewardVideoAds: RewardedVideoAd
 
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
         
        // initializing variables of below line with their id.
        rewardVideoBtn = findViewById(R.id.idBtnRewardVideo)
         
        // on the below line we are initializing our mobile ads.
        MobileAds.initialize(this)
         
        // on below line we are initializing our reward video ad.
        rewardVideoAds = MobileAds.getRewardedVideoAdInstance(this)
        
        // on below line we are setting reward video ad listener
        rewardVideoAds.rewardedVideoAdListener = this
         
        // on below line we are adding click
        // listener for our reward video button.
        rewardVideoBtn.setOnClickListener {
            // on below line we are calling
            // method to display ads.
            displayAds()
        }
    }
 
    private fun displayAds() {
        // on below line we are creating a
        // variable for ad request.
        val request = AdRequest.Builder().build()
         
        // on below line we are setting test ad id and loading our ad.
        rewardVideoAds.loadAd("ca-app-pub-3940256099942544/5224354917", request)
         
        // on below line we are checking if the reward video ad is loaded.
        if (rewardVideoAds.isLoaded) {
            // if ad is loaded we have to
            // simply show our reward video ad.
            rewardVideoAds.show()
        }
    }
 
    // on below line we are calling
    // function which is use to load ad.
    override fun onRewardedVideoAdLoaded() {
        // when ad is loaded we have
        // to simply show the ad.
        rewardVideoAds.show()
    }
 
    // below function is called when reward video ad is
    // opened and we are simply displaying a toast message.
    override fun onRewardedVideoAdOpened() {
        Toast.makeText(this@MainActivity, "Ad opened..", Toast.LENGTH_LONG).show()
    }
 
    // below function is called when reward video is
    // started and we are displaying a toast message
    override fun onRewardedVideoStarted() {
        Toast.makeText(this@MainActivity, "Video started..", Toast.LENGTH_LONG).show()
    }
 
    // below function is called when reward
    // video ad is closed by the user.
    override fun onRewardedVideoAdClosed() {
        Toast.makeText(this@MainActivity, "Ad closed..", Toast.LENGTH_LONG).show()
    }
 
    // below function is called when user
    // is rewarded with the reward.
    override fun onRewarded(p0: RewardItem?) {
        Toast.makeText(this@MainActivity, "User rewarded..", Toast.LENGTH_LONG).show()
    }
 
    // below method is called when user has
    // left reward video ad has left.
    override fun onRewardedVideoAdLeftApplication() {
        Toast.makeText(this@MainActivity, "Ad left application..", Toast.LENGTH_LONG).show()
    }
 
    // below method is called when reward
    // video ad is failed to load.
    override fun onRewardedVideoAdFailedToLoad(p0: Int) {
        Toast.makeText(this@MainActivity, "Fail to load ad..", Toast.LENGTH_LONG).show()
    }
 
    // below method is called when
    // reward video is completed.
    override fun onRewardedVideoCompleted() {
        Toast.makeText(this@MainActivity, "Reward video completed..", Toast.LENGTH_LONG).show()
    }
     
}


Step 5: Adding metadata in AndroidManifest.xml

Navigate to the manifest > AndroidManifest.xml file and add the below code in the application tag. 

XML




<!--on below line we are specifying a test admob application id-->    
<meta-data
     android:name="com.google.android.gms.ads.APPLICATION_ID"
     android:value="ca-app-pub-3940256099942544~3347511713"
/>


Now run your application to see the output of it. 

Output: 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads