Advertisement
solodroid

AdMob Interstitial Ad Implementation with counter

Feb 25th, 2019
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.26 KB | None | 0 0
  1.     //Variable Declaration
  2.     private InterstitialAd interstitialAd;
  3.     int counter = 1;
  4.  
  5.     ========================================================================================
  6.  
  7.     //Prepare for load Interstitial Ad on Fragment
  8.     //call the function loadInterstitialAd() in the onCreate method
  9.     private void loadInterstitialAd() {
  10.         interstitialAd = new InterstitialAd(getActivity());
  11.         interstitialAd.setAdUnitId(getResources().getString(R.string.admob_interstitial_unit_id));
  12.         interstitialAd.loadAd(new AdRequest.Builder().build());
  13.         interstitialAd.setAdListener(new AdListener() {
  14.             @Override
  15.             public void onAdClosed() {
  16.                 interstitialAd.loadAd(new AdRequest.Builder().build());
  17.             }
  18.         });
  19.     }
  20.  
  21.     ========================================================================================
  22.  
  23.     //Display Interstitial Ad
  24.     //call the function showInterstitialAd() in the onItemClickListener or other method
  25.     private void showInterstitialAd() {
  26.         if (interstitialAd != null && interstitialAd.isLoaded()) {
  27.             if (counter == 3) {
  28.                 interstitialAd.show();
  29.                 counter = 1;
  30.             } else {
  31.                 counter++;
  32.             }
  33.         }
  34.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement