Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Admob Interstitial ad (fullscreen) wont show

Tags:

android

admob

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?

like image 366
SpecialFighter Avatar asked Dec 23 '15 08:12

SpecialFighter


2 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.

like image 55
AndroidMechanic - Viral Patel Avatar answered Sep 28 '22 10:09

AndroidMechanic - Viral Patel


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

like image 40
Dhaval Parmar Avatar answered Sep 28 '22 09:09

Dhaval Parmar