Android AdMob Rewarded Video ad tutorial

How to Integrate AdMob Rewarded Video Ads in Android Tutorial

What is Rewarded Video Ads ?

In this article we are going to talk about rewarded video Ads, a full screen video Ad which can help you monetize your application with AdMob Native Ads SDK.

Using AdMob Native Ads SDK you can display Video Ads in your application directly from google advertisers.

Rewarded video is a full screen Video Ad format which gives user the option to watch Video Ads in exchange of something, usually a game point.

Users have the choice not to watch Video Ads they are not interested in.

We will follow these steps

  1. Setup and Initialize Firebase Ads SDK.
  2. Request Rewarded Video Ads.
  3. Listen to Ad Events.
  4. Implement Activity lifecycle events.
  5. Ask User to Watch Video Ad.
  6. Show Video Ad.
  7. Reward user for watching Video Ads.

Rewarded Video Ads format

Rewarded Video Ads are click to download format Ad.

  • Which means every Rewarded Video Ad has app install button.
  • It also has end screen for every Video Ad like in Youtube videos.
  • User has option to close video Ads but user cannot skip video Ads.
  • If user has reached end screen or clicked install button it means user has watched video and only than developer will get paid.

Overview of Firebase Analytics with AdMob Rewared Video Ads

firebase admob analytics
AdMob Console linked with firebase

Lets start development.

Add Run time permission

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

Add Required dependency

Google Mobiloe Ads SDK (AdMob) is now linked with Firebase to get advantage of Firebase analytics. You just have to link AdMob and Firebase while creating Ad Unit ID’s.

compile 'com.google.firebase:firebase-ads:11.8.0'

Setup Ad unit ID’s

Go to strings.xml file in your project and add below AdMob application ID and Ad Unit ID which you will get after creating a new Ad at AdMob Console.

For now you can use below test Ad ID’s.

<string name="admob_app_id">ca-app-pub-3940256099942544~3347511713</string>
<string name="ad_unit_id">ca-app-pub-3940256099942544/5224354917</string>

Initialize Mobile Ads SDK

// Declare this variable inside your Activity.

private lateinit var mRewardedVideoAd: RewardedVideoAd

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

// It is recommended to use application context when initializing MobileAds SDK.

MobileAds.initialize(getApplicationContext(),
        getString(R.string.admob_app_id));

// Use activity context when getting instance of Rewarded video class.

mRewardedVideoAd = MobileAds.getRewardedVideoAdInstance(this)
mRewardedVideoAd.rewardedVideoAdListener = this

loadRewardedVideoAd()



} // onCreate() ends here

Request Rewarded Video Ads

private fun loadRewardedVideoAd() {
    mRewardedVideoAd.loadAd("ca-app-pub-3940256099942544/5224354917",
            AdRequest.Builder().build())
}

Enable Test Devices

If you are using emulator you do not need to do anything because emulators are automatically configured as test devices.

But if you are using real device for testing Rewarded Video Ads you need to provide test ad id while loading Ad.

I/Ads: Use AdRequest.Builder.addTestDevice("A68AD83F45A28BE7DB81F1FBAF84564D") to get test ads on this device.

You will find above in Android Studio Logcat. replace loadRewardedVideoAd() with below code.

private fun loadRewardedVideoAd() {

    mRewardedVideoAd.loadAd("ca-app-pub-3940256099942544/5224354917",
            AdRequest.Builder().addTestDevice("A68AD83F45A28BE7DB81F1FBAF84564D").build())
}

Listen to Ad Events

Implement RewardedVideoAdListener Interface in your Activity class. This interface has methods corresponding Video Ad lifecycle.

override fun onRewardedVideoAdClosed() {
    Log.d(this.localClassName, "on rewarded video Ad Closed")
}

override fun onRewardedVideoAdLeftApplication() {
    Log.d(this.localClassName, "on rewarded video Ad left application")
}

override fun onRewardedVideoAdLoaded() {
    Log.d(this.localClassName, "on rewarded video Ad loaded")
}

override fun onRewardedVideoAdOpened() {
    Log.d(this.localClassName, "on rewarded video Ad opened")
}

override fun onRewarded(p0: RewardItem?) {

    Log.d(this.localClassName, "on rewarded video Ad Rewared")
}

override fun onRewardedVideoStarted() {

    Log.d(this.localClassName, "on rewarded video Ad Started")
}

override fun onRewardedVideoAdFailedToLoad(p0: Int) {
    Log.d(this.localClassName, "on rewarded video Ad Failed to load")

}

Implement Activity lifecycle events

override fun onPause() {
    super.onPause()
    mRewardedVideoAd.pause(this)
}

override fun onResume() {
    super.onResume()
    mRewardedVideoAd.resume(this)
}

override fun onDestroy() {
    super.onDestroy()
    mRewardedVideoAd.destroy(this)
}

Show video Ad to user

Do not explicitly call this function and show Video Ad to user.

Add a button watch video Ad and when user click to watch Video Ad only than show Ad to user.

private fun rewardDeveloperShowVideoAds() {
    if (mRewardedVideoAd.isLoaded) {
        mRewardedVideoAd.show()
    }
}

Conclusion

You have learned about Firebase Rewared Video AdMob Ads and how to integrate AdMob Rewarded Video Ads in your Android application using Kotlin programming language.

Recommended Reading 

How to show AdMob Native Advanced Ads in Android

Post Created 3

Leave a Reply

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

Related Posts

Begin typing your search above and press enter to search. Press ESC to cancel.

Back To Top