Search code examples
androidwebviewonclickadmobinterstitial

How to show interstitial ad in webview after some interval or 3-4 click?


I have a wordpress website and of which I have a webview app and I have implemented interstitial ad on any button click, so whenever a link is clicked full page ads gets shown, How do I decrease it's frequency, I want it to come well let's say after 3 or 4 click?

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            // The webView is about to navigate to the specified url.
            // Toast.makeText(MainActivity.this, "Login clicked", Toast.LENGTH_LONG).show();
            if (interstitialAd.isLoaded()) {
                interstitialAd.show();
                interstitialAd.setAdListener(new AdListener() {
                    @Override
                    public void onAdClosed() {
                        AdRequest adRequest = new AdRequest.Builder()
                                .build();
                        interstitialAd.loadAd(adRequest);
                    }
                });
            }
            return super.shouldOverrideUrlLoading(view, url);
        }

Solution

  • Final solution to my problem, any suggestion most welcome...

    int n = 1;
                @Override
                public boolean shouldOverrideUrlLoading(WebView view, String url) {
                    // The webView is about to navigate to the specified url.
    
                    if (interstitialAd.isLoaded()) {
                        if( n%3 == 0 )
                        {
                            interstitialAd.show();
    
               } n++;
                    }
                    interstitialAd.setAdListener(new AdListener() {
                        @Override
                        public void onAdClosed() {
                            AdRequest adRequest = new AdRequest.Builder()
                                    .build();
                            interstitialAd.loadAd(adRequest);
                        }
                    });
                    return super.shouldOverrideUrlLoading(view, url);
                }