Home / Android / Rewarded Video / Example of Use

Rewarded Video

Example of minimal use

The minimal usage example is practical and easy to display ads. But in video, especially rewarded, the right thing to do is preload the ad, wait for the right moment to show it and reward the user for seeing it. To do this, listeners must be used.

public class RewardedActivity extends Activity {
TappxRewardedVideo rewardedVideo;
Boolean loading = false;
Boolean isLoad = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
view.findViewById(R.id.button_load).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
loadRewarded();
}
});
view.findViewById(R.id.button_show).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (rewardedVideo != null) {
rewardedVideo.show();
}
});
}
private void loadRewarded() {
if (loading) return;
loading = true;
isLoad = false;
if (rewardedVideo != null) {
rewardedVideo.destroy();
}
rewardedVideo = new TappxRewardedVideo(getActivity(), appKey);
rewardedVideo.setListener(rewardedListener);
rewardedVideo.loadAd();
}
private TappxRewardedVideoListener rewardedListener = new TappxRewardedVideoListener() {
@Override
public void onRewardedVideoLoaded(TappxRewardedVideo rewardedVideo) {
loading = false;
isLoad = true;
}
@Override
public void onRewardedVideoLoadFailed(TappxRewardedVideo rewardedVideo, TappxAdError errorCode) {
loading = false;
}
@Override
public void onRewardedVideoCompleted(TappxRewardedVideo rewardedVideo) {
//In this place you have to reward the user
}
};
@Override
protected void onDestroy() {
super.onDestroy();
if (rewardedVideo!= null) rewardedVideo.destroy();
}
}

To allow more control, ads can also be shown with four steps: *loadAd, isReady, show and onRewardedVideoCompleted.

rewardedVideo.loadAd();

loadAd load all the necessary information to be able to display the ad.

rewardedVideo.isReady();

isReady it is to check that the ad is ready to be shown. It receives a "true" when it is ready and a "false" when it is not.

rewardedVideo.show();

show displays the previously uploaded video ad.

public void onRewardedVideoCompleted(TappxRewardedVideo rewardedVideo) {
//In this place you have to reward the user
}

onRewardedVideoCompleted listener allows the user to reward the user.

This division is necessary in rewarded video. For example, in a game you can load the ad while the user plays a level and later show it when the level ends or pauses the game. In video ads it is much more relevant than in display since the ad is heavier and may take longer to load. For this reason, it's best to use the preload of the ad before it is going to be shown. Also, this will improve the user experience.