A complete guide to integrating AdMob in your Android App Android 16.05.2018

A complete guide to integrating AdMob in your Android App

So you have developed an android application and now you want to make some money from your application. In this article you will learn how to integrate AdMob in your application.

Google provides tools for advertisers to create ads and define the targeting criteria for their ads. For an example of targeting criteria, an ad might be targeted to be shown to people in a specific location. Google has a huge inventory of ads to display on both websites and mobile apps. You can display ads from this inventory in your app by using AdMob (which stands for Ads on Mobile).

How ads help you make money? Google pays you when users click ads in your app. The exact amount you get paid depends on the ads that get shown, and how much the advertisers were willing to pay for their ads. The total amount paid by the advertisers is distributed between Google and the publisher of the website or app where the ad appears.

There are 3 different types of ads in your application

To display an ad in your app, add an AdView in the layout of an activity and write a small amount of boilerplate code to load the ad. When a user runs your app and goes to that activity, an ad appears in the AdView.

You do not need to worry about finding an ad to display because Google handles that for you.

Don't click on ads in your own app. Google has policies that prevent website publishers and app publishers from clicking ads in their own websites and apps. The advertisers pay when people click their ads, so it would not be fair for you to display an ad in your app, then click the ad, and cause the advertiser to pay you for clicking the ad in your own app.

Create an AdMob account

Before you can experiment with running ads in your app, you need to enable AdMob for that app. To enable AdMob use these steps:

  1. In the Firebase console, select AdMob in the left hand navigation, then select Sign Up For AdMob. You will be re-directed to the AdMob console.
  2. Follow the sign up wizard to create your AdMob account and add your app to AdMob.

To display an ad in your app, you need your AdMob app ID and an ad unit ID. You can get both of these in the AdMob console.

Implement AdMob in your app

To display an ad in your app:

  1. Make sure you have added your app to your Firebase project.
    1. If you're using the latest version of Android Studio (version 2.2 or later), it's recommend using the Firebase Assistant to connect your app to Firebase. The Firebase Assistant can connect your existing project or create a new one for you and automatically install any necessary gradle dependencies.
    2. Click Tools > Firebase to open the Assistant window.
    3. Click Admob to expand, then click Add a banner ad to your app.
    4. Click the Connect to Firebase button to connect to Firebase and add the necessary code to your app.
    5. Click the Add AdMob to your app button and then click Accept Changes.
  2. Add an AdView to the layout for the activity that will display the ad.
  3. Initialize AdMob ads at app launch, by calling MobileAds.initialize() in the onCreate() method of your main activity.
  4. Update the onCreate() of that activity to load the ad into the AdView.

Get ready to run test ads

While you are developing and testing your app, you can display and test ads to make sure your app is setup correctly to display ads. When testing ads you need:

  1. Your device ID (IMEI) for running test ads. To get the device ID either:
    • Go to Settings > About phone > status> IMEI.
    • Dial *#06#.
  2. Your AdMob app ID. Get this in the AdMob console.
  3. An ad unit ID. Get this in the AdMob console.

Add the AdView to display the ad

In the activity's layout file where you want the ad to appear, add an AdView:


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 also need to add the ads namespace to the root view of the layout:


You can also add an AdView dynamically from your code.

AdView adView = new AdView(this);
viewGroup.addChild(adView);    //Or any other way your like

Initialize MobileAds

The MobileAds class provides methods for initializing AdMob ads in your app. Call MobileAds.initialize() in the onCreate() method of your main activity, passing the context and your app ID (which you get from the AdMob console).

// Initialize AdMob
MobileAds.initialize(this, "ca-app-pub-1234");

Write the code to load the ad

In the onCreate() method of the activity that displays the ad, write the code to load the ad.

While you are developing and testing your app you can display ads in test mode by specifying specific test devices. To show ads on your own device, you need to device ID (IMEI), which you can get from Settings > About phone > Status> IMEI or by dialling *#06#.

To load an ad:

  1. Get the AdView where the ad will appear.
  2. Create an AdRequest to request the ad.
  3. Call loadAd() on the AdView to load the ad into the AdView.

Here's the code to write in onCreate() in the activity:

// Get the AdView
AdView adView = (AdView) findViewById(R.id.adView);

// Create an AdRequest
AdRequest adRequest = new AdRequest.Builder()
  // allow emulators to show ads
  // allow your device to show ads
  .addTestDevice("1234") // your device id

// Load the ad into the AdView

Following is the full code

public class MainActivity extends AppCompatActivity {
    private AdView adView;

    protected void onCreate(Bundle savedInstanceState) {

        adView = findViewById(R.id.adView);
        AdRequest adRequest = new AdRequest.Builder().build();


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.

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.

adView.setAdListener(new AdListener() {
    public void onAdLoaded() {
        // Code to be executed when an ad finishes loading.

    public void onAdFailedToLoad(int errorCode) {
        // Code to be executed when an ad request fails.

    public void onAdOpened() {
        // Code to be executed when an ad opens an overlay that
        // covers the screen.

    public void onAdLeftApplication() {
        // Code to be executed when the user has left the app.

    public void onAdClosed() {
        // Code to be executed when when the user is about to return
        // to the app after tapping on an ad.

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.

  1. 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.
  2. Select the type of AdUnit you want create.
  3. 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.