1. What are AdMob Rewarded Ads?
| 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:
Sync the project.
4. Add AdMob App ID in AndroidManifest.xml
Inside <application> tag:
And in res/values/strings.xml:
5. Initialize Mobile Ads (Usually in Application or first Activity)
In onCreate() of your Application class or main Activity, once in app:
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()
}
}
- 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
No comments:
Post a Comment