How to Implement AdMob Interstitial Ads in Android Application

Admob-Interstitial

How to Implement AdMob Interstitial Ads in Android Application

1030 579 AlDeCode

In the previous post, I explained about how to implement Admob Banner Ad, in this post I will show you how to add Admob Interstitial Ad to your Android app.

Open your Project in Android Studio

Add below line to your build.gradle in dependencies

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

Now open MainActivity,java and add below 2 & 3 lines in public class as shown below

public class MainActivity extends AppCompatActivity {
InterstitialAd mInterstitialAd;
private InterstitialAd interstitial;
}

Now open MainActivity,java and add below lines inside onCreate method

AdRequest adRequest = new AdRequest.Builder().build();
// Prepare the Interstitial Ad
interstitial = new InterstitialAd(MainActivity.this);
// Insert the Ad Unit ID
interstitial.setAdUnitId(getString(R.string.admob_interstitial_id));
interstitial.loadAd(adRequest);
// Prepare an Interstitial Ad Listener
interstitial.setAdListener(new AdListener() {
public void onAdLoaded() {
// Call displayInterstitial() function
displayInterstitial();
}
});

Now add below method to your MainActivity.java

public void displayInterstitial() {
// If Ads are loaded, show Interstitial else show nothing.
if (interstitial.isLoaded()) {
interstitial.show();
}
}

Now open strings.xml from values folder and add below string to it.

<string name="admob_interstitial_id">ca-app-pub-3940256099942544/1033173712</string>

Note: Admob id used in this post for demonstration is a test ad id. Please get your interstitial AdMob ad id from Admob

Content Source: Study Warehouse

Leave a Reply