On a button click i am showing admob interstitial adds.
For the first time it only shows the add, Again clicking on button it doesn't shows the adds.
Currently i am referring to use this google admob example link https://developers.google.com/admob/android/interstitial
Using SDK version 20
Here is the code below:
MobileAds.initialize(this, new OnInitializationCompleteListener() {
@Override
public void onInitializationComplete(InitializationStatus initializationStatus) {}
});
AdRequest adRequest = new AdRequest.Builder().build();
InterstitialAd.load(this,"ca-app-pub-3940256099942544/1033173712", adRequest, new InterstitialAdLoadCallback() {
@Override
public void onAdLoaded(@NonNull InterstitialAd interstitialAd) {
// The mInterstitialAd reference will be null until
// an ad is loaded.
mInterstitialAd = interstitialAd;
mInterstitialAd.setFullScreenContentCallback(new FullScreenContentCallback(){
@Override
public void onAdDismissedFullScreenContent() {
// Called when fullscreen content is dismissed.
Toast.makeText(getApplicationContext(), " The ad was dismissed.\n",
Toast.LENGTH_LONG).show();
}
@Override
public void onAdFailedToShowFullScreenContent(AdError adError) {
// Called when fullscreen content failed to show.
Log.d("TAG", "The ad failed to show.");
Toast.makeText(getApplicationContext(), " The ad failed to show.\n",
Toast.LENGTH_LONG).show();
}
@Override
public void onAdShowedFullScreenContent() {
// Called when fullscreen content is shown.
// Make sure to set your reference to null so you don't
// show it a second time.
mInterstitialAd = null;
Toast.makeText(getApplicationContext(), " The ad was shown\n",
Toast.LENGTH_LONG).show();
}
});
}
@Override
public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
super.onAdFailedToLoad(loadAdError);
// Handle the error
Toast.makeText(getApplicationContext(),loadAdError +" custom interstitial ad wasn't ready yet\n",
Toast.LENGTH_LONG).show();
Log.i(TAG, loadAdError.getMessage());
mInterstitialAd = null;
}
});
showAd = findViewById(R.id.showAd);
showAd.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
if (mInterstitialAd != null) {
mInterstitialAd.show(MainActivity.this);
} else {
Toast.makeText(getApplicationContext(), " The interstitial ad wasn't ready yet\n", Toast.LENGTH_LONG).show();
}
}
});