Search code examples
androidsdkadmobinterstitial

Android admob interstitial adds showing only once after the app starts


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

Solution

  • 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;
            }
        };