How to Get Started with AdMob in Your Android App

Myrick Chow
Firebase Developers
10 min readJun 7, 2020

--

Google AdMob is a tool for monetising cross-platform apps and websites. It provides a lot of different ad types for displaying different UIs to users. They include banner ads, interstitial ads, native ads and reward ads. In this article, I would focus on the banner ad and share some of my own experiences on AdMob.

In recent years, Google released adaptive banners which area replacement to the popular 320dp x 50dp banner ad and the smart banner. Its height is calculated by SDK at runtime and can optimise the UI for the user. It only requires two steps to upgrade banner ads to adaptive banner.

This article covers the AdMob Console setup and the coding part. It is suitable for both project managers and developers. Hope you will find this article useful and interesting. Let’s start!

AdMob Console —Setup and apply for a banner id

Confirmation email about the applied ad unit with the app id

Visit the AdMob website to create an account for monitoring the ad performance.

Create an app instance at the Apps > Add App path.

An app ID and an ad unit ID are created and should be passed to the developer to implement them in code. To prevent abnormal and malicious ad traffic, it is recommended to keep them as safe as possible. Your AdMob account will be suspended when Google suspects the ad requests are made from a bot instead of real user.

The app ID and ad unit ID are often confused by developers. They can be distinguished by the unique delimiter. App ID is delimited by ~ and ad unit ID is delimited by /.

Bear in mind that Google needs a long time to process your ad unit request. It states “up to an hour” but it fluctuates greatly. One of my previous projects needs around 4 hours! So, please be patient to the ad unit application.
It is also recommended to use the sample ad unit ID during development:
“ca-app-pub-3940256099942544/6300978111"

Message when ad unit application is made

Android project setup

Step 1: Ensure google() is added to the repositories session of allprojects in build.gradle(project)

Step 2: Add the play-services-ads to the app module dependencies in build.gradle(app). Make sure you use the latest version of the library, which can be checked here.

Step 3: Add your app ID, which was set at the previous session at the AdMob Console, at your AndroidManifest.xml.

Different types of Banner ads

There are totally 7 official banner types. Each of them has different dimensions and some of them should be prohibited to be shown in phone:

  1. Banner (320dp x 50dp)

Banner is going to be replaced by Adaptive Banner which has a dynamic height instead of fixed width(320dp) and height (50dp). Please refer to the later session — “Adaptive banner” for more details.

2. Large banner (320dp x 100dp)

3. Full banner (468dp x 60dp)

The width of the full banner ad is 468dp which is larger than the screen width of MOST of the phones in portrait mode. Take Pixel 3 (2018) as an example, the screen width is only 392dp and the full banner is actually cropped (See below for the screenshot). Therefore, this ad should NOT be used in mobile devices unless your app is in landscape mode or targets tablets instead.

4. Smart banner ([Screen width] x [32dp or 50dp or 90dp])

Smart banner is going to be fully replaced by adaptive banner since adaptive banner can calculate the height of banner by developer defined width while smart banner can only use the screen width. Therefore, adaptive banner can provide a better UI to user than smart banner.

5. Adaptive banner ([Developer defined width] x runtime height)

The setup of adaptive banner is not the same as the other banner type. Please refer to the session — “Adaptive banner” for more details.

6. Medium rectangle (300dp x 250dp)

7. Leader Board (728dp x 90dp)

Similar to Full Banner Ad, the width is too large to be shown at phone in both portrait mode and landscape mode. It is highly recommended to check if device dimension is available to display the whole leader board ad.

Programming part — XML attributes

Step 1) Add meta-data at the AndroidManifest.xml

Step 2) Initialise AdMob SDK

Initialisation of AdMob SDK is straightforward and the SDK can reference the app ID declared in AndroidManifest.

If your app is targeted to the US or European Economic Area (EEA) market, you must compile with the Children’s Online Privacy Protection Act (COPPA) or General Data Protection Regulation (GDPR). It is better to show a notice to user about how your app is operating and how you would handle their personal information. This will be discussed in details in the later session — “AdMob handling policy”.

Step 3) Declare at layout XML file

It is straightforward to add an AdView to layout. Both ads:adSize and ads:adUnitId must be declared in the <AdView>, or else the following error layout will be shown at runtime.

The ca-app-pub-3940256099942544/6300978111 is a demo banner ad unit ID.

Note: Adaptive banner cannot be declared at the layout file since the adSize is calculated with a user-defined ad-width at runtime. This will be discussed in the later part of this article.

Step 4) [Optional] Add AdListener

These listener will be discussed in details in the later section “AdListener in details”.

Step 5) Load an Ad request

After running your app, your banner ad can be shown successfully after the onAdOpened() at AdListener is called. Under good network connectivity, the banner ad can be successfully requested within 3 seconds. If there is no ad shown, please refer to the onAdFailedToLoad(errorCode: Int) at “AdListener in details”.

Adaptive banner (2 steps only)

Adaptive banner is a replacement to the 320dp x 50dp banner and also smart banner. It uses the developer defined ad banner width to calculate the best height of ad banners dynamically at runtime.

Adaptive banner solves the issues other banner types can have. 320dp x 50dp banners have a fixed width and height, and could have a large gap between ad and screen edges. Smart banner ads have dynamic ad height but the height must be calculated according to the screen width. The height is limited to one of these options: 32dp, 50dp and 90dp. The UI of smart banner is sometimes not optimised at all. See the above graph for better illustration.

Setup

  1. Calculate an AdSize

AdMob SDK provides an easy to use function getCurrentOrientationAnchoredAdaptiveBannerAdSize(context: Context, adWidth: Int) for dynamically calculating the optimised AdSize.

