AdMob Interstitial Kotlin Implementation Android Studio
Google’s AdMob is one of the most profitable monetization tools for app developers. Moreover, AdMob offers a wide variety of Ad formats that include App Open Ads, Banner Ads, Rewarded Interstitial Ads, and Native Ads.
The interstitial ads are full-screen ads that shroud the app. The interstitial ad can place between two activities. There is a snag! Your app should have a natural transition point.
We have discussed the Kotlin implementation of the AdMob Banner Ad.
Let’s create a new Android Project. Choose the Empty Activity from Android Templates > Empty Activity.
Step 1 Enable ViewBinding in Android Project
Open the build.gradle(:app) file and add the following lines inside the android {} section.
buildFeatures{
viewBinding = true
}
Let’s include the Google Mobile Ads SDK. Open the build.gradle (:app) and head to the dependencies section and finally include the following dependency
implementation 'com.google.android.gms:play-services-ads:21.3.0'
Time to Sync the dependencies.
build.gradle(:app) might look like below
plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
}
android {
namespace 'com.bigknol.interstitialad'
compileSdk 32
defaultConfig {
applicationId "com.bigknol.interstitialad"
minSdk 26
targetSdk 32
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
}
kotlinOptions {
jvmTarget = '1.8'
}
buildFeatures {
viewBinding = true
}
}
dependencies {
implementation 'com.google.android.gms:play-services-ads:21.3.0'
implementation 'androidx.core:core-ktx:1.7.0'
implementation 'androidx.appcompat:appcompat:1.4.1'
implementation 'com.google.android.material:material:1.5.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}
Make sure that App Id is placed under the application tag. (AndroidManifest.xml)
<meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="ca-app-pub-3940256099942544~3347511713"/>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.InterstitialAd"
tools:targetApi="31">
<meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="ca-app-pub-3940256099942544~3347511713"/>
<activity
android:name=".LessonActivity"
android:exported="false">
<meta-data
android:name="android.app.lib_name"
android:value="" />
</activity>
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data
android:name="android.app.lib_name"
android:value="" />
</activity>
</application>
</manifest>
Let’s add a binding object and replace the setContentView in MainActivity.kt
MainActivity.kt
package com.bigknol.interstitialad
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.bigknol.interstitialad.databinding.ActivityMainBinding
class MainActivity : AppCompatActivity() {
lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
val view = binding.root
setContentView(view)
}
}
Create another Activity called ‘LessonActivity‘ and change its TextView to “Lessons”
activity_lesson.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=".LessonActivity">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Lessons"
android:textSize="28sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Add a button to activity_main.xml file
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/buttonNextLesson"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="52dp"
android:text="Next Lesson"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Open MainActivity.kt Let’s initiate InterstitialAd
private var myInterstitialAd: InterstitialAd? = null
Set test AdMob Interstitial As Id for ad request.
const val AD_UNIT_ID = "ca-app-pub-3940256099942544/1033173712"
Initialise MobileAds SDK
MobileAds.initialize(this){}
Let’s make an ad request
val myAdRequest = AdRequest.Builder().build()
MainActivity.kt (Complete Code)
package com.bigknol.interstitialad
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import com.bigknol.interstitialad.databinding.ActivityMainBinding
import com.google.android.gms.ads.AdRequest
import com.google.android.gms.ads.LoadAdError
import com.google.android.gms.ads.MobileAds
import com.google.android.gms.ads.interstitial.InterstitialAdLoadCallback
import com.google.android.gms.ads.interstitial.InterstitialAd
const val AD_UNIT_ID = "ca-app-pub-3940256099942544/1033173712"
class MainActivity : AppCompatActivity() {
lateinit var binding: ActivityMainBinding
// lateinit var myInterstitialAd: InterstitialAd
private var myInterstitialAd: InterstitialAd? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
val view = binding.root
setContentView(view)
MobileAds.initialize(this){}
val myAdRequest = AdRequest.Builder().build()
InterstitialAd.load(this, AD_UNIT_ID, myAdRequest,object: InterstitialAdLoadCallback(){
override fun onAdFailedToLoad(InterAdError: LoadAdError) {
InterAdError.toString().let { Log.d("Error", it) }
}
override fun onAdLoaded(interAd: InterstitialAd) {
Log.d("AdLoad","Ad Loaded")
myInterstitialAd = interAd
}})
binding.buttonNextLesson.setOnClickListener {
if(myInterstitialAd != null)
{
myInterstitialAd?.show(this)
}
else {
Log.d("TAG", "Ad wasn't ready yet.")
}
val nextLessonIntent = Intent(this, LessonActivity::class.java)
startActivity(nextLessonIntent)
}
}
}