Search code examples
androidbuttonadmobadsinterstitial

Show interstitial ads when clicking on any of four buttons


I have a layout with four buttons. When the user clicks on one of the buttons, I want to show interstitial ads .

My code below works when I click on one of the four buttons, but when I click again, the interstitial ads don't show.

For example:

BUTTON1BUTTON2BUTTON3BUTTON4

When I click for the first time on BUTTON1 the interstitial ads show; when I want to click again on one of the buttons (BUTTON2, for example), the interstitial ads are not showing at all.

How can I get the interstitial ads to show each time I click the button?

InterstitialAd mInterstitialAd;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity);
    mInterstitialAd = new InterstitialAd(this);
    mInterstitialAd.setAdUnitId(getString(R.string.interstitialAd));
    mInterstitialAd.loadAd(adRequest);
     btn1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            mInterstitialAd.setAdListener(new AdListener() {
                public void onAdLoaded() {
                    if (mInterstitialAd.isLoaded()) {
                        mInterstitialAd.show();
                    }
                }
            });
         //button 1 function
        }
    });
btn2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            mInterstitialAd.setAdListener(new AdListener() {
                public void onAdLoaded() {
                    if (mInterstitialAd.isLoaded()) {
                        mInterstitialAd.show();
                    }
                }
            });
         //button 2 function
        }
    });
btn3.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            mInterstitialAd.setAdListener(new AdListener() {
                public void onAdLoaded() {
                    if (mInterstitialAd.isLoaded()) {
                        mInterstitialAd.show();
                    }
                }
            });
         //button 3 function
        }
    });
btn4.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            mInterstitialAd.setAdListener(new AdListener() {
                public void onAdLoaded() {
                    if (mInterstitialAd.isLoaded()) {
                        mInterstitialAd.show();
                    }
                }
            });
         //button 4 function
        }
    });

Solution

  • After displaying an InterstitialAd, you should request a new one with a new AdRequest:

    So, one option is add following lines to your every ClickListener (not discussing how to improve the quality... just how to fix it):

    if (mInterstitialAd.isLoaded()) {
        mInterstitialAd.show();
        mInterstitialAd.loadRequest(new AdRequest.Builder().build());
    }
    

    Could you please test and share the results (you may need to wait until a new AD is loaded before clicking on next button).