<p class="p2"><strong><span class="s1" style="color: #008000;"><a style="color: #008000;" href="https://developers.google.com/admob/">AdMob</a></span></strong> is a multi-platform mobile ad network that allows you to monetize your android app. It is very useful particularly when you are publishing a free app and want to earn some money from it. Integrating AdMob is such an easy task that it takes not more than 5mins.</p>
<p class="p2">In this article, I will teach you how to integrate AdMob in your application without using Firebase. By the end of this article, you will know how to integrate 2 different types of ads in your application, <strong><span style="color: #008000;">Banners ads </span></strong><span style="color: #000000;">and</span><strong><span style="color: #008000;"> Interstitial ads</span></strong>.</p>
<p><amp-youtube layout="responsive" width="1200" height="675" data-videoid="EMZDV9RApkU" title="Android Integrating AdMob"><a placeholder href="https://youtu.be/EMZDV9RApkU"><img src="https://i.ytimg.com/vi/EMZDV9RApkU/hqdefault.jpg" layout="fill" object-fit="cover" alt="Android Integrating AdMob"></a></amp-youtube></p>
<h3><strong><span style="color: #000080;">Type of AdMob Ads</span></strong></h3>
<p>AdMob offers a number of different ad formats, so you can choose the one that best fits your app&#8217;s user experience.</p>
<h4 class="p1"><span style="color: #0000ff;"><b>Banner Ad</b></span></h4>
<p class="p1"><strong><span style="color: #008000;"><a style="color: #008000;" href="https://developers.google.com/admob/android/banner"><span class="s1">Banner</span></a></span></strong> Ads occupies only portion of the screen depending on the ad size that is created and stays on screen while users are interacting with the app, and can refresh automatically after a certain period of time.</p>
<p>It comes in multiple sizes <span style="color: #008000;"><b>Standard, Medium, Large, Full-Size, Leaderboard</b><span style="color: #000000;">,</span><span style="color: #000000;"> and</span> <b>Smart Banner</b>.</span></p>
<p class="p1">Smart banners are very useful when you target multiple device sizes. It detects the width of the device in its current orientation and creates the ad view that size.</p>
<h4 class="p1"><span style="color: #0000ff;"><b>Interstitial Ad</b></span></h4>
<p class="p1"><strong><span style="color: #008000;"><a style="color: #008000;" href="https://developers.google.com/admob/android/interstitial"><span class="s1">Interstitial</span></a></span></strong> ads are full-screen ads that cover the interface of an app until closed by the user. Basically, they will shown on a timely basis, between screen transition or when the user is done with a task. Usually, we can see these ads in games displaying Ad when a level is completed.</p>
<h4 class="p1"><span style="color: #0000ff;"><b>Rewarded Video Ad</b></span></h4>
<p class="p1"><span style="color: #008000;"><strong><a style="color: #008000;" href="https://developers.google.com/admob/android/rewarded-video"><span class="s1">Rewarded</span></a></strong></span> Video Ads are fullscreen video ads which offers some reward points if the user watches the ad video. These ads are very useful to offer some reward points / coins in video games.</p>
<h4 class="p1"><span style="color: #0000ff;"><b>Native Ad</b></span></h4>
<p class="p1"><strong><span style="color: #008000;"><a style="color: #008000;" href="https://developers.google.com/admob/android/native"><span class="s1">Native</span></a></span></strong> Ads offers flexibility to configure the ad appearance like color, text color and buttons to make them appear as native component of the app.</p>
<h3><strong><span style="color: #000080;">Setting Up AdMob</span></strong></h3>
<p>Before displaying ads and earning revenue firstly we have to do some three-step setup.</p>
<h4 id="import_the_mobile_ads_sdk"><strong><span style="color: #000080;">Import the Mobile Ads SDK</span></strong></h4>
<p>Add the below dependency, which instructs Gradle to pull in the latest version of the <span style="color: #0000ff;"><strong>Mobile Ads SDK</strong></span> and additional related dependencies and then perform a Gradle <span style="color: #008000;"><strong>sync</strong></span>.</p>
<pre>dependencies {
 implementation fileTree(dir: 'libs', include: ['*.jar'])
 implementation 'androidx.appcompat:appcompat:1.1.0'
 implementation 'androidx.constraintlayout:constraintlayout:1.1.3'

 <span style="color: #008000;"><strong>implementation 'com.google.android.gms:play-services-ads:18.3.0'</strong></span>
}</pre>
<h4 id="update_your_androidmanifestxml"><strong><span style="color: #000080;">Update your AndroidManifest.xml</span></strong></h4>
<h5><span style="color: #0000ff;"><strong>Creating AdMob APP ID</strong></span></h5>
<p class="p1"><b>1</b>. Sign into your <strong><span style="color: #008000;"><a style="color: #008000;" href="https://apps.admob.com/"><span class="s1">AdMob</span></a></span></strong> account.</p>
<p class="p1"><b>2</b>. Click on <span style="color: #008000;"><strong>Apps</strong></span> option at the left side and then click on <span style="color: #008000;"><strong>ADD APP</strong></span> to add an app in which you want to integrate AdMob.</p>
<p><img class=" wp-image-1480 aligncenter" src="https://c1ctech.com/wp-content/uploads/2020/01/Screenshot-2020-01-24-12.11.21.png" alt="Screenshot 2020-01-24 12.11.21" width="604" height="311" /></p>
<p><b>3</b>. Now it will ask one question that the app in which you want to add AdMob is already published on Google Play or the App Store. In our case say <strong><span style="color: #008000;">No</span></strong>.</p>
<p><img class=" wp-image-1481 aligncenter" src="https://c1ctech.com/wp-content/uploads/2020/01/Screenshot-2020-01-24-12.12.26.png" alt="Screenshot 2020-01-24 12.12.26" width="573" height="247" /></p>
<p><b>4</b>. Provide the <strong><span style="color: #008000;">application name</span>,</strong> select the <span style="color: #008000;"><strong>platform</strong></span> in which you have created your app and then finally click on <span style="color: #008000;"><strong>ADD</strong></span>.</p>
<p><img class=" wp-image-1482 aligncenter" src="https://c1ctech.com/wp-content/uploads/2020/01/Screenshot-2020-01-24-12.13.32.png" alt="Screenshot 2020-01-24 12.13.32" width="616" height="233" /></p>
<p><b>5</b>. <span style="color: var(--color-text);">Once the App is added to AdMob, you can find the <strong><span style="color: #008000;">APP ID</span></strong> on the dashboard which looks like </span><span style="color: #008000;"><b>ca-app-pub-XXXXXXXXX~XXXXXXXXX</b></span><span style="color: var(--color-text);">.</span></p>
<h5><strong><span style="color: #0000ff;">Adding AdMob App Id in ManifestFile</span></strong></h5>
<p class="p1">Add your AdMob <strong><span style="color: #008000;"><span class="s1">App</span><span class="s1"> ID</span></span></strong> to your app&#8217;s <span class="s2">AndroidManifest.xml</span> file by adding a tag with name <strong><span class="s2" style="color: #008000;">com.google.android.gms.ads.APPLICATION_ID</span></strong>, as shown below.</p>
<pre><;?xml version="1.0" encoding="utf-8"?>;
<;manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.androidadmobdemo">;

