Advertisement
solodroid

AdMob Interstitial Ad Implementation

Dec 8th, 2017
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.58 KB | None | 0 0
  1. //Variable Declaration
  2. private InterstitialAd interstitialAd;
  3.  
  4. //Prepare for load Interstitial Ad on Fragment
  5. //call the function loadInterstitialAd() in the onCreate method
  6. private void loadInterstitialAd() {
  7.         interstitialAd = new InterstitialAd(getActivity());
  8.         interstitialAd.setAdUnitId(getResources().getString(R.string.admob_interstitial_id));
  9.         interstitialAd.loadAd(new AdRequest.Builder().build());
  10.         interstitialAd.setAdListener(new AdListener() {
  11.             @Override
  12.             public void onAdClosed() {
  13.                 //your method here when the ad is closed
  14.                 interstitialAd.loadAd(new AdRequest.Builder().build());
  15.             }
  16.         });
  17.     }
  18.  
  19. //Prepare for load Interstitial Ad on Activity
  20. //call the function loadInterstitialAd() in the onCreate method
  21. private void loadInterstitialAd() {
  22.         interstitialAd = new InterstitialAd(getApplicationContext());
  23.         interstitialAd.setAdUnitId(getResources().getString(R.string.admob_interstitial_id));
  24.         interstitialAd.loadAd(new AdRequest.Builder().build());
  25.         interstitialAd.setAdListener(new AdListener() {
  26.             @Override
  27.             public void onAdClosed() {
  28.                 //your method here when the ad is closed
  29.                 interstitialAd.loadAd(new AdRequest.Builder().build());
  30.             }
  31.         });
  32.     }
  33.  
  34. //Display Interstitial Ad
  35. //call the function showInterstitialAd() in the onItemClickListener or other method
  36. private void showInterstitialAd() {
  37.         if (interstitialAd.isLoaded()) {
  38.             interstitialAd.show();
  39.         }
  40.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement