firebase_admob_feature_image

Firebase Admob Integration in Android 2


This tutorial describes the usage of the Firebase AdMob service in Android. If you don’t know what is Firebase and how to setup in android studio, then please through this previous blog.

Using AdMob with Firebase Analytics provides you with additional app usage data and analytics capabilities. Firebase integrates with AdMob without requiring changes to your existing AdMob configuration.

 

So lets starts with AdMob service:

To write your Firebase AdMob service, use the MobileAds API. The instructions in this page assume that you have completed the steps for adding Firebase to your Android project.

Give your app an ad unit ID

An ad unit ID is a unique identifier given to the places in your app where ads are displayed. Create an ad unit for each activity your app will perform. If you have an app with two activities, for example, each displaying a banner, you need two ad units, each with its own ID. AdMob ad unit IDs have the following form:

Note: If you don’t know on How to create Ad Unit ID, please go through this page.

 

Set up Firebase and the AdMob SDK

In Android Studio, add the Authentication dependency to your app-level build.gradle file:

Permissions

We need request for internet permission.

Initialize the Google Mobile Ads SDK

At app launch, initialize the Google Mobile Ads SDK by calling MobileAds.initialize in the onCreate method of Activity.

Displaying a Banner Ads:

Place an AdView in layout

Add these to the XML:

  • An additional namespace used for ads:
  • A new element for your AdView 

The following ad formats are supported:

Size (WxH) Description Availability AdSize Constant
320×50 Standard Banner Phones and Tablets BANNER
320×100 Large Banner Phones and Tablets LARGE_BANNER
300×250 IAB Medium Rectangle Phones and Tablets MEDIUM_RECTANGLE
468×60 IAB Full-Size Banner Tablets FULL_BANNER
728×90 IAB Leaderboard Tablets LEADERBOARD
Screen width x 32|50|90 Smart Banner Phones and Tablets SMART_BANNER

Load the ad in the AdView

Ad lifecycle event callbacks

You may optionally track ad lifecycle events like request failures or “click-through” by passing an object to AdView.setAdListener that extends com.google.android.gms.ads.AdListener

Forward lifecycle events

Displaying an Interstitial Ads:

Interstitial ads are full screen ads that cover the interface of their host app. They’re typically displayed at natural transition points in the flow of an app, such as between activities or during the pause between levels in a game. When an app shows an interstitial, the user has the choice to either tap on the ad and continue to its destination or close it and return to the app. The direct call to action and larger size combine to make interstitials a particularly effective form of mobile advertisement.

Add interstitial ads to an activity

Display the interstitial

Ad lifecycle event callbacks

You may optionally track ad lifecycle events like request failures or “click-through” by passing an object to AdView.setAdListener that extends com.google.android.gms.ads.AdListener

Forward lifecycle events

Screen Shots:

firebase_admob_banner_ad.pngfirebase_admob_interstitial_ad.png

Sample Code:

Complete Source code available here.

Conclusion

I hope you will like the article and it will definitely help you to make your apps more productive. In case of any confusion or query, feel free to ask. And if you found this tutorial to be useful, Share with others.


About Durga Chiranjeevi

I’m a normal guy, engineer by education who is passionate about Programming and Internet. An android enthusiast, now moving for cross platforms and game development.