Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Variable Declaration
- private InterstitialAd interstitialAd;
- int counter = 1;
- ========================================================================================
- //Prepare for load Interstitial Ad on Fragment
- //call the function loadInterstitialAd() in the onCreate method
- private void loadInterstitialAd() {
- interstitialAd = new InterstitialAd(getActivity());
- interstitialAd.setAdUnitId(getResources().getString(R.string.admob_interstitial_unit_id));
- interstitialAd.loadAd(new AdRequest.Builder().build());
- interstitialAd.setAdListener(new AdListener() {
- @Override
- public void onAdClosed() {
- interstitialAd.loadAd(new AdRequest.Builder().build());
- }
- });
- }
- ========================================================================================
- //Display Interstitial Ad
- //call the function showInterstitialAd() in the onItemClickListener or other method
- private void showInterstitialAd() {
- if (interstitialAd != null && interstitialAd.isLoaded()) {
- if (counter == 3) {
- interstitialAd.show();
- counter = 1;
- } else {
- counter++;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement