user3930695
user3930695

Reputation: 31

Interstitial Ad not showing (with Banner ads)

I tried to add the code as per Google ADMOB Ads but it didn't work. Then I started working with GITHUB example to add Interstitial ads. I have no errors whatsoever in my project. Eclipse shows nothing in place of ads. Please have a look at my code and tell me where am I going wrong. I will highlight stuff wherever necessary. BANNER ADS ARE WORKING!!!!

public class AppGame extends Activity {


AdView adView;

 /** The log tag. */
  private static final String LOG_TAG = "InterstitialSample";

  /** Your ad unit id. Replace with your actual ad unit id. */
  private static final String AD_UNIT_ID1 = "INSERT AD ID HERE";

  /** The interstitial ad. */
  private InterstitialAd interstitialAd;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    interstitialAd = new InterstitialAd(this);
    interstitialAd.setAdUnitId(AD_UNIT_ID1);

    // Set the AdListener.
    interstitialAd.setAdListener(new AdListener() {
      @Override
      public void onAdLoaded() {
        Log.d(LOG_TAG, "onAdLoaded");

      }

      @Override
      public void onAdFailedToLoad(int errorCode) {


        // Change the button text and disable the button.

      }
    });

}



/** Called when the Load Interstitial button is clicked. */
  public void loadInterstitial(View unusedView) {
    // Disable the show button until the new ad is loaded.

    // Check the logcat output for your hashed device ID to get test ads on a physical device.
    AdRequest adRequest = new AdRequest.Builder()
        .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
        .addTestDevice("INSERT_YOUR_HASHED_DEVICE_ID_HERE")
        .build();

    // Load the interstitial ad.
    interstitialAd.loadAd(adRequest);
  }

  /** Called when the Show Interstitial button is clicked. */
  public void showInterstitial(View unusedView) {
    // Disable the show button until another interstitial is loaded.


    if (interstitialAd.isLoaded()) {
      interstitialAd.show();
    } else {
      Log.d(LOG_TAG, "Interstitial ad was not ready to be shown.");
    }
  }

  /** Gets a string error reason from an error code. */
  private String getErrorReason(int errorCode) {
    String errorReason = "";
    switch(errorCode) {
      case AdRequest.ERROR_CODE_INTERNAL_ERROR:
        errorReason = "Internal error";
        break;
      case AdRequest.ERROR_CODE_INVALID_REQUEST:
        errorReason = "Invalid request";
        break;
      case AdRequest.ERROR_CODE_NETWORK_ERROR:
        errorReason = "Network Error";
        break;
      case AdRequest.ERROR_CODE_NO_FILL:
        errorReason = "No fill";
        break;
    }
    return errorReason;
  }

  }

Upvotes: 0

Views: 4140

Answers (4)

Sỹ Phạm
Sỹ Phạm

Reputation: 540

onCreate add below lines

mInterstitialAd = new InterstitialAd(this);
        // set the ad unit ID
        mInterstitialAd.setAdUnitId(getString(R.string.interstitial_full_screen));
        mInterstitialAd.setAdListener(new AdListener() {
            @Override
            public void onAdClosed() {
            }
            @Override
            public void onAdLoaded() {
                // Ads loaded, stop requesting
                    mInterstitialAd.show();
               //update time start
                    timeStart = new Date().getTime();
            }
        });

onResume check time show Intertitial Ads and send request Ads:

  if(new Date().getTime()-timeStart>=timeShowAds) {
      AdRequest adRequest = new AdRequest.Builder()
              .addTestDevice("36E0B62FE34536712ED1BEBD85467085")
              .build();
      // Load ads into Interstitial Ads
      mInterstitialAd.loadAd(adRequest);
}

Upvotes: 0

Vinil Chandran
Vinil Chandran

Reputation: 1561

I have the same problem in my one of the testing device. I searched so many days. But i didn't get any solution.

At last I tried by removing the code

.addTestDevice("INSERT_YOUR_HASHED_DEVICE_ID_HERE")

and I got successful result.

So try like this at last step of testing.

Upvotes: 0

Anita Smith
Anita Smith

Reputation: 33

Call loadInterstitial(v) after setting setting the adunit:

 interstitialAd.setAdUnitId(AD_UNIT_ID1);
 loadInterstitial(v);

And call showInterstitial(v) when/where you want to show the ad:

Upvotes: 1

Gooey
Gooey

Reputation: 4778

You forgot to call the loadInterstitial function for the interstitial ad to load.

In your onCreate function, call the loadInterstitial function. Then add the listener to it like so:

interstitialAd.setAdListener(new AdListener() {
      @Override
      public void onAdLoaded() {
        Log.d(LOG_TAG, "onAdLoaded");
        interstitialAd.show();
      }

or set a boolean to true and at a natural breakpoint in your app, check if the boolean is true and then show the ad.

Upvotes: 0

Related Questions