So you have developed an android application and now you want to make some money from your application. You have come to the right place. In this article I will teach you how to integrate AdMob in your application. By the end of this article you will know how to integrate 3 different types of ads in your application, Banners ads, Interstitial ads and Rewarded Video ads. We will integrate AdMob using Firebase.

Setting Up AdMob

Make sure you have Firebase set up in your application. Here are the steps to add Firebase in your project.

Next you need to sign up for an AdMob account. If you don’t have an AdSense account, you will need to create it before creating an AdMob account. Here are all the possible cases to create AdSense and AdMob account.

Remember to signup for AdMob with the same account that you will be using with Firebase.

Once you have created an account, login into AdMob and open your dashboard. Now go to Apps > Add App > Fill in the information required by AdMob > Click on Add.

Once you done setting up Firebase and AdMob and creating your application, you will need to link your application in AdMob to Firebase.

Select the newly added app, go to App Settings and there under App Info you will see an option to link application with Firebase.

admob

Click on ‘LINK TO FIREBASE’, a pop up will appear asking you the package name of your application, enter the correct package name of you application. If you face any problem linking application to Firebase, here is the official support page from Google.

In you application’s build.gradle add the Mobile Ads SDK dependency:

You need to initialise the Mobile Ads SDK, do it in one of your activities when your application starts. This should be done before showing any kind of ad.

Right now for testing purposes you can use the sample AdMob app ID provided by Google:

So the initialisation code will be:

Now that we are done with the set up lets create some ads!

Creating Banner Ads

To add a Banner Ad to any of your activity/fragment you will have to use AdView . In your layout file add the following code:

There are two different parameters that you should notice here, adUnitId  and adSize .

adUnitId is the unique ID that identifies your single ad unit. Here the ID that we have used is a test ID provided by Google. Always use test ID when your are developing/testing an app. A real ID should only be used in production. In the end of this tutorial I will show you how to create original ID’s for your ad units.

adSize defines the size of your banner, there are various banner sizes available which must have a look at here.

You can also add an AdView  dynamically from your code.

Loading Ad

After adding the AdView, we will now load an ad into it.

Get a reference to the adView, create a AdRequest  object, load the ad by calling loadAd on the AdView reference and passing adRequest into it.

Handling ad events

Ad is loaded using internet so it is very likely that the user might not have proper internet connection or any type of error occurs. Also you might want to know when the ad is completely loaded. AdMob provides a set of callbacks for all type of ads.

You can use an AdListener to listen to various events.

Note: Always use test Ad ID while you are testing your application, you should never use original Ad ID in your application when in testing/developing phase. If you do so you might get blocked from AdMob.

Once the ad is loaded, you will see something like this:

admob

Creating Interstitial Ads

When creating interstitial ads, we don’t need to define any view in your layout file, these can only be created programatically.

Make a global variable of InterstitialAd , initialise the the variable in onCreate and set the AdUnitId.

Google provides a testing id for interstitial ad, make sure to use exactly this ID when testing/debugging your application.

Now we need to load the ad.

Lets show the ad. Always check if the ad is loaded before showing it.

You can set various callbacks to listen to various events regarding the ad.

Watch this video to get to know the best practices when implementing interstitial ads. This is how the ad will look once loaded.

admob

Rewarded Video Ads

Now we will see how to implement video ads which can be used to give rewards to the user.

Just as in interstitial ads, we don’t need to add anything to our layout, these ads can only be implemented programatically.

Create a global variable of RewardedVideoAd, initialise the variable in onCreate and set the AdUnitId.

We create a new instance of RewardedVideoAd differently compared to other ad types by using  MobileAds.getRewardedVideoAdInstance(this);

To load RewardedVideoAd you need to call the loadAd(adUnitId, AdRequest) on the ad object.

Google provides a testing id for video ad, make sure to use exactly this ID when testing/debugging your application.

As with other types of ads you can listen to various ad events by setting a RewardedVideoAdListener  using mRewardedVideoAd.setRewardedVideoAdListener(RewardedVideoAdListener); .

 

Always check if the ad is loaded before showing the ad.

With RewardedVideoAd ‘s you have the take care of the lifecycle events as shown below.

Once loaded the ad will look something like this.

admob

Getting real ad unit ID’s

When your application is in production, it must use real ad unit ID’s created in the admob dashboard. Don’t leave test ad unit ID’s in your production application, else you won’t earn any money :P.

Go to AdMob dashboard > Select your application user Apps > Click on Ad Units > Click Get Started. Now it ask you what kind of AdUnit do you want to create.

admob

Select the type of AdUnit you want create. Here I have created a banner ad unit.

admob

Once the ad unit is created successfully it will show you the AdUnitId which you will use instead of the test ID that you used previously.

Some helpful links

Banner Ads Developer Page

Interstital Ads Developer Page

Rewarded Video Ads Developer Page

Discouraged Banner Ad Implementation

Recommended Banner Ad Implementation

Disallowed Interstitial Ad Implementations

Recommended interstitial implementations

How to use Room in Android

How to make Bottom Sheet in Android


Gurleen Sethi

How can I tell you about myself in just a few lines, to get to know me you can reach out to me, email, text, call, or any other method of communication you like. I am always there for you!

2 Comments

Alex · October 15, 2018 at 9:06 pm

Please can you help me integrate my banner in an app I am having difficulties

Guide to Notifications in Android - TheTechnoCafe · May 28, 2018 at 9:05 pm

[…] A complete guide to integrating AdMob in you Android App. […]

Leave a Reply

Your email address will not be published. Required fields are marked *

15 − 12 =