How to Adding Google AdMob Ads to an Android application ?




 


AdMob is Google’s advertising platform specifically designed for mobile applications. AdMob allows you to monetize your apps by serving ads selected according to criteria you define. So if you’ve developed an Android app and now you want to make money with your app, in this article I show you how to do it, how to add different ad formats to your apps.


To get started, make sure you have integrated Firebase into your project. If you haven’t already done so, here’s how to do it.


Set up your application in your AdMob account :

  1. To get started you need to create an AdMob account or sign in if you already have one.
  2. Login to AdMob and open your dashboard. Now, go to Applications> Add an Application> Fill out the information required by AdMob> Click Add, which will generate an AdMob ID for your application.
  3. To link your newly added application to Firebase. Select the application, access the application settings and under Application Information you will see an option to link the application to Firebase.
  4. Add your AdMob Application ID to your AndroidManifest.xml file by adding the <meta-data>tag. 

<meta-data
    android:name="com.google.android.gms.ads.APPLICATION_ID"
    android:value="ADMOB_APP_ID"
    />
5. Add and initialize the Mobile Ads SDK. Add the dependency of the Google Mobile Ads SDK to            the build.gradle module file

implementation 'com.google.android.gms:play-services-ads:19.1.0'


You must initialize the Mobile Ads SDK, before you can broadcast any type of announcement made in one of your activities at the start of your application. Call this method only once and as soon as possible, ideally at the launch of the application.

class MainActivity : AppCompatActivity() {

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

        MobileAds.initialize(this, "YOUR_ADMOB_APP_ID")
    }
}


Choose an ad format to implement in your application



AdMob offers different ad formats (Banner, Interstitial and Rewarded). You can choose the format that best suits your application’s user experience.

1. Creation of advertising banners

Banner ads take up space in the layout of an application, at the top or bottom of the device screen. They remain on the screen while users interact with the application and may refresh automatically after a period of time. To add a banner ad to one of your activities / fragments, you will need to use AdView . In your layout file, add the following code:


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    tools:context=".MainActivity"
    android:orientation="vertical">
    <com.google.android.gms.ads.AdView
        android:id="@+id/adView"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        ads:adSize = "BANNER"
        ads:adUnitId = "ca-app-pub-3940256099942544~3347511713">

    </com.google.android.gms.ads.AdView>
</LinearLayout>

There are two settings to note here adSize which defines the size of your banner, there are different sizes of banners available that need to be viewed and adUnitId is the unique ID that identifies your unique ad block.


Loading an ad

The ad loading is done with the method loadAd() of AdViewclass. it take as parameter a object ot typeAdRequest, which contains execution information (such as targeting information) on a single ad request.

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() {

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

        MobileAds.initialize(this) {}

        val mAdView = findViewById<AdView> (R.id.adView)
        val adRequest = AdRequest.Builder (). build ()
        mAdView.loadAd (adRequest)
    }
}
Admob


2. Creation of interstitial ad

Interstitial announcements are full screen announcements that cover the interface of their host application. When creating interstitial advertisements, we do not need to define a view in your layout file, they can only be created by programming.

Interstitial announcements are requested and displayed by InterstitialAd objects. Start by instantiating InterstitialAd to set its announcement block ID. This is done in the onCreate() method of an activity.


class MainActivity : AppCompatActivity() {

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

        MobileAds.initialize(this) {}

        val mInterstitialAd = InterstitialAd(this)
        mInterstitialAd.adUnitId = "ca-app-pub-3940256099942544/1033173712"
    }
}

Loading ad

To load an interstitial announcement, call the method loadAd() ofInterstitialA object, this method accept a object to typeAdRequest as unique parameter

mInterstitialAd.loadAd(AdRequest.Builder().build())

Showing the Ad

To display an interstitial, use the method isLoaded() to check that the loading is complete, then call show().


clickButton.setOnClickListener {
            if (mInterstitialAd.isLoaded) {
                mInterstitialAd.show()
            } else {
                Log.d("TAG", "The ads wasn't loaded yet.")
            }
        }



For testing purposes, you can use the sample AdMob Application ID provided by Google:

App Id = ca-app-pub-3940256099942544~3347511713
Ads Id  =ca-app-pub-3940256099942544/1033173712


Featured Post

How to fix CLS issue in your blog or web ?

  Fixing CLS issues is a big deal. That's because most of your competitors won't fix CLS problems and as a result won't rank wel...