Home » Android » android – Admob Interstitial ad (fullscreen) wont show

android – Admob Interstitial ad (fullscreen) wont show

Posted by: admin June 15, 2020 Leave a comment

Questions:

I would like to show a fullscreen banner in my Android app.

In onCreate I call this function:

@Override 
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    showInterstitial();
}

My function:

private void showInterstitial() {

    interstitialAd = new InterstitialAd(this);
    interstitialAd.setAdUnitId(getString(R.string.ad_banner_id));
    interstitialAd.show();

    Toast.makeText(this, "Ad will load", Toast.LENGTH_SHORT).show();
}

My App will crash with this message:

Caused by: java.lang.IllegalStateException: The ad unit ID must be set
on InterstitialAd before show is called.

But i set the ad id before show, or not?

How to&Answers:

You did not call loadAd() for the interstitialAd. The interstitial ad should load before you can show it.

interstitialAd.loadAd(adRequest);

also you should check if it is loaded before calling show(). It may not be immediately available and you may want to keep it loaded in advance before you call show.

if(mInterstitial.isLoaded()){
            mInterstitial.show();
            AdRequest adRequest = new AdRequest.Builder().build();
            mInterstitial.loadAd(adRequest); //optionally load again if you plan to show another one
        }

Possible implementation (change it so suit your requirement)

So basically the following can go in onCreate()

    interstitialAd = new InterstitialAd(this);
    interstitialAd.setAdUnitId(getString(R.string.ad_banner_id));
    AdRequest adRequest = new AdRequest.Builder().build();
    interstitialAd.loadAd(adRequest);
    Toast.makeText(this, "Ad will load", Toast.LENGTH_SHORT).show();

and showInterstitial() becomes this

private void showInterstitial() {
        if(mInterstitial.isLoaded()){
                mInterstitial.show();
                //optionally load again if you plan to show another one later
                AdRequest adRequest = new AdRequest.Builder().build();
                mInterstitial.loadAd(adRequest); 
            }
    }

NOTE: call showInterstitial() when you want to display the interstitial ad. But, not immediately after calling loadAd(). It takes a few moments to load the intersitital ad and you may miss by a fraction of a second if the network is laggy or ad content is heavier than normal.

Also, here is the documentation for implementing Admob Intersitials the right way.

Answer:

add below perameters in com.google.android.gms.ads.AdView

ads:adSize="BANNER"
ads:adUnitId="@string/banner_ad_unit_id"

<com.google.android.gms.ads.AdView
        android:id="@+id/adView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_alignParentBottom="true"
        ads:adSize="BANNER"
        ads:adUnitId="@string/banner_ad_unit_id">
    </com.google.android.gms.ads.AdView>

verify that you have used below line in your parent layout

xmlns:ads="http://schemas.android.com/apk/res-auto"

just verify code

Answer:

public class LoadAd {

    private final AdView ad;

    InterstitialAd interstitialAd;
    boolean isInterstitialShown = false;

    public LoadAd(Context con)
    {
        if (con.getClass().getName() == MainActivity.class.getName())
        {
            interstitialAd = new InterstitialAd(con);
            interstitialAd.setAdUnitId("ca-app-pub-8037008543529602/2054128571");

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

            interstitialAd.setAdListener(new AdListener()
            {
                @Override
                public void onAdLoaded()
                {
                    super.onAdLoaded();
                }

                @Override
                public void onAdOpened()
                {
                    super.onAdOpened();
                    isInterstitialShown = true;
                }


                @Override
                public void onAdFailedToLoad(int errorCode)
                {
                    super.onAdFailedToLoad(errorCode);
                }
            });

        }

        ad = (AdView) ((Activity)con).findViewById(R.id.adView);
    }


    public void init()
    {
        if (ad != null)
        {
            AdRequest req = new AdRequest.Builder().build();
            ad.loadAd(req);
        }
    }


    public void destroy()
    {
        if (ad != null)
        {
            ad.destroy();
        }
    }


    public boolean showInterstetial()
    {
        if (isInterstitialShown)
        {
            return false;
        }
        if (interstitialAd != null)
        {
            if (interstitialAd.isLoaded())
            {
                interstitialAd.show();
            }
            else
            {
                return false;
            }
            return true;
        }
        return false;
    }
}