Google provides AdMob to monetize Android Application through in-app advertising. This tutorial helps in a easy way to display advertise in your Android App.

Introduction

Google provides a platform to display ads in your App. You can generate revenue from your App. Ads can be displayed as a Banner, interstitial and video Ads. To display ads within Android Application, we need to create an AdMob account and activate one or more Ad Unit Ids.
 

Create AdMob Account

To create Ads unit, we need to create AdMob account and Sign into your AdMob account.
 

Click on Monetize Tab

After successfully sign into AdMob Account click on the Monetize Tab to select or create app. To create an new App, click on “ADD YOUR APP MANUALLY” TAB. Fill App name and select Android Platform then new App Id generated look like this:
admob1-min
 

Select Ad Format And Name Ad Unit

If we want to create a Banner Ads then click on Banner Tab otherwise want to create full size Ads then click on Interstitial Tab. We need to fill name of the Ads Unit in the Ad unit name input.
 
Banner Ads

admob2-min

Interstitial Ads

admob3-min

Set Up Firebase

This is optional, so we can skip to setup Firebase to click SKIP button and click on DONE button. Then Finally we can get App Id and Unit Id. It will take about an hour to activate Ads.
admob5-min

We created an Ads Unit successfully
admob6-min
 

Create a new project

To implement Ads in Android App, we are going to create a new android project. Go to File ⇒ New ⇒ New Projects in Android studio.
 

Add Google Adsense library

We need to add Google Adsense library in our project, so open build.gradle(Module:app) file and add following code:


dependencies {
   .....
    compile 'com.google.android.gms:play-services-ads:8.4.0'
   .....
}

 

Add Ads Unit Id

We will add Ads Unit Id in the string values.

<string name="banner_ad_unit_id">ca-app-pub-4514287475704546/4074XXXX</string>
<string name="fullscreen_ad_unit_id">ca-app-pub-4514287475704546/581869XXXX</string>

Create Layout

We will display Banner Ads in the MainActivity and open Interstitial Ads on click a button hence, we need to add Button and Ads view com.google.android.gms.ads.AdView in Main Activity. In the AdView tag, set the adSize to BANNER and the AdUnitId to @string/banner_ad_unit_id.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="in.studytitorial.androidgoogleadmob.MainActivity">

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Show Full Screen Ad"
        android:layout_centerInParent="true"/>

    <com.google.android.gms.ads.AdView
        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="@string/banner_ad_unit_id">
    </com.google.android.gms.ads.AdView>
</RelativeLayout>




Banner Ads

Banners Ads are the Ad unit that render on screen with fixed size. We added Banner Ad view com.google.android.gms.ads.AdView above in the activity.xml file.
 

Initialize Google Ads SDK

To display Banner Ads, we need to initialize Google Ads SDK in onCreate() method of MainActivity.java. Call MobileAds.initialize() to initialize Google Ads SDK. Pass context and Ad unit Id in the MobileAds.initialize().

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        MobileAds.initialize(getApplicationContext(),
                   getString(R.string.banner_ad_unit_id));
   }
}

 

Load Ad

We will load Ad in the AdView, so we need to initialize Adview Id. Create an AdRequest.build object and then load an ad into the AdView.

public class MainActivity extends AppCompatActivity {
    AdView mAdView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        MobileAds.initialize(getApplicationContext(),
                   getString(R.string.banner_ad_unit_id));
        mAdView = (AdView) findViewById(R.id.adView);
        AdRequest adRequest = new AdRequest.Builder().build();
        mAdView.loadAd(adRequest);
   }
}

 

Load Ad for Test

If we want to load an Ad as test then we need to call addTestDevice() method.

public class MainActivity extends AppCompatActivity {
    AdView mAdView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        MobileAds.initialize(getApplicationContext(),
                   getString(R.string.banner_ad_unit_id));
        mAdView = (AdView) findViewById(R.id.adView);
        AdRequest adRequest = new AdRequest.Builder()
                .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
                // Check the LogCat to get your test device ID
                .addTestDevice("Your App Device")
                .build();
        mAdView.loadAd(adRequest);
     }
}

 

