Google AdMob Interstitial Ad Tutorial (New /Java Guide)
Step by Step guide for AdMob Interstitial Ad implementation
Google AdMob offers a wide variety of ad formats, Banner, Adaptive Banner, and Rewarded Interstitial ads are a great way for making immeasurable revenue from your apps or games.
An interstitial ad is a full-screen ad that covers the entire area of your mobile app screen. When an app shows an interstitial ad, the user has the opportunity to either tap on the ad and follow to its destination or close it using a cross button at the top left corner of the ad.
Prerequisite for following this tutorial
- Google Mobile Ads SDK 20.2.0 or higher
- Use Android Studio 3.2 or higher
- compileSdkVersion 28 or higher
- Optional: AdMob Account (register only when your app is in production)
Example project-level build.gradle (excerpt)
allprojects {
repositories {
google()
}
}
Now time to create the Android project.
Let’s start with an Empty Activity
Name : InterAdExample
Language : Java
Minimum SDK : Android API 21: 5.0 Lollipop
Prepare the MainActivity layout
Add button to the bottom centre of a constraint layout, name this button as ‘Show’ give an id ‘showAd’.
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:layout_marginBottom="16dp"
android:text="Show"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Insert AdMob App Id (Use Test Id) to AndroidManifest.xml file
<meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="ca-app-pub-3940256099942544~3347511713"/>
Final AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.bigknol.interadexample">
<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.InterAdExample">
<meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="ca-app-pub-3940256099942544~3347511713" />
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Add required dependency to build.gradle(:app)
implementation 'com.google.android.gms:play-services-ads:20.2.0'
Final build.gradle (:app) file will look like below
plugins {
id 'com.android.application'
}
android {
compileSdkVersion 30
buildToolsVersion "30.0.3"
defaultConfig {
applicationId "com.bigknol.interadexample"
minSdkVersion 21
targetSdkVersion 30
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
dependencies {
implementation 'com.google.android.gms:play-services-ads:20.2.0'
implementation 'androidx.appcompat:appcompat:1.3.0'
implementation 'com.google.android.material:material:1.3.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
}
Initialise AdMob Interstitial Ad
private InterstitialAd mInterAd;
MobileAds.initialize(this, new OnInitializationCompleteListener() {
@Override
public void onInitializationComplete(InitializationStatus initializationStatus) {
}
});
MainActivity.java [Not Final Version]
package com.bigknol.interadexample;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
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.interstitial.InterstitialAd;
import com.google.android.gms.ads.interstitial.InterstitialAdLoadCallback;
public class MainActivity extends AppCompatActivity {
private InterstitialAd mInterAd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AdRequest adRequest = new AdRequest.Builder().build();
InterstitialAd.load(this,"ca-app-pub-3940256099942544/1033173712", adRequest,
new InterstitialAdLoadCallback() {
@Override
public void onAdLoaded(@NonNull InterstitialAd interstitialAd) {
// an ad is loaded.
mInterAd = interstitialAd;
Log.i("MainActivity", "onAdLoaded");
}
@Override
public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
Log.i("MainActivity", loadAdError.getMessage());
mInterAd = null;
}
});
}
}
Initialise button for calling the interstitial ad
Button button = findViewById(R.id.showAd);
Show the AdMob Interstitial Ad
//show ad
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mInterAd != null) {
mInterAd.show(MainActivity.this);
} else {
Log.d("TAG", "The interstitial ad wasn't ready yet.");
}
}
});
Final version of MainActivity.java
package com.bigknol.interadexample;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
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.interstitial.InterstitialAd;
import com.google.android.gms.ads.interstitial.InterstitialAdLoadCallback;
public class MainActivity extends AppCompatActivity {
private InterstitialAd mInterAd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = findViewById(R.id.showAd);
AdRequest adRequest = new AdRequest.Builder().build();
InterstitialAd.load(this,"ca-app-pub-3940256099942544/1033173712", adRequest,
new InterstitialAdLoadCallback() {
@Override
public void onAdLoaded(@NonNull InterstitialAd interstitialAd) {
// an ad is loaded.
mInterAd = interstitialAd;
Log.i("MainActivity", "onAdLoaded");
}
@Override
public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
Log.i("MainActivity", loadAdError.getMessage());
mInterAd = null;
}
});
// show ad
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mInterAd != null) {
mInterAd.show(MainActivity.this);
} else {
Log.d("TAG", "The interstitial ad wasn't ready yet.");
}
}
});
}
}
Final output of the project
{{CODEinterAd}}