Integrate Google AdMob In Android Application Tutorial

Udemy Generic 728x90


In this tutorial, we will learn how to integrate Google AdMob for Android in your Android Application through a simple example.


Google AdMob is a platform through which you can monetize your app and earn revenues through Google Ads. In this tutorial, we will learn how to integrate Google AdMob SDK in Android Application and display AdMob ad.


Step 2

Create New Project

Create a new project in Android Studio, named GAdMobDemo.


Step 3

Integrate Mobile Ads SDK

To show ads in your application, you need to integrate Google Mobile Ads SDK as dependency. To do so, open your app’s build.gradle file and the following line in the dependencies section :

<br />
compile ''<br />


Step 4

Create Ad Unit Id

Next, you need to create an Ad Unit Id. It is a unique identifier generated by creating a Ad Unit in your AdMob account. This Ad Unit Id will be used to display ads in app.

Step 4.1

Add New App

To create ad unit id, you need to sign in to your AdMob account at At AdMob dashboard, click on Monetize tab and then click on Monetize New App button.


This will redirect you to Monetize New App form. There select “Add your app manually tab” and add your app’s name and choose its platfrom.



Step 4.2

Create New Add Unit

After adding your app, you need to create a new ad unit. So, select type of ad, i.e., banner or interstitial. Then you need to provide following details to create ad unit :

  • Ad type : Ad type defines the type of ad that will be shown on your app. So, choose the ad types allowed for this ad unit. Its recommended that you should choose all the ad types because not choosing any of the ad type might limit the revenue generated through ad unit. Its mandatory that you should select at least one ad type because unchecking all of the boxes will result in an error.
  • Automatic refresh : Next you need to determine the generation rate of a new ad impression. You can choose not to refresh or to refresh ads every 30 to 120 seconds. Recommended refresh rate is between 45 and 60 seconds.
  • Text ad style : Text ad style defines the text style shown in your ad. You can either select the standard style or customize it to match your app’s text style.
  • Ad unit name : Unique name for your ad unit. You can find your ad unit in dashboard using this.


After providing all of the above details, click on Save button. This will save your ad unit and auto-generate an ad unit id for the unit. You can use this id to integrate ads in your application



Step 5

Integrate Ads in App

After creating unit id, you need to integrate the ad in your app. There are two type of basic ads are provided by AdMob :

  • Banner Ad : Ad shown as small banner at bottom of the screen
  • Interstitial Ad : This ad takes the full screen of the device. User can either click on it or can choose to close by clicking on close icon.


Step 5.1

Add Id as String

To display an ad, you need to define the ad unit id in your app. Open your string.xml file and add a new item, named banner_unit_id and assign ca-app-pub-3940256099942544/6300978111 to it.

<br />
&lt;string name=&quot;banner_unit_id&quot;&gt;ca-app-pub-3940256099942544/6300978111&lt;/string&gt;<br />

This ad id is the recommended ad id to use during development as it will display test ads. You can also add your ad id instead of test id but Google recommends you not to click on your ads because it may lead to suspension of your AdMob account.

Step 5.2

Add AdView in Layout

Next step is to add AdView in your layout. Mobile SDK will use this ad view in layout to show your ad. Open your activity_main.xml file and add the following code snippet :

<br />
&lt;RelativeLayout xmlns:android=&quot;;<br />
    xmlns:tools=&quot;;<br />
    android:layout_width=&quot;match_parent&quot;<br />
    android:layout_height=&quot;match_parent&quot;<br />
    xmlns:ads=&quot;;<br />
<p>    &lt;<br />
        android:id=&quot;@+id/adView&quot;<br />
        android:layout_width=&quot;wrap_content&quot;<br />
        android:layout_height=&quot;wrap_content&quot;<br />
        android:layout_centerHorizontal=&quot;true&quot;<br />
        android:layout_alignParentBottom=&quot;true&quot;<br />
        ads:adSize=&quot;BANNER&quot;<br />
        ads:adUnitId=&quot;@string/banner_ad_unit_id&quot;&gt;<br />
    &lt;/;<br />
&lt;/RelativeLayout&gt;<br />


Step 5.3

Initialize and Load Ad

Final step to display ad in your app is initialize and load it. To do so, first we have to initialize the AdView, then request for the ad by creating an ad request through AdRequest class of SDK and finally load the the ad to AdView.
Here is the code snippet for the same :