2. Inflate an AdView at runtime

Unlike other banner ad, adaptive banner ads can only be inflated at runtime since adSize cannot be defined at the XML at compile time.

AdListener in details

1. onAdLoaded()

The ad is requested from Google servers and onAdLoaded() is called when the ad details are successfully requested and are ready to be shown.

2. onAdFailedToLoad(errorCode: Int)

There are 5 error codes in total for requesting an ad:

  1. ERROR_CODE_INTERNAL_ERROR (Constant: 0)

This is not an error at the app side. It is due to an error with the AdMob server. For instance, an invalid response was received from the ad server.

Remember, Google AdMob needs several hours to process your new ad unit application. In my past experience, 4 hours is the shortest record! So, it is recommended to use the debug ad unit ID ca-app-pub-3940256099942544/6300978111 to develop your app during this period of time.

Furthermore, please ensure your payment information is setup properly. You have to bind your AdMob account to a bank in order to receive income from your ads.

2. ERROR_CODE_INVALID_REQUEST (Constant: 1)

The ad unit ID is incorrect. Please go to the AdMob Console to check if the ad unit ID is copied correctly.

3. ERROR_CODE_NETWORK_ERROR (Constant: 2)

There is no network connection at that moment and the ad request fails. The AdMob SDK would automatically retry requesting a new ad from ad server. The developer does not need to handle retries according to the documentation.

4. ERROR_CODE_NO_FILL (Constant: 3)

The settings for requesting ad from ad server are correct and there is a successful response. However, there is no ad at the server ad inventory. The SDK will retry the request similar to how it retries for the ERROR_CODE_NETWORK_ERROR type of errors.

5. ERROR_CODE_APP_ID_MISSING (Constant: 8)

The ad request was not made due to a missing app ID. Please go to the Apps > App settings > App ID to check if your app ID is copied correctly.

3. onAdOpened()

This is called when banner ad is clicked and opens an overlay that covers the screen.

4. onAdLeftApplication()

This is called when a banner ad leaves the application and redirects user to another app, e.g. Google Play and any browser app.

5. onAdClosed()

This is called when user completes viewing the details of ad in another app, e.g. browser, and is going to return to the application.

Overall behavior

The overall ad events can be summarized in the following flowchart:

Flow chart of the ad events

AdMob policy

1. App targeting to the US market — COPPA

Developer must compile with the Children’s Online Privacy Protection Act (COPPA) which indicates whether the app is made for kids. This is a regulation for any apps which are targeting the US market.

Your Google account will be terminated if Google finds any discrepancy between your statement and you app content.

Please refer to the line 3 of the above code for setting the COPPA flag.

Reference: Google AdMob — Child-directed setting

2. Age of consent

General Data Protection Regulation (GDPR) is a regulation applied within the European Economic Area (EEA). If you target your app to the EEA, you must comply with the regulation. This parameter disables personalised advertising, including remarketing, for all ad requests. It also disables requests to third-party ad vendors, such as ad measurement pixels and third-party ad servers.

Please refer to the line 6 of the above code for setting the GDPR flag.

Reference: Google AdMob — Users under the age of consent

3. Ad content filtering

AdMob can help app owners filter ads to target different age-group customers. There are totally 4 different levels available:

  1. MAX_AD_CONTENT_RATING_T
    “T” stands for “Teen”. It filters out all ads with contents which are suitable for all teens.
  2. MAX_AD_CONTENT_RATING_PG
    “PG” stands for “Parental Guidance”. Ads can contain some contents which can be viewed by teens but with sufficient parental guidance.
  3. MAX_AD_CONTENT_RATING_MA
    “MA” stands for “Mature Audience”. Ads can contain some information which is only suitable for adult audience.
  4. MAX_AD_CONTENT_RATING_G
    “G” stands for “General” which is self-explanatory.

Please refer to the line 9 of the above code for setting the ad content filter.

Reference: Google AdMob — Ad content filtering

Analytics

Google AdMob provides a clean dashboard for user to view some analytic reports about how active your ad campaign. It shows the trend of estimated earnings and the total number of ad requests.

User can view these graph by the following steps:

Summary

  1. AdMob is a great tools to monetise your app.
  2. AdMob provides 7 types of banner ads. They are banners, large banners, full banners, leader boards, medium rectangle banners, smart banners and adaptive banners.
  3. Full banner and leader board banner can not be shown on a normal phone in portrait mode. It should only be used in landscape mode or on tablets. Please consider wisely before adopting the banner.
  4. Adaptive banner is a replacement to the 320dp x 50dp banner ad and the smart banner.
  5. AdMob provides debug ad unit IDs for banners. Debugging your app with the debug ID can prevent your app from being suspended by Google due to abnormal ad request traffic.
  6. It is highly recommended that you implement AdListener methods in order to debug the app flow more easily.
  7. Children’s Online Privacy Protection Act (COPPA) and General Data Protection Regulation (GDPR) must be complied carefully in order to target your app to US and EEA markets. Violating these rules can lead to a termination to your Google account.
  8. AdMob Analytics can provide lots of information to app owner about the trend and reports of the ad performances.

Reference and further readings

  1. AdRequest | Google APIs for Android | Google Developers
  2. Google Play Console set up
  3. Stack Overflow: Android — Admob ads not loading — Failed to load ad: 0

4. Google AdMob targeting official documentation

You are welcome to follow me at Twitter@myrick_chow for more information and articles. Thank you for reading this article. Have a nice day! 😄

--

--

Myrick Chow
Firebase Developers

Mobile Lead @REAL Messenger Inc. https://real.co Focus on Android & iOS Native programming.