25

I used to display AdMob banner on my future apps, and I'd like to give a try to the interstitial ads.

I checked the AdMob SDK for implementation, and I copied their example source because it was exactly what I want (i.e. the interstitial shown when the activity launch).

I tried it on emulator and on my Galaxy, no ad has been displayed.

Here is the source code:

public class Asscreed extends Activity {
    private InterstitialAd interstitial;

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

        // Create the interstitial.
        interstitial = new InterstitialAd(this);
        interstitial.setAdUnitId("ca-app-pub-6046034785851961/xxxxxx");

        // Create ad request.
        AdRequest adRequest = new AdRequest.Builder().build();

        // Begin loading your interstitial.
        interstitial.loadAd(adRequest);
    }

    // Invoke displayInterstitial() when you are ready to display an interstitial.
    public void displayInterstitial() {
        if (interstitial.isLoaded()) {
            interstitial.show();
        }
    }
}

The imports are OK and the Google Play Services library is of course imported.

I use this example: AdMob Android Guides - Interstitial Ad.

Could someone tell me what's wrong in my code?

Bhavesh Odedra
  • 10,990
  • 12
  • 33
  • 58
user2661663
  • 277
  • 1
  • 3
  • 6

7 Answers7

50

You should wait for the ad to be loaded. Only then, you can call displayInterstial() method, which would show the ad.

You can register for a listener, that will let you know when the loading is done.

interstitial.setAdListener(new AdListener(){
          public void onAdLoaded(){
               displayInterstitial();
          }
});
Kumar Bibek
  • 9,016
  • 2
  • 39
  • 68
  • Ok, so I may replace `public void displayInterstitial() { if (interstitial.isLoaded()) { interstitial.show(); } } }` with interstitial.setAdListener(new AdListener(){ public void onAdLoaded(){ displayInterstitial(); } });`` ? Thank you ! – user2661663 Dec 31 '13 at 09:23
  • 25
    Do NOT display the Interstitial as soon as it is loaded unless you want to annoy your users. Wait until an appropriate break in your app and display it then. – William Dec 31 '13 at 22:36
  • 1
    Never trigger for show your Interstitial ad after Ad loaded. Interstitial ads must be triggered only after user interaction like button click or screen close. If you show interstitial ad without user interaction and user clicks it, this is an unwanted click and your application can be banned. – MERT DOĞAN Jan 03 '18 at 17:45
10

this did it for me.

// Begin listening to interstitial & show ads.
interstitial.setAdListener(new AdListener(){
     public void onAdLoaded(){
          interstitial.show();
     }
});

i still wonder why all the guys at google upload code implementing directions that just don't work after one follows them to the dot..

Jared Burrows
  • 54,294
  • 25
  • 151
  • 185
  • 3
    This can lead to AdMob rules violations. Interstitial according Google guidelines should be shown only during a transition from a section to another, so show the ad directly after that is loaded in not considered a good practice. – Silverstorm Mar 08 '16 at 00:05
  • Never trigger for show your Interstitial ad after Ad loaded. Interstitial ads must be triggered only after user interaction like button click or screen close. If you show interstitial ad without user interaction and user clicks it, this is an unwanted click and your application can be banned. – MERT DOĞAN Jan 03 '18 at 17:45
6

I had the same problem. The issue was that admob activity was not defined in manifest. Please make sure you have folllowing activity tag in your manifest file

<activity
        android:name="com.google.android.gms.ads.AdActivity"
        android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
        android:theme="@android:style/Theme.Translucent" />
Muhammad Hamza Shahid
  • 956
  • 1
  • 15
  • 23
3
mInterstitialAd = new InterstitialAd(this);
                mInterstitialAd.setAdUnitId("Your Interstitial Id ca-app-pub-46563563567356235/3452455");
                AdRequest adRequest1 = new AdRequest.Builder()
                        .build();
                mInterstitialAd.loadAd(adRequest1);
                mInterstitialAd.setAdListener(new com.google.android.gms.ads.AdListener() {
                    @Override
                    public void onAdLoaded() {
                        mInterstitialAd.show();
                        super.onAdLoaded();

                    }
                });
1

Try initializing it in onCreate, but only call the show() method from an event, such as a button click.

You can use this code, the test ids work:

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

        MobileAds.initialize(this, "ca-app-pub-3940256099942544~3347511713"); //test id

        mInterstitialAd = new InterstitialAd(this);
        mInterstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712"); //test ad

        mInterstitialAd.loadAd(new AdRequest.Builder().build());


        btn_test.setOnClickListener(view -> {
            showInterstitial();
        });
...
    }    
    private void showInterstitial()
    {
        if (mInterstitialAd.isLoaded()) {
            mInterstitialAd.show();
        } else {
            Log.d("TAG", "The interstitial wasn't loaded yet.");
        }
    }
live-love
  • 48,840
  • 22
  • 240
  • 204
0

in your AndroidManifest you must put the tags

    <activity
        android:name="com.google.android.gms.ads.AdActivity"
        android:theme="@android:style/Theme.Translucent"
        tools:replace="android:theme" />
    <meta-data
        android:name="com.google.android.gms.ads.APPLICATION_ID"
        android:value="APP_ID" />
    <meta-data
        android:name="com.google.android.gms.ads.AD_MANAGER_APP"
        android:value="true" />
    <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />

Ad Manager

Ad Mob

AllanRibas
  • 678
  • 5
  • 14
-1

You didn't call displayInsterstitial().

But what you should do is call the listener for the interstitial :

interstitial.setAdListener(this);

It will then implements the Listener to your Activity and then display it onAdLoaded (or something).

Tsunaze
  • 3,204
  • 7
  • 44
  • 81