<;application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">;

<;activity android:name=".MainActivity">;
<;intent-filter>;
<;action android:name="android.intent.action.MAIN" />;
<;category android:name="android.intent.category.LAUNCHER" />;
<;/intent-filter>;
<;/activity>;

<strong><span style="color: #008000;"><;meta-data</span></strong>
<strong><span style="color: #008000;">android:name="com.google.android.gms.ads.APPLICATION_ID"</span></strong>
<strong><span style="color: #008000;">android:value="ENTER YOUR ADMOB APP ID" />;</span></strong>

<;/application>;

<;/manifest>;</pre>
<h4 id="initialize_mobile_ads_sdk"><strong><span style="color: #000080;">Initialize Mobile Ads SDK</span></strong></h4>
<p>Create a class named <span style="color: #008000;"><strong>MyApplication.java</strong></span> and extend the class from Application. In this application class we have to globally initialize the <span style="color: #008000;"><strong>AdMob App Id</strong></span>. Here we use <span style="color: #0000ff;"><strong>MobileAds.initialize()</strong></span> method to initialize the AdMob.</p>
<p><span style="color: #0000ff;"><strong>MyApplication.java</strong></span></p>
<pre>package com.example.androidadmobdemo;

import android.app.Application;

import com.google.android.gms.ads.MobileAds;

