Blog purpose for android basic example for android app developer. any query please my contact

Friday, 12 December 2025

How to Implement Google AdMob Banner Ads in Android Using Kotlin

 

Google AdMob is one of the most reliable and widely used mobile advertising platforms for Android app monetization. If you are developing an Android app using Kotlin, integrating banner ads is one of the simplest ways to start earning revenue.

In this article, you will learn how to integrate AdMob Banner Ads using Kotlin, using the official Google test ad unit ID. This step-by-step guide is optimized for SEO and perfect for beginners as well as experienced developers.


What Is a Banner Ad in AdMob?

A banner ad is a small rectangular advertisement that occupies a part of the app screen. It refreshes automatically and displays new ads without disturbing the user experience.

Most common banner sizes:

  • 320×50 (standard banner)

  • 320×100 (large banner)

  • Adaptive banners (recommended)


⭐ Why Use Google Test Ad IDs?

Google strongly recommends using test ads while developing and testing your Android app.
Using real ads during development can result in:

  • Account suspension

  • Invalid traffic detection

  • Ad serving disabled

  • Policy violation warnings

Therefore, always use Google’s official test banner ID:

Test Banner Ad Unit ID

ca-app-pub-3940256099942544/6300978111

📌 Step 1: Add AdMob Dependency to build.gradle

Open app-level build.gradle and add:

implementation("com.google.android.gms:play-services-ads:23.1.0")

Sync your project.


📌 Step 2: Add AdMob App ID in AndroidManifest.xml

Inside <application> tag, add:

<uses-permission android:name="android.permission.INTERNET" />

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

<meta-data android:name="com.google.android.gms.ads.APPLICATION_ID" android:value="ca-app-pub-3940256099942544~3347511713"/>

(This is Google’s official test App ID)


📌 Step 3: Add Banner View in XML Layout

Create or open your activity layout file (example: activity_main.xml) and add:

<com.google.android.gms.ads.AdView android:id="@+id/adView" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" ads:adSize="BANNER" ads:adUnitId="ca-app-pub-3940256099942544/6300978111" />

This loads the test banner ad.


📌 Step 4: Load Banner Ad in Kotlin Code

Inside your activity (example: MainActivity.kt):

import com.google.android.gms.ads.AdRequest import com.google.android.gms.ads.MobileAds class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // Initialize AdMob MobileAds.initialize(this) val adRequest = AdRequest.Builder().build() adView.loadAd(adRequest) } }

Using Adaptive Banner Ads (Recommended)

Adaptive banners adjust themselves according to screen size.

Kotlin Code

private fun loadAdaptiveBanner() { val adView = AdView(this) adView.adUnitId = "ca-app-pub-3940256099942544/6300978111" val adSize = getAdSize() adView.setAdSize(adSize) bannerContainer.addView(adView) val adRequest = AdRequest.Builder().build() adView.loadAd(adRequest) } private fun getAdSize(): AdSize { val display = windowManager.defaultDisplay val outMetrics = DisplayMetrics() display.getMetrics(outMetrics) val widthPixels = outMetrics.widthPixels val density = outMetrics.density val adWidth = (widthPixels / density).toInt() return AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize(this, adWidth) }

No comments:

Post a Comment