Implement Google Admob Banner Ads in Android using Kotlin
Last Updated :
02 Jun, 2022
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 Banner ads in Android applications using Kotlin.
Admob Banner Ads
Note: If you are looking to integrate Admob Banner ads within your android application using Java. Check out the following article: Admob Banner 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 this 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:text = "Admob Banner Ads Example"
android:textAlignment = "center"
android:textColor = "@color/black"
android:textSize = "20sp" />
< com.google.android.gms.ads.AdView
android:id = "@+id/adView"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:layout_alignParentBottom = "true"
android:layout_centerHorizontal = "true"
ads:adSize = "BANNER"
ads:adUnitId = "ca-app-pub-3940256099942544/6300978111" />
</ 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.AdRequest
import com.google.android.gms.ads.AdView
import com.google.android.gms.ads.MobileAds
class MainActivity : AppCompatActivity() {
lateinit var adView: AdView
lateinit var adRequest: AdRequest
override fun onCreate(savedInstanceState: Bundle?) {
super .onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
MobileAds.initialize( this )
adView = findViewById(R.id.adView)
adRequest = AdRequest.Builder().build()
adView.loadAd(adRequest)
}
}
|
Now simply run your application to see the output of it.
Output:
Share your thoughts in the comments
Please Login to comment...