AdMob Rewarded Interstitial Ads Integration Tutorial
Rewarded Interstitial ad is new to the AdMob app monetization platform. We can offer coins or points after the natural transition of your apps. Your app users get instantaneous rewards after watching the Rewarded Interstitial ads. Unlike rewarded ads, users aren’t obligated to opt-in to view a rewarded interstitial.
How to integrate AdMob Rewarded Interstitial Ad in your Android app?
Prerequisites
You need to update the Android Manifest file (permission)
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
- Use Android Studio 3.2 or higher
- minSdkVersion 16 or higher
- compileSdkVersion 28 or higher
Step 1: Changes to project-level build.gradle
allprojects {
repositories {
google()
}
}
Open and edit the app-level build.gradle with new Mobile Ads SDK
dependencies {
implementation 'com.google.android.gms:play-services-ads:19.8.0'
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'com.google.android.material:material:1.3.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
}
Step 2 : Update your AndroidManifest.xml file
You must include the AdMob app ID in AndroidManifest.xml, If you don’t have an ID create a new AdMob account. You can follow this tutorial without having an AdMob account. Google AdMob provides a sample app ID, ad unit id for testing. In a real app, you must use your actual AdMob app ID.
<manifest>
<application>
<meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="ca-app-pub-3940256099942544~3347511713"/>
</application>
</manifest>
Before We start …
Updated Manifest File
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.bigknol.example.rewardinterstitialadexample">
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<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.RewardInterstitialAdExample">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="ca-app-pub-3940256099942544~3347511713" />
</application>
</manifest>
Create an intro screen that provides clear reward messaging and an option to skip the ad before it starts.
Layout for an Intro Screen
Android Studio > layouts > New > Layout Resource File > intro_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="350dp"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_gravity="center"
android:layout_height="wrap_content">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="220dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
app:cardCornerRadius="15dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/btnNoThanks"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_marginStart="16dp"
android:layout_marginBottom="8dp"
android:background="@drawable/button_background"
android:text="@string/no_thanks"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/btnVideoStarts"
android:layout_width="180dp"
android:layout_height="40dp"
android:layout_marginEnd="24dp"
android:layout_marginBottom="8dp"
android:background="@drawable/button_background"
android:text="@string/video_starting_in"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Out of points!\n Watch this video and receive 2 more points"
android:textAlignment="center"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
</androidx.constraintlayout.widget.ConstraintLayout>
button_background.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:angle="225"
android:startColor="#DDF7BE07"
android:endColor="#F2DA51"/>
<corners
android:bottomLeftRadius="5dp"
android:bottomRightRadius="5dp"
android:topLeftRadius="5dp"
android:topRightRadius="5dp" />
</shape>
strings.xml
<resources>
<string name="app_name">RewardInterstitialAdExample</string>
<string name="no_thanks">No thanks</string>
<string name="video_starting_in">Video starting in</string>
</resources>
Rewarded Interstitial Ad Implementation
Load Ad
Loading an ad is accomplished using the static load()
method on the RewardedInterstitialAd
class
Sample Snippet from official AdMob Doc.
public class MainActivity extends AppCompatActivity {
private RewardedInterstitialAd rewardedInterstitialAd;
private String TAG = "MainActivity";
@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) {
loadAd();
}
});
public void loadAd() {
// Use the test ad unit ID to load an ad.
RewardedInterstitialAd.load(MainActivity.this, ca-app-pub-3940256099942544/5354046379,
new AdRequest.Builder().build(), new RewardedInterstitialAdLoadCallback() {
@Override
public void onAdLoaded(RewardedInterstitialAd ad) {
rewardedInterstitialAd = ad;
Log.e(TAG, "onAdLoaded");
}
@Override
public void onAdFailedToLoad(LoadAdError loadAdError) {
Log.e(TAG, "onAdFailedToLoad");
}
});
}
}
Complete Code MainActivity.java
package com.bigknol.example.rewardinterstitialadexample;
import android.app.Dialog;
import android.content.DialogInterface;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
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.rewardedinterstitial.RewardedInterstitialAd;
import com.google.android.gms.ads.rewardedinterstitial.RewardedInterstitialAdLoadCallback;
import java.util.Locale;
import java.util.concurrent.TimeUnit;
public class MainActivity extends AppCompatActivity implements OnUserEarnedRewardListener {
Dialog dialog;
int value = 0;
private RewardedInterstitialAd rewardedInterstitialAd;
private String TAG = "MainActivity";
private Button showAd;
private TextView points;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
showAd = (Button) findViewById(R.id.showAd);
points = (TextView) findViewById(R.id.point);
dialog = new Dialog(this);
dialog.setCanceledOnTouchOutside(true);
MobileAds.initialize(this, new OnInitializationCompleteListener() {
@Override
public void onInitializationComplete(InitializationStatus initializationStatus) {
loadAd();
}
});
showAd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
openIntroDialog();
}
});
}
public void loadAd() {
// Use the test ad unit ID to load an ad.
RewardedInterstitialAd.load(MainActivity.this, "ca-app-pub-3940256099942544/5354046379",
new AdRequest.Builder().build(), new RewardedInterstitialAdLoadCallback() {
@Override
public void onAdLoaded(RewardedInterstitialAd ad) {
rewardedInterstitialAd = ad;
rewardedInterstitialAd.setFullScreenContentCallback(new FullScreenContentCallback() {
/** Called when the ad failed to show full screen content. */
@Override
public void onAdFailedToShowFullScreenContent(AdError adError) {
Log.i(TAG, "onAdFailedToShowFullScreenContent");
}
/** Called when ad showed the full screen content. */
@Override
public void onAdShowedFullScreenContent() {
Log.i(TAG, "onAdShowedFullScreenContent");
}
/** Called when full screen content is dismissed. */
@Override
public void onAdDismissedFullScreenContent() {
Log.i(TAG, "onAdDismissedFullScreenContent");
loadAd(); // don't know how useful
}
});
}
@Override
public void onAdFailedToLoad(LoadAdError loadAdError) {
Log.e(TAG, "onAdFailedToLoad");
}
});
}
@Override
public void onUserEarnedReward(@NonNull RewardItem rewardItem) {
Log.i(TAG, "onUserEarnedReward");
my_reward();
}
public void my_reward() {
value = Integer.parseInt(points.getText().toString());
value = value + 1;
points.setText(String.valueOf(value));
}
private void openIntroDialog() {
dialog.setContentView(R.layout.intro_layout);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
Button btnNoThanks = dialog.findViewById(R.id.btnNoThanks);
Button btnVideoStarts = dialog.findViewById(R.id.btnVideoStarts);
btnNoThanks.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
dialog.dismiss();
}
});
btnVideoStarts.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
showAdNow();
dialog.dismiss();
}
});
//optional
dialog.setOnShowListener(new DialogInterface.OnShowListener() {
private static final int AUTO_MILLS = 6000;
@Override
public void onShow(DialogInterface dialogInterface) {
final CharSequence videoStartsButtonText = btnVideoStarts.getText();
new CountDownTimer(AUTO_MILLS, 100) {
@Override
public void onTick(long l) {
btnVideoStarts.setText(String.format(
Locale.getDefault(), "%s(%d)",
videoStartsButtonText,
TimeUnit.MILLISECONDS.toSeconds(l) + 1
));
}
@Override
public void onFinish() {
if (dialog.isShowing()) {
// dialog.dismiss();
btnVideoStarts.setText("Watch now");
}
}
}.start();
}
});
//optional ends
dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialogInterface) {
dialog.cancel();
}
});
dialog.show();
}
private void showAdNow() {
if (rewardedInterstitialAd != null) {
rewardedInterstitialAd.show(MainActivity.this, MainActivity.this);
} else {
Toast.makeText(MainActivity.this, "Ad Not Loaded", Toast.LENGTH_SHORT).show();
}
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
tools:context=".MainActivity">
<Button
android:id="@+id/showAd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show me"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="@+id/point"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:textSize="30sp"
app:layout_constraintBottom_toTopOf="@+id/showAd"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
How does our rewarded interstitial ad work?
{{CODE1}}