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" > <!--on below line we are creating a simple text view--> < 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() { // on below line we are creating a // variable for ad view and ad request lateinit var interstitialAd: InterstitialAd lateinit var adRequest: AdRequest override fun onCreate(savedInstanceState: Bundle?) { super .onCreate(savedInstanceState) setContentView(R.layout.activity_main) // on below line we are // initializing our mobile ads. MobileAds.initialize( this ) // on below line we are // initializing our ad request. adRequest = AdRequest.Builder().build() // on below line we are // initializing our interstitial ad. interstitialAd = InterstitialAd( this ) // on below line we are setting ad // unit id for our interstitial ad. interstitialAd.adUnitId = "ca-app-pub-3940256099942544/1033173712" // on below line we are loading // our ad with ad request interstitialAd.loadAd(adRequest) // on below line we are setting ad // listener for our interstitial ad. interstitialAd.setAdListener(object : AdListener() { override fun onAdLoaded() { // on below line we are calling display // ad function to display interstitial ad. displayInterstitialAd(interstitialAd) } }) } } // on below line we are creating a // function for displaying interstitial ad. private fun displayInterstitialAd(interstitialAd: InterstitialAd) { // on below line we are // checking if the ad is loaded if (interstitialAd.isLoaded) { // if the ad is loaded we are displaying // interstitial ad by calling show method. 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
<!--on below line we are specifying a test admob application id--> < 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:
Please Login to comment...