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.

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:

compile 'com.google.firebase:firebase-ads:12.0.0' 1 compile 'com.google.firebase:firebase-ads:12.0.0'

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.

public class MainActivity extends AppCompatActivity { protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Sample AdMob app ID: ca-app-pub-3940256099942544~3347511713 MobileAds.initialize(this, "YOUR_ADMOB_APP_ID"); } } 1 2 3 4 5 6 7 8 public class MainActivity extends AppCompatActivity { protected void onCreate ( Bundle savedInstanceState ) { super . onCreate ( savedInstanceState ) ; setContentView ( R . layout . activity_main ) ; // Sample AdMob app ID: ca-app-pub-3940256099942544~3347511713 MobileAds . initialize ( this , "YOUR_ADMOB_APP_ID" ) ; } }

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

ca-app-pub-3940256099942544~3347511713 1 ca - app - pub - 3940256099942544 ~ 3347511713

So the initialisation code will be:

MobileAds.initialize(this, "ca-app-pub-3940256099942544~3347511713"); 1 MobileAds . initialize ( this , "ca-app-pub-3940256099942544~3347511713" ) ;

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:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_height="match_parent" android:layout_width="match_parent"> <com.google.android.gms.ads.AdView xmlns:ads="http://schemas.android.com/apk/res-auto" android:id="@+id/adView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_alignParentBottom="true" ads:adSize="BANNER" ads:adUnitId="ca-app-pub-3940256099942544/6300978111"> </com.google.android.gms.ads.AdView> </RelativeLayout> 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 < RelativeLayout xmlns : android = "http://schemas.android.com/apk/res/android" xmlns : tools = "http://schemas.android.com/tools" android : layout_height = "match_parent" android : layout_width = "match_parent" > < com . google . android . gms . ads . AdView xmlns : ads = "http://schemas.android.com/apk/res-auto" android : id = "@+id/adView" android : layout_width = "wrap_content" android : layout_height = "wrap_content" android : layout_centerHorizontal = "true" android : layout_alignParentBottom = "true" ads : adSize = "BANNER" ads : adUnitId = "ca-app-pub-3940256099942544/6300978111" > < / com . google . android . gms . ads . AdView > < / RelativeLayout >

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.

Banner test ID from Google ca-app-pub-3940256099942544/6300978111 1 ca - app - pub - 3940256099942544 / 6300978111

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.

AdView adView = new AdView(this); adView.setAdSize(AdSize.BANNER); adView.setAdUnitId("ca-app-pub-3940256099942544/6300978111"); viewGroup.addChild(adView); //Or any other way your like 1 2 3 4 AdView adView = new AdView ( this ) ; adView . setAdSize ( AdSize . BANNER ) ; adView . setAdUnitId ( "ca-app-pub-3940256099942544/6300978111" ) ; viewGroup . addChild ( adView ) ; //Or any other way your like

Loading Ad

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

public class MainActivity extends AppCompatActivity { private AdView mAdView; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); MobileAds.initialize(this,"ca-app-pub-3940256099942544~3347511713"); mAdView = findViewById(R.id.adView); AdRequest adRequest = new AdRequest.Builder().build(); mAdView.loadAd(adRequest); } } 1 2 3 4 5 6 7 8 9 10 11 12 13 public class MainActivity extends AppCompatActivity { private AdView mAdView ; protected void onCreate ( Bundle savedInstanceState ) { super . onCreate ( savedInstanceState ) ; setContentView ( R . layout . activity_main ) ; MobileAds . initialize ( this , "ca-app-pub-3940256099942544~3347511713" ) ; mAdView = findViewById ( R . id . adView ) ; AdRequest adRequest = new AdRequest . Builder ( ) . build ( ) ; mAdView . loadAd ( adRequest ) ; } }

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.

