ALEX JOY
ALEX JOY

Reputation: 111

Android Interstitial Ad not loading

I'm trying to load interstitial ad on the tablet, with the test ad id. On output ad is not showing, but I'm getting the callback on "onAdLoaded".

This is my code:-

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

 mPublisherInterstitialAd = new PublisherInterstitialAd(this);
 mPublisherInterstitialAd.setAdUnitId("/6499/example/interstitial");
 mPublisherInterstitialAd.loadAd(newPublisherAdRequest.Builder().build());

    mPublisherInterstitialAd.setAdListener(new AdListener(){
        @Override
        public void onAdLoaded() {

            Log.d("AD ","LOADED");
        }

        @Override
        public void onAdFailedToLoad(int i) {

            Log.d("AD ","FAILED");
        }
    });
}

Upvotes: 3

Views: 4366

Answers (2)

Teshte
Teshte

Reputation: 632

Here is how I handle this kind of code

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

private void initAds() {
      MobileAds.initialize(this, "your app id");
      mInterstitialAd = new InterstitialAd(this);                         
      mInterstitialAd.setAdUnitId("your ad unit ID"); 
      mInterstitialAd.loadAd(new AdRequest.Builder().build());

      mInterstitialAd.setAdListener(new AdListener() {
        @Override
        public void onAdClosed() {
            // Load the next interstitial.
            mInterstitialAd.loadAd(new AdRequest.Builder().build());
        }

      });
}

Also in the manifest file:

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

Next, depending on the logic of your app, you would have something like:

if (mInterstitialAd.isLoaded()) {
   mInterstitialAd.show();
} else {
   System.out.println("The interstitial wasn't loaded yet. Do some other action");
}

Upvotes: 0

Siddharth Patel
Siddharth Patel

Reputation: 205

Try this

public void inrequestadd() {
    mInterstitial = new InterstitialAd(MainActivity.this);
    mInterstitial.setAdUnitId("Your ID");
    mInterstitial.loadAd(new AdRequest.Builder().build());
    mInterstitial.setAdListener(new AdListener() {

        @Override
        public void onAdClosed() {
            super.onAdClosed();

        }

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

        }

        @Override
        public void onAdLoaded() {
            super.onAdLoaded();
            if (mInterstitial.isLoaded()) {
                mInterstitial.show();
            }
        }

    });
}

Upvotes: 3

Related Questions