【Kotlin/Android Studio】How to install AdMob! Implementing banner ads

【Kotlin/Android Studio】How to install AdMob! Implementing banner ads

What you will learn from this article

  • How to implement ads in an app developed using Android Studio/Kotlin
  • How to set up and use AdMob
  • The process of placing ads with AdMob
  • How to create the logic for Banner ads
  • Implementing test ads in a developing app

index

[open]

\ アプリをリリースしました /

みんなの誕生日

友達や家族の誕生日をメモ!通知も届く-みんなの誕生日-

posted withアプリーチ

Environment

What is Google AdMob?

AdMob is a service provided by Google that allows you to place advertisements on mobile apps such as iOS and Android.

By providing AdMob with a space on the app screen to place ads, they will automatically deliver ads based on the user's information.

The basic service content and usage are the same as Google AdSense, which allows you to add advertisements to web pages.

Ad Units

AdMob allows you to choose from four types of ad units.

Google AdSenseのホーム画面

Banner

Banner-type advertisements placed at the top or bottom of the app screen. They are always displayed and the content is updated at regular intervals.

Interstitial

Advertisements that are displayed full-screen during screen transitions and flows. They will not disappear unless you press the close button.

Native

Customizable ads that can be integrated into the app design.

Reward

Ads that ask users to watch videos for several tens of seconds. You can implement a process to have users watch ads in exchange for rewards such as points or stamina.

How to Implement Banner Ads in an Android App

This guide summarizes how to integrate ads into an Android app using Google AdMob.

Steps

  1. Register a Google AdMob account
  2. Add your app to AdMob
  3. Add the Mobile Ads SDK in build.gradle (Module)
  4. Add your App ID in the manifest file
  5. Implement AdView (banner view)
  6. Initialize the SDK and load the ad

How to Register a Google AdMob Account

You must first register for a Google AdMob account to get started.

You can easily register using the link above. The registration steps vary depending on whether you already have a Google AdSense account, so follow the appropriate instructions based on your situation.

How to implement ads using AdMob in SwiftUI

If you already have a Google AdSense account, confirm your registration details and click "Create AdMob Account" if everything looks correct.

How to implement ads using AdMob in SwiftUI

After following the instructions, you'll be taken to the AdMob home screen.

If you don’t have a Google AdSense account, go to the home screen's sidebar and register your payment information via “Payments”.

Google AdMob Home Screen

Add Your App to AdMob

From the home screen sidebar, go to “Apps” > “Add App”, choose your platform, and check the appropriate boxes. You can register your app even if it hasn’t been published on the Play Store. Select No for "Is your app listed on a supported app store?" and proceed by clicking “Next”.

Register app in Google AdMob

On the next screen, enter your app’s name and click “Add App” to complete registration.

Register app in Google AdMob

This completes the app registration. After implementing the ad code described below and publishing your app to the store, you can input store information and start the review process. (The screenshots are from iOS, but the steps are the same for Android.)

Register app in Google AdMob

Next, choose the type of ad unit you want to create.

Add an ad unit in Google AdSense

Select your preferred ad unit type, enter a name, and create the unit.

Add an ad unit in Google AdSense

This will display your App ID and Ad Unit ID. You'll need these IDs later, but you can always check them again, so it’s okay to close the dialog.

Add an ad unit in Google AdSense

Add the Mobile Ads SDK via build.gradle (Module)

Add the Mobile Ads SDK to your project. In your build.gradle (Module) file, add the line below and click “Sync Now”.


dependencies {
  // ------------------------------
  implementation("com.google.android.gms:play-services-ads:24.2.0")
}

Add App ID to the Manifest File

Add your AdMob App ID to the manifest file. Open AndroidManifest.kt and add the following meta-data tag inside the application tag. If this is missing, ads won’t display properly.


<?xml version="1.0" encoding="utf-8"?>
<manifest>
// ------------------------------

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

</manifest>

The android:value attribute should be your actual App ID from AdMob. For testing purposes, you can use ca-app-pub-3940256099942544~3347511713.

Implement AdView (Banner View)

In your layout file, implement the AdView. Use adSize="BANNER" and pass the banner ad unit ID to adUnitId. For testing, use ca-app-pub-3940256099942544/6300978111.


<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"
  app:adSize="BANNER"
  app:adUnitId="ca-app-pub-3940256099942544/6300978111"
  app:layout_constraintBottom_toBottomOf="parent"
  app:layout_constraintEnd_toEndOf="parent"
  app:layout_constraintStart_toStartOf="parent"/>

Initialize SDK and Load the Ad

Once the layout is done, initialize the SDK and load the ad in your activity.


override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    // Initialize
    MobileAds.initialize(this)
    // Load Ad
    var adView: AdView = findViewById(R.id.adView)
    adView.loadAd(AdRequest.Builder().build())
}

This completes the implementation of AdMob ads. Run your app and check if the ad is displayed like below.

How to implement banner ads in Kotlin/Android Studio using AdMob

I am still learning, so if you find any mistakes or shortcomings, I would appreciate it if you could let me know.

Thank you for reading.

Search Box

Sponsor

ProFile

ame

Hobbies: reading, learning programming, website creation, blogging

I studied to get an IT pass in order to overcome my dislike of IT, and website creation became my hobby lol. Now I'm running this site completely by myself without using a CMS~

New Article

index