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
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.");
}
}
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" />
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..
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).
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(); } });
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