How to Integrate Google AdMob Interstitial Ads in a WebView Android App

Created on 18 September, 2024 • 47 views • 2 minutes read

Learn how to easily integrate Google AdMob interstitial ads into your Android WebView application. This step-by-step guide walks you through setting up the AdMob SDK, configuring the MainActivity, loading and displaying interstitial ads.

How to Add Google AdMob Interstitial Ads to a Java Android WebView App

If you are working on a WebView Android app and want to monetize it using Google AdMob interstitial ads, this guide will walk you through the process step-by-step.

1. Set up AdMob Account

If you haven’t already, sign up for AdMob. Create an interstitial ad unit and make sure to copy the ad unit ID, which will be used later in your app.

2. Add AdMob SDK to Your Project

In your app’s build.gradle file (usually the app/build.gradle), add the following dependencies:


dependencies {
    implementation 'com.google.android.gms:play-services-ads:22.0.0'
}

Once added, sync your project with Gradle.

3. Modify the AndroidManifest.xml

Ensure that your AndroidManifest.xml contains the necessary permissions and the AdMob app ID. You can set the AdMob app ID inside the <application> tag as shown below:


<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="your.package.name">
    
    <application
        android:name=".YourApplication"
        android:allowBackup="true"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        
        <!-- Add this meta-data tag -->
        <meta-data
            android:name="com.google.android.gms.ads.APPLICATION_ID"
            android:value="YOUR_ADMOB_APP_ID" />
    </application>
</manifest>

Make sure to replace YOUR_ADMOB_APP_ID with your actual AdMob app ID.

4. Initialize Mobile Ads SDK

In your MainActivity.java, initialize the Mobile Ads SDK inside the onCreate method:


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

public class MainActivity extends AppCompatActivity {

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

        // Initialize the Mobile Ads SDK
        MobileAds.initialize(this, initializationStatus -> {});
    }
}

5. Load and Display Interstitial Ads

You will now need to load and show the interstitial ad. Add the following code to your MainActivity.java:

Load the Interstitial Ad:


import com.google.android.gms.ads.interstitial.InterstitialAd;
import com.google.android.gms.ads.interstitial.InterstitialAdLoadCallback;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.LoadAdError;

public class MainActivity extends AppCompatActivity {

    private InterstitialAd mInterstitialAd;

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

        // Initialize the Mobile Ads SDK
        MobileAds.initialize(this, initializationStatus -> {});

        // Load an Interstitial Ad
        loadInterstitialAd();
    }

    private void loadInterstitialAd() {
        AdRequest adRequest = new AdRequest.Builder().build();
        InterstitialAd.load(this, "YOUR_INTERSTITIAL_AD_UNIT_ID", adRequest,
            new InterstitialAdLoadCallback() {
                @Override
                public void onAdLoaded(InterstitialAd interstitialAd) {
                    // The interstitial ad was loaded.
                    mInterstitialAd = interstitialAd;
                }

                @Override
                public void onAdFailedToLoad(LoadAdError adError) {
                    // Handle the error
                    mInterstitialAd = null;
                }
            });
    }
}

Replace YOUR_INTERSTITIAL_AD_UNIT_ID with your actual interstitial ad unit ID.

Show the Interstitial Ad:

To display the interstitial ad, you can call the following method at the appropriate time, such as when a button is clicked or a page is loaded:


private void showInterstitialAd() {
    if (mInterstitialAd != null) {
        mInterstitialAd.show(MainActivity.this);
    } else {
        // Ad wasn't ready, load again
        loadInterstitialAd();
    }
}

Call showInterstitialAd() wherever you'd like to show the ad.

6. Test Your Ads

Before going live, make sure you use test ads. You can do this by replacing your ad unit ID with Google’s test ad unit ID:


InterstitialAd.load(this, "ca-app-pub-3940256099942544/1033173712", adRequest, callback);

This helps you avoid getting flagged for invalid traffic during the development phase.

7. Follow AdMob Policies

Ensure that your app complies with AdMob’s policies, particularly in how you display interstitial ads, to avoid violations.

With this setup, you’ll be able to easily add Google AdMob interstitial ads to your WebView-based Android app. Happy coding!