Run Your App

Now, run your Android App then you will able to see Google Ads in your App.
Ads in Android App
 

Stop & Pause Ads

Do not forgot to pause and destroy Ads while App pause and stop.

public class MainActivity extends AppCompatActivity {
    AdView mAdView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        MobileAds.initialize(getApplicationContext(),
                   getString(R.string.banner_ad_unit_id));
        mAdView = (AdView) findViewById(R.id.adView);
        AdRequest adRequest = new AdRequest.Builder().build();
        mAdView.loadAd(adRequest);
     }

     
    @Override
    public void onPause() {
        if (mAdView != null) {
            mAdView.pause();
        }
        super.onPause();
    }

    @Override
    public void onResume() {
        super.onResume();
        if (mAdView != null) {
            mAdView.resume();
        }
    }

    @Override
    public void onDestroy() {
        if (mAdView != null) {
            mAdView.destroy();
        }
        super.onDestroy();
    }
}

 

Create a new Activity

To display full Screen Ads(Interstitial Ads), we need to create a new Activity named as FullScreenAd. Initialize button in MainActivity and Open FullScreenAd Activity on click button.

public class MainActivity extends AppCompatActivity {
    AdView mAdView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        MobileAds.initialize(getApplicationContext(),
                   getString(R.string.banner_ad_unit_id));
        mAdView = (AdView) findViewById(R.id.adView);
        AdRequest adRequest = new AdRequest.Builder().build();
        mAdView.loadAd(adRequest);
     }

     
    @Override
    public void onPause() {
        if (mAdView != null) {
            mAdView.pause();
        }
        super.onPause();
    }

    @Override
    public void onResume() {
        super.onResume();
        if (mAdView != null) {
            mAdView.resume();
        }
    }

    @Override
    public void onDestroy() {
        if (mAdView != null) {
            mAdView.destroy();
        }
        super.onDestroy();
    }
}

 

Interstitial Ads

Interstitial Ad is an Ad to render full screen in Android device.
 

Load Interstitial Sdk

To display full screen Ad, we need to initialize InterstitialAd() and set Ad unit Id.


public class FullScreenAd extends AppCompatActivity {

    private String TAG = FullScreenAd.class.getSimpleName();
    InterstitialAd mInterstitialAd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_full_screen_ad);

        mInterstitialAd = new InterstitialAd(this);

        // set the ad unit ID
        mInterstitialAd.setAdUnitId(getString(R.string.fullscreen_ad_unit_id));
        }
}

 

Load Ad

We need to create AdRequest.build() object and load it into Interstitial Ad.

public class FullScreenAd extends AppCompatActivity {

    private String TAG = FullScreenAd.class.getSimpleName();
    InterstitialAd mInterstitialAd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_full_screen_ad);

        mInterstitialAd = new InterstitialAd(this);

        // set the ad unit ID
        mInterstitialAd.setAdUnitId(getString(R.string.fullscreen_ad_unit_id));
        AdRequest adRequest = new AdRequest.Builder()
        // Load ads into Interstitial Ads
        mInterstitialAd.loadAd(adRequest);
        }
}

 

Set Ad Listener

We need to ad set Ad listener by using setAdListener() and call showInterstitial() method with in it to show Ad.

public class FullScreenAd extends AppCompatActivity {

    private String TAG = FullScreenAd.class.getSimpleName();
    InterstitialAd mInterstitialAd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_full_screen_ad);

        mInterstitialAd = new InterstitialAd(this);

        // set the ad unit ID
        mInterstitialAd.setAdUnitId(getString(R.string.fullscreen_ad_unit_id));
        AdRequest adRequest = new AdRequest.Builder()
        // Load ads into Interstitial Ads
        mInterstitialAd.loadAd(adRequest);
        mInterstitialAd.setAdListener(new AdListener() {
            public void onAdLoaded() {
                showInterstitial();
            }
       });
    }

    private void showInterstitial() {
        if (mInterstitialAd.isLoaded()) {
            mInterstitialAd.show();
        }
    }
}

 



Interstitial Ads