public class MyApplication extends Application {

 @Override
 public void onCreate() {
 super.onCreate();

 <span style="color: #008000;"><strong>//initializes Mobile Ads SDK</strong></span>
 MobileAds.initialize(this, "ENTER YOUR ADMOB APP_ID");
 }
}</pre>
<p>Open<span style="color: #008000;"> <strong>AndroidManifest.xml</strong> </span>and add <span style="color: #008000;"><strong>MyApplication</strong></span> to <strong><;<span style="color: #008000;">application</span>>;</strong> tag to execute the class on app launch.</p>
<p><strong><span style="color: #0000ff;">AndroidManifest.xml</span></strong><!--?xml version="1.0" encoding="utf-8"?--></p>
<pre><;?xml version="1.0" encoding="utf-8"?>;
<;manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.androidadmobdemo">;

<;application
<strong><span style="color: #008000;">android:name=".MyApplication"</span></strong>
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">;

<;activity
android:name=".InterstitialAdActivity"
android:label="Interstitial" />;
<;activity android:name=".MainActivity">;
<;intent-filter>;
<;action android:name="android.intent.action.MAIN" />;

<;category android:name="android.intent.category.LAUNCHER" />;
<;/intent-filter>;
<;/activity>;

<strong><span style="color: #008000;"><;meta-data</span></strong>
<strong><span style="color: #008000;">android:name="com.google.android.gms.ads.APPLICATION_ID"</span></strong>
<strong><span style="color: #008000;">android:value="ENTER YOUR ADMOB APP ID" />;</span></strong>
<;/application>;

<;/manifest>;</pre>
<h3><strong><span style="color: #000080;">Creating Ad Units</span></strong></h3>
<p class="p1"><b>1</b>. Sign into your <strong><span style="color: #008000;"><a style="color: #008000;" href="https://apps.admob.com/"><span class="s1">AdMob</span></a></span></strong> account.</p>
<p class="p1"><strong>2 </strong>. From the left side click on <span style="color: #008000;"><strong>Apps</strong></span> and then select the newly created App(In my case <strong>AndroidAdMobDemo</strong>).</p>
<p class="p1"><strong>3</strong>. Inside your App name select <span style="color: #008000;"><strong>Ad units</strong></span> and click on <span style="color: #008000;"><b>ADD AD UNIT</b> </span>button to create a new ad unit.</p>
<p class="p1"><img class=" wp-image-1484 aligncenter" src="https://c1ctech.com/wp-content/uploads/2020/01/Screenshot-2020-01-24-13.17.28.png" alt="Screenshot 2020-01-24 13.17.28" width="663" height="301" /></p>
<p class="p1"><b>4</b>. Select the <span style="color: #008000;"><b>ad format</b></span> and give the ad unit a name.</p>
<p class="p1"><b>5</b>. Once the ad unit is created, you can notice the <span style="color: #008000;"><b>Ad unit ID</b></span> on the dashboard. An example of ad unit id looks like <b><span style="color: #008000;">ca-app-pub-962XXXXXXX/XXXXXXXXXXX</span>.</b></p>
<p class="p1">Create as many ad units as required for your app.</p>
<h3 class="p1"><span style="color: #000080;"><strong>Adding Banner Ad</strong></span></h3>
<p class="p1"><strong><span class="s1" style="color: #008000;"><a style="color: #008000;" href="https://developers.google.com/admob/android/banner">Banner</a></span></strong> ads occupy only a portion of the screen. I am adding a banner ad in my main activity aligning to bottom of the screen. In order to add the banner ad, you need to add <span style="color: #0000ff;"><b>com.google.android.gms.ads.AdView</b></span> element to your xml layout.</p>
<pre><;com.google.android.gms.ads.AdView
android:id="@+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="10dp"
ads:adSize="BANNER"
ads:adUnitId="ca-app-pub-3940256099942544/6300978111"/>;

