How to integrate Admob Rewarded Video Ads in Android Application
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.learnoset.googlerewardedvideoad">
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.GoogleRewardedVideoAd">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!---Always test your ads with test app id -->
<meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="ca-app-pub-3940256099942544~3347511713" />
</application>
</manifest>
gradle.build (app)
dependencies {
// Implement Google Ads Dependency
implementation 'com.google.android.gms:play-services-ads:20.5.0'
}
activity_main.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">
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/showAdBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Show Ad" />
</RelativeLayout>
MainActivity.java
package com.learnoset.googlerewardedvideoad;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import com.google.android.gms.ads.AdError;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.FullScreenContentCallback;
import com.google.android.gms.ads.LoadAdError;
import com.google.android.gms.ads.MobileAds;
import com.google.android.gms.ads.OnUserEarnedRewardListener;
import com.google.android.gms.ads.initialization.InitializationStatus;
import com.google.android.gms.ads.initialization.OnInitializationCompleteListener;
import com.google.android.gms.ads.rewarded.RewardItem;
import com.google.android.gms.ads.rewarded.RewardedAd;
import com.google.android.gms.ads.rewarded.RewardedAdLoadCallback;
public class MainActivity extends AppCompatActivity {
private RewardedAd rewardedAd2 = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button showAdBtn = findViewById(R.id.showAdBtn);
// Add below line in your starting/launcher activity like SplashScreen etc.
MobileAds.initialize(this, new OnInitializationCompleteListener() {
@Override
public void onInitializationComplete(InitializationStatus initializationStatus) {
}
});
// making ad request to google's servers
AdRequest adRequest = new AdRequest.Builder().build();
//replace the test ad unit ID with your original ad unit id. my ad unit id = ca-app-pub-3299203761326949/6263598439
// loading requested ad
RewardedAd.load(this, "ca-app-pub-3940256099942544/5224354917",
adRequest, new RewardedAdLoadCallback() {
@Override
public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
rewardedAd2 = null;
// ad load failed
}
@Override
public void onAdLoaded(@NonNull RewardedAd rewardedAd) {
// assign loaded ad to rewardedAd2
rewardedAd2 = rewardedAd;
// ad loaded successfully
// show ad over the Activity
rewardedAd.show(MainActivity.this, new OnUserEarnedRewardListener() {
@Override
public void onUserEarnedReward(@NonNull RewardItem rewardItem) {
// your reward amount
int rewardAmount = rewardItem.getAmount();
// you can also give custom rewards here Ex.
rewardAmount = 200;
// your reward item type
String rewardType = rewardItem.getType();
}
});
// listening for ad events
rewardedAd.setFullScreenContentCallback(new FullScreenContentCallback() {
@Override
public void onAdShowedFullScreenContent() {
Toast.makeText(MainActivity.this, "Ad is Showing", Toast.LENGTH_SHORT).show();
}
@Override
public void onAdFailedToShowFullScreenContent(AdError adError) {
rewardedAd2 = null;
Toast.makeText(MainActivity.this, "Failed to show ad", Toast.LENGTH_SHORT).show();
}
@Override
public void onAdDismissedFullScreenContent() {
Toast.makeText(MainActivity.this, "Ad closed or dismissed", Toast.LENGTH_SHORT).show();
}
});
}
});
showAdBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// showing ad on button click
// checking if ad is loaded
if (rewardedAd2 != null) {
rewardedAd2.show(MainActivity.this, new OnUserEarnedRewardListener() {
@Override
public void onUserEarnedReward(@NonNull RewardItem rewardItem) {
// getting reward amount
int rewardAmount = rewardItem.getAmount();
// you can also give custom rewards here Ex.
rewardAmount = 200;
// your reward item type
String rewardType = rewardItem.getType();
}
});
}
}
});
}
}
Projects with Source Code + Video Tutorials
You can download our Java and Android Studio Projects with Source Code and Video Tutorials.
Modern Login and Register screen UI design
Simple Music Player App (Version 2) for Android (Offline Version)
Simple Calculator App for Android
If you have any Questions or Queries
You can mail us at info.learnoset@gmail.com
Follow us to learn Coding and get in touch with new Technologies.