AdMob Rewarded Ad Implementation in Android Studio (Kotlin) – Step-by-Step Guide

1. What are AdMob Rewarded Ads?

AdMob Rewarded Ad Implementation
Image by: iion

AdMob rewarded ads let users watch a full-screen video (or interactive ad) in exchange for a reward:

  • Unlock a premium feature

  • Enable “Pro” settings (like volume-button scrolling)

  • Give in-app coins, hints, or extra lives

They’re perfect for non-intrusive monetization: user chooses to watch the ad instead of you forcing it.


2. Prerequisites

  • Android Studio installed

  • A project using Kotlin

  • Google Mobile Ads SDK added

  • Your AdMob App ID and Rewarded Ad Unit ID (use test IDs during development)


3. Add the Google Mobile Ads SDK

3.1. Add Gradle dependency

In your app-level build.gradle:

dependencies {
implementation("com.google.android.gms:play-services-ads:24.8.0") // or latest
}

Sync the project.


4. Add AdMob App ID in AndroidManifest.xml

Inside <application> tag:

<meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="@string/admob_app_id" />

And in res/values/strings.xml:

<!-- AdMob Test IDs -->
<string name="admob_app_id">ca-app-pub-3940256099942544~3347511713</string>
<string name="admob_banner_ad_unit_id">ca-app-pub-3940256099942544/6300978111</string>
<string name="admob_native_ad_unit_id">ca-app-pub-3940256099942544/2247696110</string>
<string name="admob_interstitial_ad_unit_id">ca-app-pub-3940256099942544/1033173712</string>
<string name="admob_rewarded_ad_unit_id">ca-app-pub-3940256099942544/5224354917</string>
<string name="admob_app_open_ad_unit_id">ca-app-pub-3940256099942544/9257395921</string>

5. Initialize Mobile Ads (Usually in Application or first Activity)

In onCreate() of your Application class or main Activity, once in app:

MobileAds.initialize(this) {}

If you don’t have a custom Application class, you can initialize in your main activity’s onCreate() once.


6. Basic Rewarded Ad Flow (Concept)

  • Load rewarded ad → loadRewardedAd()

  • Show dialog → “Watch Ad to Unlock Feature”

  • Show ad → rewardedAd.show()

  • On reward callback → enable your feature

  • Reload a new rewarded ad for next time

We’ll implement this pattern now.


7. Implement Rewarded Ad in Kotlin

  • Initialize at top of onCreate
private var rewardedAd: RewardedAd? = null
  • Call it inside onCreate/onViewCreated
loadRewardedAd()
  • Create functions
// Load Rewarded Ad
private fun loadRewardedAd() {
val adRequest = AdRequest.Builder().build()

RewardedAd.load(
requireContext(),
getString(R.string.admob_rewarded_ad_unit_id),
adRequest,
object : RewardedAdLoadCallback() {
override fun onAdLoaded(ad: RewardedAd) {
rewardedAd = ad
}

override fun onAdFailedToLoad(error: LoadAdError) {
rewardedAd = null
}
}
)
}
// Unlock with Ad dialog
private fun showPremiumFeatureDialog(onRewardEarned: () -> Unit) {
MaterialAlertDialogBuilder(requireContext())
.setTitle("Unlock Premium Feature")
.setMessage("Watch a short ad to delete this comic")
.setPositiveButton(" Watch Ad ") { _, _ ->
showLoadingAndRewardedAd(onRewardEarned)
}
.setNegativeButton("No Thanks", null)
.show()
.apply {
getButton(AlertDialog.BUTTON_POSITIVE)?.apply {
backgroundTintList = ColorStateList.valueOf(
ContextCompat.getColor(requireContext(), R.color.primary)
)
setTextColor(Color.WHITE)
}
}
}
// Show Rewarded Ad
private fun showLoadingAndRewardedAd(onRewardEarned: () -> Unit) {
val loadingDialog = MaterialAlertDialogBuilder(requireContext())
.setTitle("Loading Ad")
.setMessage("Please wait...")
.setCancelable(false)
.create()

loadingDialog.show()

val ad = rewardedAd
if (ad != null) {
ad.fullScreenContentCallback = object : FullScreenContentCallback() {
override fun onAdDismissedFullScreenContent() {
loadingDialog.dismiss()
rewardedAd = null
loadRewardedAd() // reload for next time
}

override fun onAdFailedToShowFullScreenContent(error: AdError) {
loadingDialog.dismiss()
rewardedAd = null
}
}

ad.show(requireActivity()) { _ ->
loadingDialog.dismiss()
onRewardEarned()
}
} else {
loadingDialog.dismiss()
MaterialAlertDialogBuilder(requireContext())
.setTitle("Ad Not Ready")
.setMessage("Please try again in a moment")
.setPositiveButton("OK", null)
.show()
}
}
Note: for Activity change requireContext() to this@yourActivity or this
  • Call showPremiumFeatureDialog() in Action
binding.btnAction.setOnClickListener {
showPremiumFeatureDialog{
// Reward Generated(Next Action)
}
}

8. Imports

import com.google.android.gms.ads.AdRequest
import com.google.android.gms.ads.AdError
import com.google.android.gms.ads.FullScreenContentCallback
import com.google.android.gms.ads.LoadAdError
import com.google.android.gms.ads.rewarded.RewardedAd
import com.google.android.gms.ads.rewarded.RewardedAdLoadCallback
AdMob Rewarded Ad Implementation done.

No comments:

Post a Comment