1

I am trying to add banner ads in my game. I am able to display the ads on load but the problem is when I am making the Adview object invisible its not loading in background. Ads are only loading when the Visibility of the Adview Object is set to Invisible but for my game I have to show an ad on exit screen.

Thanks in advance

Code:

public void initAds()
    {
        rect_layout = (FrameLayout) findViewById(R.id.rectangleView);
        banner_layout  = (LinearLayout) findViewById(R.id.bannerView);

        banner_ad = new AdView(_activity);
        banner_ad.setAdUnitId(BANNER_AD_ID);
        banner_ad.setAdSize(AdSize.SMART_BANNER);
        AdRequest adRequestBanner = new AdRequest.Builder().build();
        banner_ad.loadAd(adRequestBanner);
        banner_layout.addView(banner_ad);

        rect_ad =  new AdView(_activity);
        rect_ad.setAdUnitId(RECTANGLE_AD_ID);
        rect_ad.setAdSize(AdSize.MEDIUM_RECTANGLE);
        AdRequest adRequestRectangle = new AdRequest.Builder().build();
        rect_ad.loadAd(adRequestRectangle);
        rect_layout.addView(rect_ad);

        rect_ad.setAdListener(new AdListener() {
            @Override
            public void onAdClosed() {
                super.onAdClosed();
            }

            @Override
            public void onAdFailedToLoad(int errorCode) {
                super.onAdFailedToLoad(errorCode);
            }

            @Override
            public void onAdLeftApplication() {
                super.onAdLeftApplication();
            }

            @Override
            public void onAdOpened() {
                super.onAdOpened();
            }

            @Override
            public void onAdLoaded() {
                super.onAdLoaded();
                //rect_layout.setVisibility(View.INVISIBLE);
            }
        });
        //rect_layout.setAlpha(0);

    }
//Rectangular ad
    public static void showRectangularAd(final String _show)
    {

        _activity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if(Boolean.parseBoolean(_show))
                {
                    _activity.rect_layout.setVisibility(View.VISIBLE);
                    //_activity.rect_layout.setAlpha(1);
                    Log.d(TAG, "Set to visible");
                }

                else
                {
                    _activity.rect_layout.setVisibility(View.INVISIBLE);
                    //_activity.rect_layout.setAlpha(0);
                    Log.d(TAG, "Set to invisible");
                }
            }
        });
    }
Jai Sharma
  • 11
  • 3

1 Answers1

1

First of all you cannot load an adview when in its invisible state. Actually google keep track of your adview visibility and load ads to the adview only an adview is visible.

So you have to keep the adview visible so that it will load.

you can do something like this. Force the user to wait until the ad load complete. (Show the exit button only if the adload complete when it become visible).

HourGlass
  • 1,805
  • 1
  • 15
  • 29