1

How to show interstitial ads?

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        //Log.v("SDL", "onCreate()");
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        interstitial = new InterstitialAd(this);
        interstitial.setAdUnitId("ca-app-pub-2188258702xxxxxxxxxxx");


        AdRequest adRequest = new AdRequest.Builder()
            .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
                .addTestDevice("XXXXXXXXXXXXX")
                .build();

        interstitial.loadAd(adRequest);

        // So we can call stuff from static callbacks
        mSingleton = this;

        // Set up the surface
        mEGLSurface = EGL10.EGL_NO_SURFACE;
        mSurface = new SDLSurface(getApplication());

        mLayout = new AbsoluteLayout(this);
        mLayout.addView(mSurface);

        setContentView(mLayout);

    }




public void displayInterstitial() {
    if (interstitial.isLoaded()) {
      interstitial.show();
    }
  }

I use sdl2 for android, and I trying to show interstitial ads in my app, but it's doesn't show. If I comment this line setContentView(mLayout); and call displayinterstitial() all works fine, except all sdl native content(screen is black):-)

And question is How to make work interstitial ads with sdl on android?

Full SDLActivity code here: http://pastebin.com/DsRRtRS5

JustOneMan
  • 231
  • 1
  • 9
  • 34

2 Answers2

1

It seems that you have not mentioned: displayInterstitial(), anywhere in onCreate() Method, You sure the code execution reaches the displayInterstitial() method?

Also, you will have some point where the code returns after the ad has been fully loaded and ready to display. Such as:

@Override
  public void onReceiveAd(Ad ad) {
    Log.d("OK", "Received ad");
    if (ad == interstitial) {
      interstitial.show();
    }
  }
Kailas
  • 7,350
  • 3
  • 47
  • 63
  • It's a little strange situation for me. If I comment this line setContentView(mLayout); and call displayinterstitial() all works fine and interstitial shows fine on any function i call it. but sdl content doesn't show. But if I dont't comment this line SetContentView(mLayout); and SDL(native) content shows correctly interstitial show only if i do: interstitial.setAdListener(new AdListener(){ public void onAdLoaded(){ displayInterstitial(); } }); – JustOneMan Jul 23 '14 at 11:17
  • And i know interstitial needs to be loaded. I see in logcat when it's loaded and call it but it doesnt shows. – JustOneMan Jul 23 '14 at 11:18
  • setContentView(mLayout); I didn't get the issue full, interstitial ads are popup right? Doesn't need to have a layout set... which we are trying in setContentView(mLayout); – Kailas Jul 23 '14 at 12:10
  • mLayout is for showing SDL(native c++ content), and interstitial ads doesn't shows if SDL content shows, but if I coment itSetContentView(mLayout); (i.e. I don't show SDL content) when interstitial works fine. – JustOneMan Jul 23 '14 at 12:37
  • My SDL apps needs setContentView(mLayout); to show it self:-) – JustOneMan Jul 23 '14 at 12:39
  • Do **NOT** call ```displayInterstitial``` from ```onAdLoaded()```. It will generate a very unexpected and unpleasant user experience and is likely to get your account banned. – William Jul 24 '14 at 10:22
0
public class MainActivity extends Activity {
    private InterstitialAd interstitial;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Get the view from activity_main.xml
        setContentView(R.layout.activity_main);

        // Prepare the Interstitial Ad
        interstitial = new InterstitialAd(MainActivity.this);
        // Insert the Ad Unit ID
        interstitial.setAdUnitId("ca-app-pub-6641284216335957/7211421024");

        //Locate the Banner Ad in activity_main.xml
        AdView adView = (AdView) this.findViewById(R.id.adView);

        // Request for Ads
        AdRequest adRequest = new AdRequest.Builder()

        // Add a test device to show Test Ads
         .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
         .addTestDevice("")
                .build();

        // Load ads into Banner Ads
        adView.loadAd(adRequest);

        // Load ads into Interstitial Ads
        interstitial.loadAd(adRequest);

        // Prepare an Interstitial Ad Listener
        interstitial.setAdListener(new AdListener() {
            public void onAdLoaded() {
                // Call displayInterstitial() function
                displayInterstitial();
            }
        });
    }
    public void displayInterstitial() {
        // If Ads are loaded, show Interstitial else show nothing.
        if (interstitial.isLoaded()) {
            interstitial.show();
        }
    }
}
DanM7
  • 2,203
  • 3
  • 28
  • 46
  • 2
    It's generally a good idea to add some explanation to your code, not just post a block of code as your answer. – DanM7 Sep 03 '14 at 18:16