Adding Google AdMob to an Android application

Deborah Yambenu
ProAndroidDev
Published in
4 min readMay 22, 2020

--

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.

5. Add and initialize the Mobile Ads SDK. Add the dependency of the Google Mobile Ads SDK to the build.gradle module file

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.

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.

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:

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.

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.

  • Loading ad

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

  • Showing the Ad

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

Create Rewarded Video Ads

Rewarded Video Ads are full screen video ads that users have the ability to watch in full in exchange for rewards built into the application.

Get theRewardedVideoA. object by using this method MobileAds.getRewardedVideoAdInstance().

To be notified of award-winning video lifecycle events, you must implement the RewardedVideoAdListenerclass, The award-winning announcement earpiece is defined using the method setRewardedVideoAdListener().

  • Request an award-winning video ad
  • Post an award-winning video ad

The isLoaded() method indicates whether the award-winning video ad request has been successfully processed.

Conclusion

Don’t hesitate to ask questions if you can’t understand a concept explained in the article, you can find the complete source code by clicking here.

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

ca-app-pub-3940256099942544~3347511713
ca-app-pub-3940256099942544/1033173712

Reference

--

--