</pre>
<p>There are two different parameters that you should notice here, <strong><span style="color: #008000;">adUnitId</span></strong> and <strong><span style="color: #008000;">adSize</span></strong> . <strong><span style="color: #0000ff;">adUnitId</span></strong> 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 you are developing/testing an app. A real ID should only be used in production.</p>
<pre>ca-app-pub-3940256099942544/6300978111</pre>
<p><strong><span style="color: #0000ff;">adSize</span></strong> defines the size of your banner, there are various banner sizes available which must have a look at <strong><span style="color: #008000;"><a style="color: #008000;" href="https://developers.google.com/admob/android/banner#banner_sizes">here</a></span></strong>.</p>
<p>You can also add an AdView dynamically :</p>
<pre>AdView adView = new AdView(this);
adView.setAdSize(AdSize.BANNER);
adView.setAdUnitId("ca-app-pub-3940256099942544/6300978111");
<span style="color: #008000;"><strong>
//add adview to your view hierarchy</strong></span>
viewGroup.addChild(adView);</pre>
<p class="p1">Open the layout file of your main activity (<span style="color: #008000;"><b>activity_main.xml</b></span>) and add the <strong><span style="color: #008000;"><a style="color: #008000;" href="https://developers.google.com/android/reference/com/google/android/gms/ads/AdView"><span class="s1">AdView</span></a></span></strong> widget. I am also adding a button to launch another activity in which we’ll try <strong><span style="color: #008000;">Interstitial ad.</span></strong></p>
<p><span style="color: #0000ff;"><b>activity_main.xml</b></span></p>
<!-- WP QUADS Content Ad Plugin v. 2.0.98.1 -->
<div class="quads-location quads-ad2" id="quads-ad2" style="float:none;margin:0px;">

</div>

<pre>package com.example.androidadmobdemo;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

private AdView mAdView;
Button btnFullScreenAd;

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

mAdView = findViewById(R.id.adView);

btnFullScreenAd = findViewById(R.id.btn_fullscreen_ad);
btnFullScreenAd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(new Intent(MainActivity.this, InterstitialAdActivity.class));
}
});

AdRequest adRequest = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.build();

mAdView.loadAd(adRequest);

