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");
}
}
});
}