Reputation: 61
How do you people use admob banner? Can you please take a look at my code and show what is wrong with it. I have a game that has a Pause screen and it has much room to place a banner.
However when I load it, banner takes too long to show up in slow connection. So i made to load it up but keep it INVISIBLE, then make it visible when I need it. But it's not work. The banner won't visible! Please give me any advice... Here is the code:
//Admob request Banner
bannerAdmob = new AdView(this);
bannerAdmob.setAdSize(AdSize.WIDE_SKYSCRAPER);
bannerAdmob.setAdUnitId(Setting.admobBannerId);
requestAdmobBanner();
I use handler to choose adunit. Interstitial is work nice, but neither banner.
protected Handler handlerAdmob = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case 1: //If is interstitial
if (interstitialAdmob.isLoaded()) {
interstitialAdmob.show();
} else {
requestAdmobInterstitial();
}
break;
case 2: //If is banner
bannerAdmob.setVisibility(View.VISIBLE);
break;
case 6: //Hide banner
bannerAdmob.destroy();
requestAdmobBanner(); //Request new banner
}
}
};
Then the method to load banner
private void requestAdmobBanner() {
AdRequest bannerRequest = new AdRequest.Builder()
// Add a test device to show Test Ads
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.addTestDevice(Setting.Device_ID)
.build();
// Load the banner ad.
bannerAdmob.loadAd(bannerRequest);
// Now we add ads listener for Admob banner so we can SHOW and HIDE it
bannerAdmob.setAdListener(new AdListener() {
@Override
public void onAdFailedToLoad(int errorCode) { // On admob interstitial failed to load, request new ad
bannerAdmob.setVisibility(View.GONE);
}
@Override
public void onAdLoaded() {
bannerAdmob.setVisibility(View.INVISIBLE);
System.out.println("Banner Admob is load, but still INVISIBLE");
}
});
}
Upvotes: 1
Views: 955
Reputation: 20196
You are setting the AdView to be INVISIBLE, when you should be setting it to VISIBLE. See code below:
// Now we add ads listener for Admob banner so we can SHOW and HIDE it
bannerAdmob.setAdListener(new AdListener() {
@Override
public void onAdFailedToLoad(int errorCode) { // On admob interstitial failed to load, request new ad
bannerAdmob.setVisibility(View.GONE);
}
@Override
public void onAdLoaded() {
bannerAdmob.setVisibility(View.VISIBLE);
System.out.println("#onAdLoaded - making AdView visible");
}
});
Upvotes: 1