1
votes

I'm new to Android development. I have a Web View app. My current code Admob interstitial loads only once. Can anybody help me how to set Admob Interstitial load after web page load

      AdRequest adRequest = new AdRequest.Builder().build();

            // Prepare the Interstitial Ad
            interstitial = new InterstitialAd(MainActivity.this);
// Insert the Ad Unit ID
            interstitial.setAdUnitId(getString(R.string.admob_interstitial_id));

            interstitial.loadAd(adRequest);
// Prepare an Interstitial Ad Listener
            interstitial.setAdListener(new AdListener() {
                public void onAdLoaded() {
// Call displayInterstitial() function
                    displayInterstitial();
                }

                public void displayInterstitial() {
// If Ads are loaded, show Interstitial else show nothing.
                    if (interstitial.isLoaded()) {
                        interstitial.show();
                    }
                }
            });
2

2 Answers

0
votes

Here is the code

   mWebView.setWebViewClient(new WebViewClient() {

   @Override
   public void onPageFinished(WebView view, String url) {

     //web page loaded , show your interstitial

    }
});
0
votes

I had the same problem. That worked for me (in Flutter):

import 'package:firebase_admob/firebase_admob.dart';

InterstitialAd _interstitialAd;
bool _isInterstitialAdReady;

void prepAd(){
  initState();
  _loadInterstitialAd();
}

void runAd() {
  _interstitialAd.show();
}

void initState() {
  _isInterstitialAdReady = false;
  _interstitialAd = InterstitialAd(
    adUnitId: InterstitialAd.testAdUnitId,
    listener: _onInterstitialAdEvent,
  );
}


void _loadInterstitialAd() {
  _interstitialAd.load();
}
// TODO: Implement _onInterstitialAdEvent()
void _onInterstitialAdEvent(MobileAdEvent event) {
  switch (event) {
    case MobileAdEvent.loaded:
      _isInterstitialAdReady = true;
      break;
    case MobileAdEvent.failedToLoad:
      _isInterstitialAdReady = false;
      print('Failed to load an interstitial ad');
      break;
    case MobileAdEvent.closed:
      dispose();
      prepAd();
      break;
    default:
    // do nothing
  }
}

void dispose() {// TODO: Dispose InterstitialAd object
  _interstitialAd?.dispose();
}

I ran prepAd() once in the beginning and then just runAd() when I need it. You can check _isInterstitialAdReady == true inside runAd() if you want