mAdView.setAdListener(new AdListener() {

<strong><span style="color: #008000;">// Code to be executed when an ad finishes loading.</span></strong>
@Override
public void onAdLoaded() {
}

<strong><span style="color: #008000;">//Code to be executed when an ad opens an overlay that
// covers the screen.</span></strong>
@Override
public void onAdOpened() {
super.onAdOpened();
}

<strong><span style="color: #008000;">// Code to be executed when an ad request fails.</span></strong>
@Override
public void onAdFailedToLoad(int errorCode) {
Toast.makeText(getApplicationContext(), "Ad failed to load! error code: " + errorCode, Toast.LENGTH_SHORT).show();
}

<span style="color: #008000;"><strong>//called after onAdOpened(), when a user click opens another app (such as the Google Play), backgrounding the current app</strong></span>
@Override
public void onAdLeftApplication() {
Toast.makeText(getApplicationContext(), "Ad left application!", Toast.LENGTH_SHORT).show();
}

<strong><span style="color: #008000;">// Code to be executed when the user clicks on an ad.</span></strong>
@Override
public void onAdClicked() {

}

<strong><span style="color: #008000;">// Code to be executed when the user is about to return
// to the app after tapping on an ad.</span></strong>
@Override
public void onAdClosed() {
Toast.makeText(getApplicationContext(), "Ad is closed!", Toast.LENGTH_SHORT).show();
}
});

}
@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();
}
}</pre>
<h3><span style="color: #000080;"><strong>Loading Ad</strong></span></h3>
<p>After adding the AdView, we will now load an ad into it.</p>
<pre>public class MainActivity extends AppCompatActivity {

private AdView mAdView;

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

mAdView = findViewById(R.id.adView);

AdRequest adRequest = new AdRequest.Builder()
.build();

mAdView.loadAd(adRequest);
}</pre>
<p>Get a reference to the <span style="color: #008000;"><strong>AdView</strong></span>, create a <strong><span style="color: #008000;">AdRequest</span></strong> object, load the ad by calling <strong><span style="color: #008000;">loadAd</span></strong> on the AdView reference and passing <strong><span style="color: #008000;">adRequest</span></strong> into it.</p>
<h3><strong><span style="color: #000080;">Handling ad events</span></strong></h3>
<p>To further customize the behavior of your ad, you can hook onto a number of events in the ad&#8217;s lifecycle: loading, opening, closing, and so on. You can listen for these events through the <strong><span style="color: #008000;">AdListener</span></strong> class.</p>
<p>To use an AdListener with <strong><span style="color: #008000;">AdView</span></strong>, simply call the <strong><span style="color: #008000;">setAdListener()</span></strong> method:</p>
<pre>mAdView.setAdListener(new AdListener() {

<strong><span style="color: #008000;">// Code to be executed when an ad finishes loading.</span></strong>
@Override
public void onAdLoaded() {
}

<span style="color: #008000;"><strong>//Code to be executed when an ad opens an overlay that</strong></span>
<span style="color: #008000;"><strong>// covers the screen.</strong></span>
@Override
public void onAdOpened() {
super.onAdOpened();
}

<span style="color: #008000;"><strong>// Code to be executed when an ad request fails.</strong></span>
@Override
public void onAdFailedToLoad(int errorCode) {
Toast.makeText(getApplicationContext(), "Ad failed to load! error code: " + errorCode, Toast.LENGTH_SHORT).show();
}

<strong><span style="color: #008000;">//called after onAdOpened(), when a user click opens another app </span></strong>
<strong><span style="color: #008000;">//(such as the Google Play), backgrounding the current app</span></strong>
@Override
public void onAdLeftApplication() {
Toast.makeText(getApplicationContext(), "Ad left application!", Toast.LENGTH_SHORT).show();
}

<strong><span style="color: #008000;">// Code to be executed when the user clicks on an ad.</span></strong>
@Override
public void onAdClicked() {

}

<strong><span style="color: #008000;">// Code to be executed when the user is about to return</span></strong>
<strong><span style="color: #008000;">// to the app after tapping on an ad.</span></strong>
@Override
public void onAdClosed() {
Toast.makeText(getApplicationContext(), "Ad is closed!", Toast.LENGTH_SHORT).show();
}
});</pre>
<p>Open <strong><span style="color: #0000ff;">MainActivity.java</span></strong> and modify the code as shown.</p>
<ul>
<li></li>
<li>Create an instance of <strong><span style="color: #008000;">AdRequest</span></strong> and load the ad into AdView.</li>
<li>Add the AdView life cycle methods in <strong><span style="color: #008000;">onResume(), onPause() </span></strong><span style="color: #008000;"><span style="color: #000000;">and in </span></span><strong><span style="color: #008000;">onDestroy()</span></strong> methods.</li>
</ul>
<p><strong><span style="color: #0000ff;">MainActivity.Java</span></strong></p>
<pre>package com.example.androidadmobdemo;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;

