Implement Google Admob Interstitial Ads in Android using Kotlin
There are several ways to earn money from mobile applications such as selling products online through applications or simply displaying ads within applications. There are so many types of ads that we can display within our application. Google provides us a service known as Google Admob with the help of which we can display ads within our application. In this article, we will specifically take a look at How to implement Admob Interstitial ads in Android applications using Kotlin. A sample video is given below to get an idea about what we are going to do in this article.
Note: If you are looking to implement interstitial ads within your android application using Java. Check out the following article: Implement Google Admob Interstitial Ads in Android using Java
Step by Step Implementation
Step 1: Create a New Project in Android Studio
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Kotlin as the programming language.
Step 2: Adding dependency in build.gradle
Navigate to Gradle scripts > build.gradle file and add the below dependency in the dependencies section.
implementation 'com.google.android.gms:play-services-ads:19.3.0'
After adding this dependency simply sync your project to install it.
Step 3: Working with the activity_main.xml file
Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file. Comments are added inside the code to understand the code in more detail.
XML
<? xml version = "1.0" encoding = "utf-8" ?>
< RelativeLayout
android:layout_width = "match_parent"
android:layout_height = "match_parent"
android:orientation = "vertical"
tools:context = ".MainActivity" >
< TextView
android:layout_width = "match_parent"
android:layout_height = "wrap_content"
android:layout_centerInParent = "true"
android:gravity = "center"
android:padding = "5dp"
android:text = "Admob Interstitial Ads Example"
android:textAlignment = "center"
android:textColor = "@color/black"
android:textSize = "20sp" />
</ RelativeLayout >
|
Step 4: Working with the MainActivity.kt file
Go to the MainActivity.kt file and refer to the following code. Below is the code for the MainActivity.kt file. Comments are added inside the code to understand the code in more detail.
Kotlin
package com.gtappdevelopers.kotlingfgproject
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.google.android.gms.ads.AdListener
import com.google.android.gms.ads.AdRequest
import com.google.android.gms.ads.InterstitialAd
import com.google.android.gms.ads.MobileAds
class MainActivity : AppCompatActivity() {
lateinit var interstitialAd: InterstitialAd
lateinit var adRequest: AdRequest
override fun onCreate(savedInstanceState: Bundle?) {
super .onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
MobileAds.initialize( this )
adRequest = AdRequest.Builder().build()
interstitialAd = InterstitialAd( this )
interstitialAd.adUnitId = "ca-app-pub-3940256099942544/1033173712"
interstitialAd.loadAd(adRequest)
interstitialAd.setAdListener(object : AdListener() {
override fun onAdLoaded() {
displayInterstitialAd(interstitialAd)
}
})
}
}
private fun displayInterstitialAd(interstitialAd: InterstitialAd) {
if (interstitialAd.isLoaded) {
interstitialAd.show()
}
}
|
Step 5: Adding metadata in AndroidManifest.xml
Navigate to the manifest > AndroidManifest.xml file and add the below code in the application tag.
XML
< meta-data
android:name = "com.google.android.gms.ads.APPLICATION_ID"
android:value = "ca-app-pub-3940256099942544~3347511713" />
|
Now run your application to see the output of it.
Output:
Last Updated :
12 Jun, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...