mAdView.setAdListener(new AdListener() { @Override public void onAdLoaded() { // Code to be executed when an ad finishes loading. } @Override public void onAdFailedToLoad(int errorCode) { // Code to be executed when an ad request fails. } @Override public void onAdOpened() { // Code to be executed when an ad opens an overlay that // covers the screen. } @Override public void onAdLeftApplication() { // Code to be executed when the user has left the app. } @Override public void onAdClosed() { // Code to be executed when when the user is about to return // to the app after tapping on an ad. } }); 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 mAdView . setAdListener ( new AdListener ( ) { @ Override public void onAdLoaded ( ) { // Code to be executed when an ad finishes loading. } @ Override public void onAdFailedToLoad ( int errorCode ) { // Code to be executed when an ad request fails. } @ Override public void onAdOpened ( ) { // Code to be executed when an ad opens an overlay that // covers the screen. } @ Override public void onAdLeftApplication ( ) { // Code to be executed when the user has left the app. } @ Override public void onAdClosed ( ) { // Code to be executed when when the user is about to return // to the app after tapping on an ad. } } ) ;

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:

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.

public class MainActivity extends Activity { private InterstitialAd mInterstitialAd; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); MobileAds.initialize(this,"ca-app-pub-3940256099942544~3347511713"); mInterstitialAd = new InterstitialAd(this); mInterstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712"); } } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 public class MainActivity extends Activity { private InterstitialAd mInterstitialAd ; @ Override protected void onCreate ( Bundle savedInstanceState ) { super . onCreate ( savedInstanceState ) ; setContentView ( R . layout . activity_main ) ; MobileAds . initialize ( this , "ca-app-pub-3940256099942544~3347511713" ) ; mInterstitialAd = new InterstitialAd ( this ) ; mInterstitialAd . setAdUnitId ( "ca-app-pub-3940256099942544/1033173712" ) ; } }

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

ca-app-pub-3940256099942544/1033173712 1 ca - app - pub - 3940256099942544 / 1033173712

Now we need to load the ad.

mInterstitialAd.loadAd(new AdRequest.Builder().build()); 1 mInterstitialAd . loadAd ( new AdRequest . Builder ( ) . build ( ) ) ;

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

button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (mInterstitialAd.isLoaded()) { mInterstitialAd.show(); } } }); 1 2 3 4 5 6 7 8 button . setOnClickListener ( new View . OnClickListener ( ) { @ Override public void onClick ( View v ) { if ( mInterstitialAd . isLoaded ( ) ) { mInterstitialAd . show ( ) ; } } } ) ;

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