import androidx.appcompat.app.AppCompatActivity;


public class MainActivity extends AppCompatActivity {

private AdView mAdView;
Button btnFullScreenAd;


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

mAdView = findViewById(R.id.adView);

btnFullScreenAd = findViewById(R.id.btn_fullscreen_ad);
btnFullScreenAd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(new Intent(MainActivity.this, InterstitialAdActivity.class));
}
});

AdRequest adRequest = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.build();

mAdView.loadAd(adRequest);

mAdView.setAdListener(new AdListener() {

<span style="color: #008000;"><strong>// Code to be executed when an ad finishes loading.</strong></span>
@Override
public void onAdLoaded() {
}

<strong><span style="color: #008000;">//Code to be executed when an ad opens an overlay that</span></strong>
<strong><span style="color: #008000;">// covers the screen.</span></strong>
@Override
public void onAdOpened() {
super.onAdOpened();
}

<span style="color: #008000;"><strong>// Code to be executed when an ad request fails.</strong></span>
@Override
public void onAdFailedToLoad(int errorCode) {
Toast.makeText(getApplicationContext(), "Ad failed to load! error code: " + errorCode, Toast.LENGTH_SHORT).show();
}

<span style="color: #008000;"><strong>//called after onAdOpened(), when a user click opens another app (such as the Google Play), backgrounding the current app</strong></span>
@Override
public void onAdLeftApplication() {
Toast.makeText(getApplicationContext(), "Ad left application!", Toast.LENGTH_SHORT).show();
}

<strong><span style="color: #008000;">// Code to be executed when the user clicks on an ad.</span></strong>
@Override
public void onAdClicked() {

}

<strong><span style="color: #008000;">// Code to be executed when the user is about to return</span></strong>
<strong><span style="color: #008000;">// to the app after tapping on an ad.</span></strong>
@Override
public void onAdClosed() {
Toast.makeText(getApplicationContext(), "Ad is closed!", Toast.LENGTH_SHORT).show();
}
});
}

@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();
}
}</pre>
<p><span style="color: #0000ff;"><strong>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.</strong></span></p>
<p class="p1">Now if you run the app, you should see a banner ad at the bottom of your screen.</p>
<p><img class="alignnone wp-image-1478" src="https://c1ctech.com/wp-content/uploads/2020/01/Screenshot_1580134151.png" alt="Screenshot_1580134151" width="387" height="688" /></p>
<h3><span style="color: #000080;"><strong>Adding Interstitial Ad</strong></span></h3>
<p class="p1"><strong><span class="s1" style="color: #008000;"><a style="color: #008000;" href="https://developers.google.com/admob/android/interstitial">Interstitial</a></span></strong> ads occupies full screen of the app. Normally these ads will be populated when user is moving between activities or moving to next level when playing a game.</p>
<p class="p1">When creating interstitial ads, we don’t need to define any view in your layout file, these can only be created programmatically.</p>
<p>Make a global variable of InterstitialAd, initialize the variable in onCreate and set the AdUnitId.</p>
<pre>public class InterstitialAdActivity extends AppCompatActivity {

InterstitialAd mInterstitialAd;

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

mInterstitialAd = new InterstitialAd(this);

<strong><span style="color: #008000;">// setting ad unit ID</span></strong>
mInterstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712");
 }

 }</pre>
<p>Google provides a testing id for interstitial ad, make sure to use exactly this ID when testing/debugging your application.</p>
<pre>ca-app-pub-3940256099942544/1033173712</pre>
<p>Now we need to load the ad.</p>
<pre>AdRequest adRequest = new AdRequest.Builder()
.build();