<br />
public class MainActivity extends AppCompatActivity {</p>
<p>    AdView mAdView;</p>
<p>    @Override<br />
    protected void onCreate(Bundle savedInstanceState) {<br />
        super.onCreate(savedInstanceState);<br />
<p>        AdView mAdView = (AdView) findViewById(;<br />
        AdRequest adRequest = new AdRequest.Builder()<br />
                                .build();<br />
        mAdView.loadAd(adRequest);<br />
    }<br />
}<br />


Step 5.4

Run Your App

Now, integration of Mobile Ad SDK in your app is complete and app is ready to show ad. So, run your app and you will see a test ad at the bottom of your screen.


Step 6

Integrate Interstitial Ads in App

Next, we will learn how to integrate Interstitial Ads in Android app. Unlike Banner Ads, Interstitial Ads takes full screen and covers the user interface of the app. Basically, they are displayed when a transition occurs in app such as navigating from one activity to another or user switch from one level to another. So, when an interstitial ads is displayed then used can either tap on the ad and redirect to its destination or close the ad by clicking on Close icon and return to app. Interstitial ads are more effective than Banner Ads because of their larger size which makes it easy to catch user’s attention.

In this sample, we will use test ad unit id to display ad. You can create a new ad unit id for Interstitial Ads using the same process we used to create ad unit id for Banner ad.


Step 6.1

Add Layout

To add Interstitial ad in our app, we need not to add a specific layout in layout. All you need to do is, initiate the ad using InterstitialAd object and set ad unit id after initialization. Interstitial ad should be loaded asynchronously as it takes more time to load than Banner ad because of its larger size. So you need to load the ad in advance and display it whenever there’s a transition in your app.
In this sample, we will display ad on click of the button. So, open your activity_main.xml file and add a button in the layout.

<br />
&lt;Button<br />
    android:layout_width=&quot;wrap_content&quot;<br />
    android:layout_height=&quot;wrap_content&quot;<br />
    android:id=&quot;@+id/button&quot;<br />
    android:text=&quot;Show Interstitial Ad&quot;<br />
    android:layout_centerVertical=&quot;true&quot;<br />
    android:layout_centerHorizontal=&quot;true&quot; /&gt;<br />


Step 6.2

Load Interstitial Ads in Acitivity

Next, you need to attach a click listener to the button which will check whether the ad is loaded or not. If loaded then it will show the ad. To load the create a new method, named requestNewInterstitial() and within it create ad request using AdRequest class and load the ad.
Here’s a code snippet to display Interstitial ad:

<br />
public class MainActivity extends AppCompatActivity {</p>
<p>    InterstitialAd mInterstitialAd;</p>
<p>    @Override<br />
    protected void onCreate(Bundle savedInstanceState) {<br />
        super.onCreate(savedInstanceState);<br />
<p>        mInterstitialAd = new InterstitialAd(this);<br />
        mInterstitialAd.setAdUnitId(&quot;ca-app-pub-3940256099942544/1033173712&quot;);<br />
        mInterstitialAd.setAdListener(new AdListener() {<br />
            @Override<br />
            public void onAdClosed() {<br />
                requestNewInterstitial();<br />
            }<br />
<p>        requestNewInterstitial();</p>
<p>        Button button = (Button) findViewById(;</p>
<p>        button.setOnClickListener(new View.OnClickListener() {<br />
            @Override<br />
            public void onClick(View v) {</p>
<p>                if (mInterstitialAd.isLoaded()) {<br />
          ;<br />
                }<br />
                else {<br />
                    doSomeThing();<br />
                }<br />
            }<br />
<p>    }</p>
<p>    private void requestNewInterstitial() {</p>
<p>        AdRequest adRequest = new AdRequest.Builder()<br />
                .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)<br />
<p>        mInterstitialAd.loadAd(adRequest);<br />
<p>    private void doSomeThing() {<br />
        Toast.makeText(this, &quot;Show Interstitial Ad&quot;, Toast.LENGTH_SHORT);<br />
    }<br />
}<br />


Step 6.3

Run Application

Now, your app is ready to display Interstitial ad. So run your app and will see Show Interstitial Ad button. Click on the button to display the ad.


Step 7

Add Ad Listeners

These ads have their own life cycle and have various events within their life cycle. SDK provides listeners so that developers can listen to those events and add functionality according to their app behavior.
Here is sample code to implement these listeners :

<br />
mAdView.setAdListener(new AdListener() {<br />
    @Override<br />
    public void onAdLoaded() {<br />
        // Code to be executed when an ad finishes loading.<br />
<p>    @Override<br />
    public void onAdFailedToLoad(int errorCode) {<br />
        // Code to be executed when an ad request fails.<br />
<p>    @Override<br />
    public void onAdOpened() {<br />
        // Code to be executed when an ad opens an overlay that<br />
        // covers the screen.<br />
<p>    @Override<br />
    public void onAdLeftApplication() {<br />
        // Code to be executed when the user has left the app.<br />
<p>    @Override<br />
    public void onAdClosed() {<br />
        // Code to be executed when when the user is about to return<br />
        // to the app after tapping on an ad.<br />
    }<br />
});<br />


Download Source Codedownload


Udemy Generic 728x90

Spread the word. Share this post!

  • Mritunjay Rawat

    all ads examples in this app with xml and java codes :-

  • Er Siddharth Sahni

    these are the demo ad , how can i integrate my own company ad (custom image or gif)

  • Chirag Solanki

    It’s Nice tutorial.
    I am also trying to implement this.It’s working fine.
    Now is it possible if I want only car racing and Entertainment type ad in my app?