Search code examples
androidandroid-viewpageradmob

Where to place AdMob interstitial ad in ViewPager


I have published an app in google play having the ad in it, few days ago I got the following email from google Admob.

We are alerting you that your app is currently in violation of the AdMob program policies. Importantly, this will require action on your part to ensure no disruption in ad serving. Please read below for more information on the actions you need to take:

LAYOUT ENCOURAGES ACCIDENTAL CLICKS - INTERSTITIAL ADS: Publishers are not permitted to encourage users to click AdMob interstitial ads in any way. This includes any implementation that may encourage accidental clicks, such as placing an interstitial ad in a way that prevents viewing the app’s core content or placing an interstitial ad in a way that interferes with navigating or interacting with the app’s core content and functionality.

Please review how you’ve implemented interstitial ads and be mindful of the following common examples of non-compliant implementations:

Interstitial ads that appear before the app has opened or after the app has closed. Interstitial ads that are triggered after a user closes another interstitial ad. Interstitial ads loading unexpectedly while a user is viewing the app’s content. Remember to only serve interstitials between pages of content. Interstitial ads that trigger after every user click. Interstitial ads that appear during periods of game play or heavy user interaction.

Now below is my MainActivity. I need help to place the ad in appropriate area

public class MainActivity extends Activity  {
    private InterstitialAd interstitial;    
//  static int p;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ExtendedViewPager mViewPager = (ExtendedViewPager) findViewById(R.id.view_pager);
        mViewPager.setAdapter(new TouchImageAdapter());


        mViewPager.setCurrentItem(5);
     // Prepare the Interstitial Ad

        interstitial = new InterstitialAd(MainActivity.this);
        // Insert the Ad Unit ID
        interstitial.setAdUnitId("xxxxxxxxxxxxxxxxxxxxxxxx");

            //Locate the Banner Ad in activity_main.xml
            AdView adView = (AdView) this.findViewById(R.id.adView);

            // Request for Ads
            AdRequest adRequest = new AdRequest.Builder()

            // Add a test device to show Test Ads
             .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
             .addTestDevice("")
                    .build();

            // Load ads into Banner Ads
            adView.loadAd(adRequest);

            // Load ads into Interstitial Ads
            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();
            }
        }

    class TouchImageAdapter extends PagerAdapter {

        private int[] images = { R.drawable.file_page05,R.drawable.file_page04,R.drawable.file_page03,R.drawable.file_page02,R.drawable.file_page01};

        @Override
        public int getCount() {
            return images.length;
        }

        @Override
        public View instantiateItem(ViewGroup container, int position) {
            TouchImageView img = new TouchImageView(container.getContext());
            img.setImageResource(images[position]);

            container.addView(img, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
            return img;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }



    }


}

Solution

  • Creating ad will take some time. You are displaying the ad after the viewpager is displayed. This will lead to a case where the user is navigating through viewpager and suddenly ad is loaded. This is against the ad policy.

    Before loading the viewpager, you have check whether ad is loaded. If ad is loaded, then display ad and on closing the ad, display viewpager. Else directly display viewpager.

    I will usually load the ad in the launcher activity and display when needed.