mInterstitialAd.loadAd(adRequest);</pre>
<p>Let&#8217;s show the ad. Always check if the ad is loaded before showing it.</p>
<pre>private void showInterstitial() {

<span style="color: #008000;"><strong>// Show the ad if it's ready. Otherwise toast and restart the game.</strong></span>
if (mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
} else {
Toast.makeText(this, "Ad did not load", Toast.LENGTH_SHORT).show();
<strong><span style="color: #008000;">//play game</span></strong>
}
}</pre>
<p class="p1">We’ll test this ad by creating a second activity and popup the full-screen ad when the second activity is launched.</p>
<p class="p1">Create an activity named <span style="color: #008000;"><b>InterstitialAdActivity.java</b></span> by right-clicking on package <span style="color: #008000;"><b>New </b><span class="s2"><b>⇒</b></span><b> Activity </b><span class="s2"><b>⇒</b></span><b> Empty Activity</b>.</span></p>
<p><strong><span style="color: #0000ff;">InterstitialAdActivity.java</span></strong></p>
<pre>package com.example.androidadmobdemo;

import android.os.Bundle;
import android.widget.Toast;

import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.InterstitialAd;

import androidx.appcompat.app.AppCompatActivity;

public class InterstitialAdActivity extends AppCompatActivity {

InterstitialAd mInterstitialAd;

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


mInterstitialAd = new InterstitialAd(this);

<strong><span style="color: #008000;">// setting ad unit ID</span></strong>
mInterstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712");

AdRequest adRequest = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.build();

mInterstitialAd.loadAd(adRequest);

mInterstitialAd.setAdListener(new AdListener() {

<strong><span style="color: #008000;">// Code to be executed when an ad finishes loading.</span></strong>
public void onAdLoaded() {
showInterstitial();
}

<strong><span style="color: #008000;">// Code to be executed when the user is about to return</span></strong>
<strong><span style="color: #008000;">// to the app after tapping on an ad.</span></strong>
@Override
public void onAdClosed() {
Toast.makeText(getApplicationContext(), "Ad is closed!", Toast.LENGTH_SHORT).show();
}

<strong><span style="color: #008000;">// Code to be executed when an ad request fails.</span></strong>
@Override
public void onAdFailedToLoad(int errorCode) {
Toast.makeText(getApplicationContext(), "Ad failed to load! error code: " + errorCode, Toast.LENGTH_SHORT).show();
}

<strong><span style="color: #008000;">//called after onAdOpened(), when a user click opens another app (such as the Google Play), backgrounding the current app</span></strong>
@Override
public void onAdLeftApplication() {
Toast.makeText(getApplicationContext(), "Ad left application!", Toast.LENGTH_SHORT).show();
}

<strong><span style="color: #008000;">//Code to be executed when an ad opens an overlay that</span></strong>
<strong><span style="color: #008000;">// covers the screen.</span></strong>
@Override
public void onAdOpened() {
Toast.makeText(getApplicationContext(), "Ad is opened!", Toast.LENGTH_SHORT).show();
}
});
}

private void showInterstitial() {

<span style="color: #008000;"><strong>// Show the ad if it's ready. Otherwise toast and restart the game.</strong></span>
if (mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
} else {
Toast.makeText(this, "Ad did not load", Toast.LENGTH_SHORT).show();
<strong><span style="color: #008000;">//play game</span></strong>
}
}

}</pre>
<p class="p1">Now if you run the app, you can see the interstitial ad when the second activity is launched.</p>
<p><img class="alignnone wp-image-1479" src="https://c1ctech.com/wp-content/uploads/2020/01/Screenshot_1580134026.png" alt="Screenshot_1580134026" width="434" height="772" /></p>
<p>In the next article(ie. Part II) we will talk about how to integrate other two ads in our application ie. Rewarded video ads and Native Ads in brief.

