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
public class MainActivity extends AppCompatActivity implements RewardedVideoAdListener {
private RewardedVideoAd mAd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Use an activity context to get the rewarded video instance.
mAd = MobileAds.getRewardedVideoAdInstance(this);
mAd.setRewardedVideoAdListener(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
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Use an activity context to get the rewarded video instance.
mAd = MobileAds.getRewardedVideoAdInstance(this);
mAd.setRewardedVideoAdListener(this);
loadRewardedVideoAd();
}
private void loadRewardedVideoAd() {
mAd.loadAd("ca-app-pub-3940256099942544/5224354917", new 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 to 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
The sample code above contains an ad unit ID and you're free to request ads with it. It's been specially configured to return test ads rather than production ads for every request, which makes it safe to use.
However, once you register an app in the AdMob UI and create your own ad unit IDs for use in your app, you'll need to explicitly configure your device as a test device when you're developing. This is extremely important. Testing with real ads (even if you never tap on them) is against AdMob policy and can cause your account to be suspended. See Test Ads for information on how you can make sure you always get test ads when developing.
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:
// Required to reward the user.
@Override
public void onRewarded(RewardItem reward) {
Toast.makeText(this, "onRewarded! currency: " + reward.getType() + " amount: " +
reward.getAmount(), Toast.LENGTH_SHORT).show();
// Reward the user.
}
// The following listener methods are optional.
@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();
}
Display an ad
if (mAd.isLoaded()) {
mAd.show();
}
We recommend that you ensure a rewarded video ad has completed loading
before attempting to display it. The
isLoaded()
method indicates if the
rewarded video ad request has been successfully fulfilled.
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:
@Override
public void onResume() {
mAd.resume(this);
super.onResume();
}
@Override
public void onPause() {
mAd.pause(this);
super.onPause();
}
@Override
public void onDestroy() {
mAd.destroy(this);
super.onDestroy();
}
Additional resources
Samples
- Rewarded Video sample app on GitHub
Mobile Ads Garage video tutorials
Codelab
Next steps
- Create your own rewarded video ad unit in the AdMob UI.
- Try another ad format: