Rewarded video ads are full-screen video ads that users have the option of watching in full in exchange for in-app rewards. This guide shows you how to integrate rewarded video ads from AdMob into an Android app.
Prerequisites
- Import the Google Mobile Ads SDK, either by itself or as part of Firebase.
Initialize rewarded video ads
Java
package ... import com.google.android.gms.ads.AdRequest; import com.google.android.gms.ads.MobileAds; import com.google.android.gms.ads.reward.RewardedVideoAd; public class MainActivity extends AppCompatActivity implements RewardedVideoAdListener { private RewardedVideoAd mRewardedVideoAd; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); MobileAds.initialize(this, "ca-app-pub-3940256099942544~3347511713"); // Use an activity context to get the rewarded video instance. mRewardedVideoAd = MobileAds.getRewardedVideoAdInstance(this); // Make sure that the MainActivity class implements the required // interface: RewardedVideoAdListener mRewardedVideoAd.setRewardedVideoAdListener(this); } ... }
Kotlin
package ... import com.google.android.gms.ads.AdRequest import com.google.android.gms.ads.MobileAds import com.google.android.gms.ads.reward.RewardedVideoAd class MainActivity : AppCompatActivity(), RewardedVideoAdListener { private lateinit var mRewardedVideoAd: RewardedVideoAd override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) MobileAds.initialize(this, "ca-app-pub-3940256099942544~3347511713") // Use an activity context to get the rewarded video instance. mRewardedVideoAd = MobileAds.getRewardedVideoAdInstance(this) // Make sure that the MainActivity class implements the required // interface: RewardedVideoAdListener mRewardedVideoAd.rewardedVideoAdListener = this } ... }
A
RewardedVideoAd
object can be retrieved using
MobileAds.getRewardedVideoAdInstance()
.
To be notified of rewarded video lifecycle events, you must implement
RewardedVideoAdListener
.
The rewarded ad listener is set using the
setRewardedVideoAdListener()
method. This listener is automatically notified of events from all the networks
you're mediating. For example, you are notified of a user being rewarded for
watching a video through the
onRewarded()
method on RewardedVideoAdListener
.
Request rewarded video ad
Java
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); MobileAds.initialize(this, "ca-app-pub-3940256099942544~3347511713"); // Use an activity context to get the rewarded video instance. mRewardedVideoAd = MobileAds.getRewardedVideoAdInstance(this); // Make sure that the MainActivity class implements the required interface: // RewardedVideoAdListener mRewardedVideoAd.setRewardedVideoAdListener(this); loadRewardedVideoAd(); } private void loadRewardedVideoAd() { mRewardedVideoAd.loadAd("ca-app-pub-3940256099942544/5224354917", new AdRequest.Builder().build()); }
Kotlin
override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) MobileAds.initialize(this, "ca-app-pub-3940256099942544~3347511713") // Use an activity context to get the rewarded video instance. mRewardedVideoAd = MobileAds.getRewardedVideoAdInstance(this) // Make sure that the MainActivity class implements the required interface: // RewardedVideoAdListener mRewardedVideoAd.rewardedVideoAdListener = this loadRewardedVideoAd() } private fun loadRewardedVideoAd() { mRewardedVideoAd.loadAd("ca-app-pub-3940256099942544/5224354917", AdRequest.Builder().build()) }
Adhering to the singleton design of RewardedVideoAd
, requests to load an ad
should be made to the shared instance.
It is highly recommended that you call
loadAd()
as early as possible (for example, in the onCreate()
method of your Activity)
to allow videos to be preloaded.
Always test with test ads
When building and testing your apps, make sure you use test ads rather than live, production ads. Failure to do so can lead to suspension of your account.
The easiest way to load test ads is to use our dedicated test ad unit ID for Android rewarded video:
ca-app-pub-3940256099942544/5224354917
It's been specially configured to return test ads for every request, and you're free to use it in your own apps while coding, testing, and debugging. Just make sure you replace it with your own ad unit ID before publishing your app.
For more information about how the Mobile Ads SDK's test ads work, see Test Ads.
Set up event notifications
The SDK provides the
RewardedVideoAdListener
interface, which has methods corresponding to the events in a rewarded video
ad's lifecycle. Have your app define a class that implements this interface
and pass it to
setRewardedVideoAdListener
prior to loading an ad.
The code example in Initialize rewarded video ads
already shows how to declare that your class implements
RewardedVideoAdListener
and set the listener on the RewardedVideoAd
object.
Here is a sample implementation of the listener methods:
Java
@Override public void onRewarded(RewardItem reward) { Toast.makeText(this, "onRewarded! currency: " + reward.getType() + " amount: " + reward.getAmount(), Toast.LENGTH_SHORT).show(); // Reward the user. } @Override public void onRewardedVideoAdLeftApplication() { Toast.makeText(this, "onRewardedVideoAdLeftApplication", Toast.LENGTH_SHORT).show(); } @Override public void onRewardedVideoAdClosed() { Toast.makeText(this, "onRewardedVideoAdClosed", Toast.LENGTH_SHORT).show(); } @Override public void onRewardedVideoAdFailedToLoad(int errorCode) { Toast.makeText(this, "onRewardedVideoAdFailedToLoad", Toast.LENGTH_SHORT).show(); } @Override public void onRewardedVideoAdLoaded() { Toast.makeText(this, "onRewardedVideoAdLoaded", Toast.LENGTH_SHORT).show(); } @Override public void onRewardedVideoAdOpened() { Toast.makeText(this, "onRewardedVideoAdOpened", Toast.LENGTH_SHORT).show(); } @Override public void onRewardedVideoStarted() { Toast.makeText(this, "onRewardedVideoStarted", Toast.LENGTH_SHORT).show(); } @Override public void onRewardedVideoCompleted() { Toast.makeText(this, "onRewardedVideoCompleted", Toast.LENGTH_SHORT).show(); }
Kotlin
override fun onRewarded(reward: RewardItem) { Toast.makeText(this, "onRewarded! currency: ${reward.type} amount: ${reward.amount}", Toast.LENGTH_SHORT).show() // Reward the user. } override fun onRewardedVideoAdLeftApplication() { Toast.makeText(this, "onRewardedVideoAdLeftApplication", Toast.LENGTH_SHORT).show() } override fun onRewardedVideoAdClosed() { Toast.makeText(this, "onRewardedVideoAdClosed", Toast.LENGTH_SHORT).show() } override fun onRewardedVideoAdFailedToLoad(errorCode: Int) { Toast.makeText(this, "onRewardedVideoAdFailedToLoad", Toast.LENGTH_SHORT).show() } override fun onRewardedVideoAdLoaded() { Toast.makeText(this, "onRewardedVideoAdLoaded", Toast.LENGTH_SHORT).show() } override fun onRewardedVideoAdOpened() { Toast.makeText(this, "onRewardedVideoAdOpened", Toast.LENGTH_SHORT).show() } override fun onRewardedVideoStarted() { Toast.makeText(this, "onRewardedVideoStarted", Toast.LENGTH_SHORT).show() } override fun onRewardedVideoCompleted() { Toast.makeText(this, "onRewardedVideoCompleted", Toast.LENGTH_SHORT).show() }
Display a rewarded video ad
Java
if (mRewardedVideoAd.isLoaded()) { mRewardedVideoAd.show(); }
Kotlin
if (mRewardedVideoAd.isLoaded) { mRewardedVideoAd.show() }
We recommend that you ensure that a rewarded video ad has finished loading
before attempting to display it. The
isLoaded()
method indicates if the
rewarded video ad request has been successfully fulfilled.
Using a RewardedVideoAdListener to reload an ad
The RewardedVideoAdListener
class's
onRewardedVideoAdClosed()
method is a handy place to load a new rewarded video ad after displaying the
previous one:
Java
@Override public void onRewardedVideoAdClosed() { // Load the next rewarded video ad. loadRewardedVideoAd(); }
Kotlin
override fun onRewardedVideoAdClosed() { loadRewardedVideoAd() }
Forward lifecycle events
To forward the parent Activity's lifecycle events to the RewardedVideoAd
object, call the resume()
, pause()
, and destroy()
methods in the parent
Activity's onResume()
, onPause()
, and onDestroy()
methods respectively.
Here is an example of Activity lifecycle event forwarding:
Java
@Override public void onResume() { mRewardedVideoAd.resume(this); super.onResume(); } @Override public void onPause() { mRewardedVideoAd.pause(this); super.onPause(); } @Override public void onDestroy() { mRewardedVideoAd.destroy(this); super.onDestroy(); }
Kotlin
override fun onPause() { super.onPause() mRewardedVideoAd.pause(this) } override fun onResume() { super.onResume() mRewardedVideoAd.resume(this) } override fun onDestroy() { super.onDestroy() mRewardedVideoAd.destroy(this) }
Integration with Unity
For instructions on integrating rewarded video into a Unity project, see Rewarded Video.
Additional resources
Samples on GitHub
Video tutorial
Codelab
Next steps
Create your own rewarded video ad unit in the AdMob UI.
Try another ad format: