Today we will learn , How to implement rewarded video ads in our Android App using Android Studio , so let's start direct coding
follow all steps carefully :
dependency : implementation 'com.google.android.gms:play-services-ads:20.1.0'
meta tag : androidManifest.xml
<meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="@string/admob_id"/>
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="@string/admob_id"/>
String :
<string name="admob_id">ca-app-pub-3940256099942544~3347511713</string>
<string name="rewarded_ads">ca-app-pub-3940256099942544/5224354917</string>
activityMain.xml :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/coins"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp"
android:textSize="25sp" />
<Button
android:id="@+id/showAds"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="Show Ads" />
</RelativeLayout>
MainActivity.java :
private Button click;
private TextView coins;
private RewardedAd mRewardedAd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MobileAds.initialize(this, new OnInitializationCompleteListener() {
@Override
public void onInitializationComplete(InitializationStatus initializationStatus) {
}
});
rewAds();
click = findViewById(R.id.showAds);
coins = findViewById(R.id.coins);
click.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mRewardedAd != null) {
Activity activityContext = MainActivity.this;
mRewardedAd.show(activityContext, new OnUserEarnedRewardListener() {
@Override
public void onUserEarnedReward(@NonNull RewardItem rewardItem) {
// Handle the reward.
int rewardAmount = rewardItem.getAmount();
String rewardType = rewardItem.getType();
String rew= "Amount : " +rewardAmount +" Type : "+rewardType;
coins.setText(rew);
mRewardedAd.setFullScreenContentCallback(new FullScreenContentCallback() {
@Override
public void onAdDismissedFullScreenContent() {
rewAds();
mRewardedAd = null;
super.onAdDismissedFullScreenContent();
}
});
}
});
} else {
Toast.makeText(MainActivity.this, "Ads not Loaded", Toast.LENGTH_SHORT).show();
}
}
});
}
public void rewAds(){
AdRequest adRequest = new AdRequest.Builder().build();
RewardedAd.load(this, getString(R.string.rewarded_ads),
adRequest, new RewardedAdLoadCallback(){
@Override
public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
// Handle the error.
mRewardedAd = null;
}
@Override
public void onAdLoaded(@NonNull RewardedAd rewardedAd) {
mRewardedAd = rewardedAd;
}
});
}
}