To integrate any ad format, you must first have completed the instructions in the basic integration article.
Rewarded ads are a full screen experience where users opt in to view a video ad in exchange for something of value, such as virtual currency, in-app items, exclusive content, and more. The ad experience grants the reward after the user has watched the video for a specified duration. When that duration has been reached, your app receives a callback to grant the reward to the user.
Create and Load a Rewarded Ad
import com.vungle.ads.RewardedAd
private var rewardedAd: RewardedAd? = null
rewardedAd = RewardedAd(requireContext(), placementId, AdConfig().apply {
}).apply {
adListener = this@RewardedFragment
load()
}
import com.vungle.ads.RewardedAd;
private RewardedAd rewardedAd;
rewardedAd = new RewardedAd(requireContext(), placementId, new AdConfig());
rewardedAd.setAdListener(this);
rewardedAd.load();Play a Rewarded Ad
-
Once the rewarded ad is successfully loaded, you can display it by calling:
if (rewardedAd?.canPlayAd() == true) { rewardedAd?.play() }if (rewardedAd != null && rewardedAd.canPlayAd()) { rewardedAd.play(); } - During the time the rewarded ad is visible to the user, you can listen for ad lifecycle events through delegate callback methods (refer to the Register for Callbacks section below).
- The ad is closed when the user closes it. You do not need to add steps in your code to close it.
Register for Callbacks
To receive notifications for ad events, declare conformance to RewardedAdListener protocol and implement callback methods.
override fun onAdLoaded(baseAd: BaseAd) {
Log.d(TAG, "Creative id:" + baseAd.creativeId)
}
override fun onAdStart(baseAd: BaseAd) {
}
override fun onAdImpression(baseAd: BaseAd) {
}
override fun onAdEnd(baseAd: BaseAd) {
}
override fun onAdClicked(baseAd: BaseAd) {
}
override fun onAdLeftApplication(baseAd: BaseAd) {
}
override fun onAdRewarded(baseAd: BaseAd) {
}
override fun onAdFailedToLoad(baseAd: BaseAd, adError: VungleError) {
}
override fun onAdFailedToPlay(baseAd: BaseAd, adError: VungleError) {
}
@Override
public void onAdClicked(@NonNull BaseAd baseAd) {
}
@Override
public void onAdEnd(@NonNull BaseAd baseAd) {
}
@Override
public void onAdFailedToLoad(@NonNull BaseAd baseAd, @NonNull VungleError vungleError) {
}
@Override
public void onAdFailedToPlay(@NonNull BaseAd baseAd, @NonNull VungleError vungleError) {
}
@Override
public void onAdImpression(@NonNull BaseAd baseAd) {
}
@Override
public void onAdLeftApplication(@NonNull BaseAd baseAd) {
}
@Override
public void onAdLoaded(@NonNull BaseAd baseAd) {
Log.d(TAG, "Creative id:" + baseAd.getCreativeId());
}
@Override
public void onAdStart(@NonNull BaseAd baseAd) {
}
@Override
public void onAdRewarded(@NonNull BaseAd baseAd) {
}
The following table lists all the available AdConfig options:
| Option | Description |
|---|---|
setAdOrientation |
|
setBackButtonImmediatelyEnabled |
Sets whether the Android back button will be immediately enabled during the video ad, or will be inactive until the on screen close button appears (this latter is the default). |
Test a Rewarded Ad
You can test a Rewarded ad in one of two ways: by setting your app status to Test Mode so that Vungle can deliver test ads to your app, or by adding a test device to your app so that Vungle can deliver test ads specifically to the test device. Follow the instructions in Test Your Integration: Test Mode and Test Devices.