mInterstitialAd.setAdListener(new AdListener() { @Override public void onAdLoaded() { // Code to be executed when an ad finishes loading. } @Override public void onAdFailedToLoad(int errorCode) { // Code to be executed when an ad request fails. } @Override public void onAdOpened() { // Code to be executed when the ad is displayed. } @Override public void onAdLeftApplication() { // Code to be executed when the user has left the app. } @Override public void onAdClosed() { // Code to be executed when when the interstitial ad is closed. } }); 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 mInterstitialAd . setAdListener ( new AdListener ( ) { @ Override public void onAdLoaded ( ) { // Code to be executed when an ad finishes loading. } @ Override public void onAdFailedToLoad ( int errorCode ) { // Code to be executed when an ad request fails. } @ Override public void onAdOpened ( ) { // Code to be executed when the ad is displayed. } @ Override public void onAdLeftApplication ( ) { // Code to be executed when the user has left the app. } @ Override public void onAdClosed ( ) { // Code to be executed when when the interstitial ad is closed. } } ) ;

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

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.

public class MainActivity extends AppCompatActivity{ private RewardedVideoAd mRewardedVideoAd; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Use an activity context to get the rewarded video instance. mRewardedVideoAd = MobileAds.getRewardedVideoAdInstance(this); } } 1 2 3 4 5 6 7 8 9 10 11 12 public class MainActivity extends AppCompatActivity { private RewardedVideoAd mRewardedVideoAd ; @ Override protected void onCreate ( Bundle savedInstanceState ) { super . onCreate ( savedInstanceState ) ; setContentView ( R . layout . activity_main ) ; // Use an activity context to get the rewarded video instance. mRewardedVideoAd = MobileAds . getRewardedVideoAdInstance ( this ) ; } }

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.

private void loadRewardedVideoAd() { mRewardedVideoAd.loadAd("ca-app-pub-3940256099942544/5224354917", new AdRequest.Builder().build()); } 1 2 3 4 private void loadRewardedVideoAd ( ) { mRewardedVideoAd . loadAd ( "ca-app-pub-3940256099942544/5224354917" , new AdRequest . Builder ( ) . build ( ) ) ; }

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

ca-app-pub-3940256099942544/5224354917 1 ca - app - pub - 3940256099942544 / 5224354917

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

@Override public void onRewarded(RewardItem reward) { Toast.makeText(this, "onRewarded! currency: " + reward.getType() + " amount: " + reward.getAmount(), Toast.LENGTH_SHORT).show(); // Reward the user. } @Override public void onRewardedVideoAdLeftApplication() { Toast.makeText(this, "onRewardedVideoAdLeftApplication", Toast.LENGTH_SHORT).show(); } @Override public void onRewardedVideoAdClosed() { Toast.makeText(this, "onRewardedVideoAdClosed", Toast.LENGTH_SHORT).show(); } @Override public void onRewardedVideoAdFailedToLoad(int errorCode) { Toast.makeText(this, "onRewardedVideoAdFailedToLoad", Toast.LENGTH_SHORT).show(); } @Override public void onRewardedVideoAdLoaded() { Toast.makeText(this, "onRewardedVideoAdLoaded", Toast.LENGTH_SHORT).show(); } @Override public void onRewardedVideoAdOpened() { Toast.makeText(this, "onRewardedVideoAdOpened", Toast.LENGTH_SHORT).show(); } @Override public void onRewardedVideoStarted() { Toast.makeText(this, "onRewardedVideoStarted", Toast.LENGTH_SHORT).show(); } @Override public void onRewardedVideoCompleted() { Toast.makeText(this, "onRewardedVideoCompleted", Toast.LENGTH_SHORT).show(); } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 @ Override public void onRewarded ( RewardItem reward ) { Toast . makeText ( this , "onRewarded! currency: " + reward . getType ( ) + " amount: " + reward . getAmount ( ) , Toast . LENGTH_SHORT ) . show ( ) ; // Reward the user. } @ Override public void onRewardedVideoAdLeftApplication ( ) { Toast . makeText ( this , "onRewardedVideoAdLeftApplication" , Toast . LENGTH_SHORT ) . show ( ) ; } @ Override public void onRewardedVideoAdClosed ( ) { Toast . makeText ( this , "onRewardedVideoAdClosed" , Toast . LENGTH_SHORT ) . show ( ) ; } @ Override public void onRewardedVideoAdFailedToLoad ( int errorCode ) { Toast . makeText ( this , "onRewardedVideoAdFailedToLoad" , Toast . LENGTH_SHORT ) . show ( ) ; } @ Override public void onRewardedVideoAdLoaded ( ) { Toast . makeText ( this , "onRewardedVideoAdLoaded" , Toast . LENGTH_SHORT ) . show ( ) ; } @ Override public void onRewardedVideoAdOpened ( ) { Toast . makeText ( this , "onRewardedVideoAdOpened" , Toast . LENGTH_SHORT ) . show ( ) ; } @ Override public void onRewardedVideoStarted ( ) { Toast . makeText ( this , "onRewardedVideoStarted" , Toast . LENGTH_SHORT ) . show ( ) ; } @ Override public void onRewardedVideoCompleted ( ) { Toast . makeText ( this , "onRewardedVideoCompleted" , Toast . LENGTH_SHORT ) . show ( ) ; }

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

if (mRewardedVideoAd.isLoaded()) { mRewardedVideoAd.show(); } 1 2 3 if ( mRewardedVideoAd . isLoaded ( ) ) { mRewardedVideoAd . show ( ) ; }

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

@Override public void onResume() { mRewardedVideoAd.resume(this); super.onResume(); } @Override public void onPause() { mRewardedVideoAd.pause(this); super.onPause(); } @Override public void onDestroy() { mRewardedVideoAd.destroy(this); super.onDestroy(); } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 @ Override public void onResume ( ) { mRewardedVideoAd . resume ( this ) ; super . onResume ( ) ; } @ Override public void onPause ( ) { mRewardedVideoAd . pause ( this ) ; super . onPause ( ) ; } @ Override public void onDestroy ( ) { mRewardedVideoAd . destroy ( this ) ; super . onDestroy ( ) ; }

Once loaded the ad will look something like this.

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.

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

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