Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App
geeksforgeeks
Browser
Continue

Related Articles

Android | AdMob Interstitial Ads for Android Studio

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

Interstitial ads are full-screen ads that cover the whole UI of the app. This article shows you how to integrate Interstitial ads from AdMob into an Android app. Example – First Create a new Project in Android Studio and add the following codes to import the google Mobile Ads SDK. In the project-level build.gradle file, add the highlighted code to the allprojects section. 

Java




allprojects
{
    repositories
    {
        google()
            jcenter()
                maven
        {
            url "https://maven.google.com"
        }
    }
}

In the app-level build.gradle file, add the highlighted code to dependencies section. 

Java




dependencies
{
implementation fileTree (dir : 'libs', include : [ '*.jar' ])
implementation 'com.android.support:appcompat-v7:26.1.0'
compile 'com.google.android.gms:play-services-ads:15.0.0'

Add the following code to Main Activity to initialize Mobile Ads SDK (this only needs to be done once in app lifecycle). You can find the app’s App ID in AdMob console. 

Java




package org.geeksforgeeks.geeksforgeeks;
 
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import com.google.android.gms.ads.MobileAds;
 
public class MainActivity extends AppCompatActivity
{
    protected void onCreate (Bundle savedInstanceState)
    {
        super.onCreate (savedInstanceState);
        setContentView (R.layout.activity_main);
 
        // Initialize the Mobile Ads SDK
        MobileAds.initialize (this, getString (R.string.admob_app_id));
    }
}

Add the highlighted code to Main Activity to show Interstitial Ad: MainActivity.class – 

JAVA




package org.geeksforgeeks.geeksforgeeks;
 
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
 
import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.InterstitialAd;
import com.google.android.gms.ads.MobileAds;
 
public class MainActivity extends AppCompatActivity {
 
    private InterstitialAd interstitial;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        // Initialize the Mobile Ads SDK
        MobileAds.initialize(this, getString(R.string.admob_app_id));
         
        // create ad request
        AdRequest adIRequest = new AdRequest.Builder().build();
 
        // Create an instance of the InterstitialAd object
        interstitial = new InterstitialAd(MainActivity.this);
 
        // Set the Ad Unit ID
        interstitial.setAdUnitId(getString(R.string.admob_interstitial_id));
 
        // Load the Interstitial Ad
        interstitial.loadAd(adIRequest);
 
        // Prepare an Interstitial Ad Listener
        interstitial.setAdListener(new AdListener() {
            @Override
            public void onAdLoaded() {
                // Call displayInterstitial() function when the Ad loads
                displayInterstitial();
            }
        });
    }
 
    public void displayInterstitial() {
        // If Interstitial Ads are loaded then show them, otherwise do nothing.
        if (interstitial.isLoaded()) {
            interstitial.show();
        }
    }
}

Add the Admob App Id and Interstitial Ad Id to string.xml strings.xml – 

XML




<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="admob_app_id">
        ca-app-pub-3940256099942544~3347511713</string>
    <string name="admob_interstitial_id">
        ca-app-pub-3940256099942544/1033173712</string>
    <!-- ca-app-pub-3940256099942544~3347511713
                          this is your admob app id -->
    <!-- ca-app-pub-3940256099942544/1033173712
              this is your admob Interstitial ad id -->
</resources>


My Personal Notes arrow_drop_up
Last Updated : 17 Jan, 2023
Like Article
Save Article
Similar Reads