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
Rewarded Video Ads are click to download format Ad.
Lets start development.
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
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'
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>
// 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
private fun loadRewardedVideoAd() {
mRewardedVideoAd.loadAd("ca-app-pub-3940256099942544/5224354917",
AdRequest.Builder().build())
}
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())
}
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")
}
override fun onPause() {
super.onPause()
mRewardedVideoAd.pause(this)
}
override fun onResume() {
super.onResume()
mRewardedVideoAd.resume(this)
}
override fun onDestroy() {
super.onDestroy()
mRewardedVideoAd.destroy(this)
}
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