How to create a Rewarded Video Ad in android app | Place Rewarded Video Ads
In our Previous Posts, We have created Banner Ad and Interstitial Ad.
In this post, we will create Rewarded Video Ad to show video Ads on button click and also we add reward coins.
For that, first, we create a Rewarded ad in our Admob Account on Admob.com
Rewarded Video Ad |
The button is used to show a video ad and the Text view is used to change the value when your ad finished.
MainActivity.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:id="@+id/helloworld"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Video Ad"
android:id="@+id/VideoAd">
</Button>
</LinearLayout>
Then, we implement the Rewarded video Ad Listener and implement its methods.
Also, we set up our rewarded ad.
MainActivity.java
package studio.harpreet.myadmobads;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.InterstitialAd;
import com.google.android.gms.ads.MobileAds;
import com.google.android.gms.ads.reward.RewardItem;
import com.google.android.gms.ads.reward.RewardedVideoAd;
import com.google.android.gms.ads.reward.RewardedVideoAdListener;
public class MainActivity extends AppCompatActivity implements RewardedVideoAdListener {
Button vbtn;
RewardedVideoAd mAd;
TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
vbtn = findViewById(R.id.VideoAd);
tv = findViewById(R.id.helloworld);
mAd = MobileAds.getRewardedVideoAdInstance(this);
mAd.setRewardedVideoAdListener(this);
vbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AdRequest adRequest2 = new AdRequest.Builder().addTestDevice("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX").build();
mAd.loadAd("ca-app-pub-1858792262020609/9984696229",adRequest2);
}
});
}
@Override
public void onRewardedVideoAdLoaded() {
if (mAd.isLoaded()) {
mAd.show();
}
}
@Override
public void onRewardedVideoAdOpened() {
}
@Override
public void onRewardedVideoStarted() {
}
@Override
public void onRewardedVideoAdClosed() {
}
@Override
public void onRewarded(RewardItem rewardItem) {
Toast.makeText(this, rewardItem.getType() + ": " + rewardItem.getAmount(), Toast.LENGTH_SHORT).show();
}
@Override
public void onRewardedVideoAdLeftApplication() {
}
@Override
public void onRewardedVideoAdFailedToLoad(int i) {
}
@Override
public void onRewardedVideoCompleted() {
tv.setText("5");
}
}
Here, we load our rewarded ad by filling ad unit id. Also, we change value of Text View when our Video Ad is finished.
Follow us for more posts like this,
Subscribe Harpreet studio on Youtube
Like Harpreet Studio on Facebook
Follow me on Instagram
No comments