Create Admob App Loading Ad on Android

Tiempo de lectura: 2 minutos

Today we are going to learn how to create an app loading ad with Admob on Android.

First, we will implement in our build.gradle:

    implementation 'com.google.android.gms:play-services-ads:23.0.0'

Then we need to initialize the ads in our MainActivity:

    MobileAds.initialize(
        this,
        new OnInitializationCompleteListener() {
          @Override
          public void onInitializationComplete(InitializationStatus initializationStatus) {}
        });

And add in the manifest (below </application>)

    <meta-data
        android:name="com.google.android.gms.ads.AD_MANAGER_APP"
        android:value="true" />

And also inside <application> </application>

        <meta-data
            android:name="com.google.android.gms.ads.APPLICATION_ID"
            android:value="YOUR_ADMOB_APP_ID" />

Replace YOUR_ADMOB_APP_ID with your Admob APP id.

Now let’s create an object called AppOpenManager.java where we will put this code:

<

pre class=”EnlighterJSRAW” data-enlighter-language=”java” data-enlighter-theme=”” data-enlighter-highlight=”” data-enlighter-linenumbers=”” data-enlighter-lineoffset=”” data-enlighter-title=”” data-enlighter-group=””>
import static android.provider.Settings.System.getString;

import android.app.Activity;
import android.content.Context;
import android.util.Log;

import androidx.annotation.NonNull;

import com.cityscaperoommadrid.R;
import com.google.android.gms.ads.AdError;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.FullScreenContentCallback;
import com.google.android.gms.ads.LoadAdError;
import com.google.android.gms.ads.appopen.AppOpenAd;

import java.util.Date;

public class AppOpenAdManager {

private long loadTime = 0;

private static final String LOG_TAG = "AppOpenAdManager";
// Load the Admob application id
private static final String AD_UNIT_ID = "ca-app-pub-3940256099942544/925739592";

private AppOpenAd appOpenAd = null;
private boolean isLoadingAd = false;
public boolean isShowingAd = false;

/**
 * Constructor.
 */
public AppOpenAdManager() {
}

/**
 * Check if ad exists and can be shown.
 */
private boolean isAdAvailable() {
    return appOpenAd != null &amp;&amp; wasLoadTimeLessThanNHoursAgo(4);
}

/**
 * Show the ad if one isn't already showing.
 */
public void showAdIfAvailable(@NonNull final Activity activity,
                              @NonNull MyApplication.OnShowAdCompleteListener onShowAdCompleteListener) {
    if (isShowingAd) {
        Log.d(LOG_TAG, "The app open ad is already showing.");
        return;
    }
    // If the app open ad is not available yet, invoke the callback then load the ad.
    if (!isAdAvailable()) {
        Log.d(LOG_TAG, "The app open ad is not ready yet.");
        onShowAdCompleteListener.onShowAdComplete();
        loadAd(activity);
        return;
    }
    appOpenAd.setFullScreenContentCallback(
            new FullScreenContentCallback() {
                @Override
                public void onAdDismissedFullScreenContent() {
                    // Set the reference to null so isAdAvailable() returns false.
                    appOpenAd = null;
                    isShowingAd = false;
                    onShowAdCompleteListener.onShowAdComplete();
                    loadAd(activity);
                }

                @Override
                public void onAdFailedToShowFullScreenContent(AdError adError) {
                    // Called when fullscreen content failed to show.
                    Log.d(LOG_TAG, "Ad failed to show.");
                    Log.d(LOG_TAG, adError.getMessage());
                    appOpenAd = null;
                    isShowingAd = false;

                    onShowAdCompleteListener.onShowAdComplete();
                    loadAd(activity);

                }

                @Override
                public void onAdShowedFullScreenContent() {
                    Log.d(LOG_TAG, "Ad showed fullscreen content.");
                }
            });
    isShowingAd = true;
    appOpenAd.show(activity);
}

public void loadAd(Context context) {
    // Do not load ad if there is an unused ad or one is already loading.
    if (isLoadingAd  isAdAvailable()) {
        return;
    }

    isLoadingAd = true;
    AdRequest request = new AdRequest.Builder().build();
    AppOpenAd.load(
            context, AD_UNIT_ID, request,
            AppOpenAd.APP_OPEN_AD_ORIENTATION_PORTRAIT,
            new AppOpenAd.AppOpenAdLoadCallback() {
                @Override
                public void onAdLoaded(AppOpenAd ad) {
                    // Called when an app open ad has loaded.
                    Log.d(LOG_TAG, "Ad was loaded.");
                    appOpenAd = ad;
                    isLoadingAd = false;
                    loadTime = (new Date()).getTime();
                }

                @Override
                public void onAdFailedToLoad(LoadAdError loadAdError) {
                    // Called

Leave a Comment