-1

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();
        }
    }
});
Rishi
  • 75
  • 10

2 Answers2

2

@NadeemAslam is correct. As a clear alternative, this is how I always implement it too using a method that I call within

    protected void loadInterstitialAd() { //your method can be private or protected
    if (interstitialAd == null) {
        InterstitialAd.load(this, "ca-app-pub-3940256099942544/1033173712", new AdRequest.Builder().build(), new InterstitialAdLoadCallback() {
            @Override
            public void onAdLoaded(@NonNull InterstitialAd newIntad) {
                interstitialAd = newIntad; //interstitialAd is static variable
                interstitialAd.setFullScreenContentCallback(new FullScreenContentCallback() {
                    @Override
                    public void onAdDismissedFullScreenContent() {
                        interstitialAd = null;
                        loadInterstitialAd(); //METHOD CALL WITHIN ITSELF
                    }
                });
            }

            @Override
            public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
                interstitialAd = null;
            }
        });
    }
}
Tonui Nicholus
  • 395
  • 2
  • 8
1

Interstitial ads will be shown only once after the app start, as you need to load the ad again to show it again. Once you start the app ad will be loaded, you need to implement the callback to it. Once your ad is showed, you need to load the ad again before showing it. You can read the official documentation for better understanding. Interstitial ads documentation

Update: @Rishi You need to load ad again on ad dismissed

@Override         
 public void onAdDismissedFullScreenContent() {
                    loadInterstitialAd(activity);
                }

Here is the code to load the ad

  public void loadInterstitialAd(Context context){
        AdRequest adRequest = new AdRequest.Builder().build();
            InterstitialAd.load(context,your ad id, adRequest,interstitialCallback);
    }

    InterstitialAdLoadCallback interstitialCallback = new InterstitialAdLoadCallback(){
        @Override
        public void onAdLoaded(@NonNull InterstitialAd interstitialAd) {
            mInterstitialAd = interstitialAd;
        }

        @Override
        public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
            mInterstitialAd = null;
        }
    };
Nadeem Aslam
  • 123
  • 1
  • 7
  • Glad to know that thanks, A working code solution would be mostly appreciated. Based on your comment i did more research and tried different solution from YouTube [https://www.youtube.com/watch?v=frC-RZAe4B8&t=7s&ab_channel=PRABEESHRK] but i am getting same results – Rishi Oct 04 '21 at 14:46
  • @Rishi please check the updated answer and mark as accepted if it works for you – Nadeem Aslam Oct 06 '21 at 06:12
  • Thanks, its works, answer is accepted. – Rishi Oct 06 '21 at 11:19