Google Mobile Ads SDK

Banners II

In this guide, we show you more ways to control banner ad properties.

Android (Google Play)
  1. Banner Sizes
  2. Ad Refresh
  3. com.google.android.gms.ads.AdRequest
    1. AdRequest.Builder.addTestDevice
    2. Targeting
  4. com.google.android.gms.ads.AdListener

Banner Sizes

Google Mobile Ads supports the following ad formats:

Size (WxH) Description Availability AdSize Constant
320x50 Standard Banner Phones and Tablets BANNER
300x250 IAB Medium Rectangle Phones and Tablets MEDIUM_RECTANGLE
468x60 IAB Full-Size Banner Tablets FULL_BANNER
728x90 IAB Leaderboard Tablets LEADERBOARD
See table Smart Banner Phones and Tablets SMART_BANNER

The SDK will request whatever size the requesting AdView was instantiated with. If there isn't enough space on the device's screen to display the ad, nothing will be shown.

Ad Refresh

Banners auto-refresh if a refresh rate has been specified in your AdMob account on the server and may be programmatically refreshed by loading a new request.

com.google.android.gms.ads.AdRequest

Before being passed to AdView.loadAd, an AdRequest may be customized using an AdRequest.Builder to allow Google to better target ads.

AdRequest.Builder.addTestDevice

You can use this property to specify a device that will receive test ads. You should utilize this property during development to avoid generating false impressions. To verify that you've integrated the SDK correctly, add your test device, run your application, and click on the displayed test ad.

AdRequest request = new AdRequest.Builder()
    .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)        // All emulators
    .addTestDevice("AC98C820A50B4AD8A2106EDE96FB87D4")  // My Galaxy Nexus test phone
    .build();

logcat will print the device's MD5-hashed ID for convenience, for example, to get test ads on this device, call builder.addTestDevice("AC98C820A50B4AD8A2106EDE96FB87D4");

Targeting

Demographic and location targeting information may also be specified in the AdRequest. Out of respect for user privacy, Google asks that you only specify location and demographic data if that information is already used by your app.

AdRequest request = new AdRequest.Builder()
    .setGender(AdRequest.GENDER_FEMALE)
    .setBirthday(new GregorianCalendar(1985, 1, 1).getTime())
    .setLocation(location)
    .build();

where the user's location is obtained by a suitable method.

com.google.android.gms.ads.AdListener

You may optionally track ad lifecycle events like request failures or "click-through" by passing an object to AdView.setAdListener that extends com.google.android.gms.ads.AdListener.

public abstract class AdListener {
  public void onAdLoaded();
  public void onAdFailedToLoad(int errorCode);
  public void onAdOpened();
  public void onAdClosed();
  public void onAdLeftApplication();
}

A separate class extending AdListener may be defined, or you may choose to inline an AdListener object:

adView.setAdListener(new AdListener() {
  @Override
  public void onAdOpened() {
    // Save app state before going to the ad overlay.
  }
});

AdListener provides a default empty implementation for all of its ad lifecycle events. You only need to override the ad events you wish to implement.

public void onAdLoaded()
Called when an ad is received.
public void onAdFailedToLoad(int errorCode)
Called when an ad request failed. The error code is usually one of the following:
  • AdRequest.ERROR_CODE_INTERNAL_ERROR
  • AdRequest.ERROR_CODE_INVALID_REQUEST
  • AdRequest.ERROR_CODE_NETWORK_ERROR
  • AdRequest.ERROR_CODE_NO_FILL
public void onAdOpened()
Called when an ad opens an overlay that covers the screen.
public void onAdClosed()
Called when the user is about to return to the application after clicking on an ad.
public void onAdLeftApplication()
Called when an ad leaves the application (e.g., to go to the browser).

You can download an example project containing a sample AdListener here.

Proceed to the next guide to learn about interstitial ads.

Android (6.4.1 and earlier SDKs)
  1. Banner Sizes
  2. Ad Refresh
  3. com.google.ads.AdRequest
    1. AdRequest.addTestDevice | AdRequest.setTestDevices
    2. Targeting
  4. com.google.ads.AdListener

Banner Sizes

Google Mobile Ads supports three tablet-only banner sizes in addition to the 320x50 shown on phones:

Size (WxH) Description Availability AdSize Constant
320x50 Standard Banner Phones and Tablets BANNER
300x250 IAB Medium Rectangle Phones and Tablets IAB_MRECT
468x60 IAB Full-Size Banner Tablets IAB_BANNER
728x90 IAB Leaderboard Tablets IAB_LEADERBOARD
See table Smart Banner Phones and Tablets SMART_BANNER

The SDK will request whatever size the requesting AdView was instantiated with. If there isn't enough space on the device's screen to display the ad, nothing will be shown.

Ad Refresh

Banners auto-refresh if a refresh rate has been specified in your AdMob account on the server and may be programmatically refreshed by loading a new request.

com.google.ads.AdRequest

Before being passed to AdView.loadAd an AdRequest may be customized to allow Google to better target ads.

AdRequest.addTestDevice | AdRequest.setTestDevices

You can use these properties to specify a device or Set of devices that will receive test ads. You should utilize this property during development to avoid generating false impressions. To verify that you've integrated the SDK correctly, add your test device, run your application, and click on the displayed test ad.

AdRequest request = new AdRequest();

request.addTestDevice(AdRequest.TEST_EMULATOR);
request.addTestDevice("E83D20734F72FB3108F104ABC0FFC738");    // My T-Mobile G1 test phone

logcat will print the device's MD5-hashed ID for convenience, for example:
   To get test ads on this device, call adRequest.addTestDevice("E83D20734F72FB3108F104ABC0FFC738");

Targeting

Location and demographic targeting information may also be specified. Out of respect for user privacy, Google asks that you only specify location and demographic data if that information is already used by your app.

AdRequest request = new AdRequest();

request.setGender(AdRequest.Gender.FEMALE);
request.setLocation(location);
request.setBirthday("19850101");

where the user's location is obtained by a suitable method.

com.google.ads.AdListener

You may optionally track ad lifecycle events like request failures or "click-through" by implementing com.google.ads.AdListener in an object you pass to AdView.setAdListener.

public interface AdListener {
  public void onReceiveAd(Ad ad);
  public void onFailedToReceiveAd(Ad ad, AdRequest.ErrorCode error);
  public void onPresentScreen(Ad ad);
  public void onDismissScreen(Ad ad);
  public void onLeaveApplication(Ad ad);
}

This interface may be implemented by your activity or any other object:

import com.google.ads.*;

public class BannerExample extends Activity implements AdListener {
}

and then passed to the Ad:

adView.setAdListener(this);
public void onReceiveAd(Ad ad)
Sent when AdView.loadAd has succeeded.
public void onFailedToReceiveAd(Ad ad, AdRequest.ErrorCode error)
Sent when loadAd has failed, typically because of network failure, an application configuration error, or a lack of ad inventory. You may wish to log these events for debugging:
@Override
public void onFailedToReceiveAd(Ad ad, AdRequest.ErrorCode errorCode) {
  Log.d(MY_LOG_TAG, "failed to receive ad (" + errorCode + ")");
}
public void onPresentScreen(Ad ad)
Called when an Activity is created in front of your app, presenting the user with a full-screen ad UI in response to their touching ad.
public void onDismissScreen(Ad ad)
Called when the full-screen Activity presented with onPresentScreen has been dismissed and control is returning to your app.
public void onLeaveApplication(Ad ad)
Called when an Ad touch will launch a new application.

You can download an example project containing a sample AdListener here.

Proceed to the next guide to learn about interstitial ads.

iOS
  1. Banner Sizes
  2. Ad Refresh
  3. GADRequest
    1. Testing
    2. Targeting
  4. GADBannerViewDelegate

Banner Sizes

Google Mobile Ads supports three tablet-only banner sizes on iPad in addition to the iPod Touch and iPhone's 320x50:

Size (WxH) Description Availability AdSize Constant*
320x50 Standard Banner Phones and Tablets kGADAdSizeBanner
300x250 IAB Medium Rectangle Phones and Tablets kGADAdSizeMediumRectangle
468x60 IAB Full-Size Banner Tablets kGADAdSizeFullBanner
728x90 IAB Leaderboard Tablets kGADAdSizeLeaderboard
See table Smart Banner Phones and Tablets kGADAdSizeSmartBannerPortrait
kGADAdSizeSmartBannerLandscape

*Legacy AdSize Constant values are GAD_SIZE_320x50, GAD_SIZE_300x250, GAD_SIZE_468x60, and GAD_SIZE_728x90.

The SDK will request the largest of these sizes that fits the requesting GADBannerView. No ads will be returned if your view size is too small.

Ad Refresh

Banners auto-refresh if a refresh rate has been specified in your AdMob account on the server and may be programmatically refreshed by loading a new request.

Note: The current refresh rate range is 30-120 seconds.

GADRequest

Before being passed to GADBannerView's loadRequest a GADRequest may be customized to allow Google to better target ads.

Testing

Use testDevices to enable test ads. You should utilize test ads during development to avoid generating false impressions. Here is a sample snippet:

GADRequest *request = [GADRequest request];

// Make the request for a test ad. Put in an identifier for
// the simulator as well as any devices you want to receive test ads.
request.testDevices = [NSArray arrayWithObjects:
                           @"MY_SIMULATOR_IDENTIFIER",
                           @"MY_DEVICE_IDENTIFIER",
                           nil];

You can find identifier IDs from console logs.

Targeting

Location and demographic targeting information may also be specified. Out of respect for user privacy Google asks that you only specify location and demographic data if that information is already used by your app.

GADRequest *request = [GADRequest request];

request.gender = kGADGenderMale;

[request setLocationWithLatitude:locationManager_.location.coordinate.latitude
                       longitude:locationManager_.location.coordinate.longitude
                        accuracy:locationManager_.location.horizontalAccuracy];

[request setBirthdayWithMonth:3 day:13 year:1976];

[bannerView_ loadRequest:request];

If you do not wish to use CoreLocation, location may alternately be specified in more general terms:

[request setLocationWithDescription:@"94070 US"];

Note: setLocationWithDescription: and setLocationWithLatitude:longitude:accuracy: are mutually exclusive.

GADBannerViewDelegate

You may optionally track ad lifecycle events like request failures or "click-through" by implementing all or part of GADBannerViewDelegate.

@protocol GADBannerViewDelegate <NSObject>
  @optional
  - (void)adViewDidReceiveAd:(GADBannerView *)bannerView;
  - (void)adView:(GADBannerView *)bannerView
      didFailToReceiveAdWithError:(GADRequestError *)error;

  - (void)adViewWillPresentScreen:(GADBannerView *)bannerView;
  - (void)adViewDidDismissScreen:(GADBannerView *)bannerView;
  - (void)adViewWillDismissScreen:(GADBannerView *)bannerView;
  - (void)adViewWillLeaveApplication:(GADBannerView *)bannerView;
@end

These methods may be implemented in either a separate object like a view controller:

#import "GADBannerViewDelegate.h"

@interface NibExampleViewController : UIViewController <GADBannerViewDelegate> {
}

@end

or as part of a GADBannerView subclass:

#import "GADBannerView.h"
#import "GADBannerViewDelegate.h"

@interface MyBannerView : GADBannerView <GADBannerViewDelegate> {
}

@end

Remember to set the delegate using setDelegate: before making the request for an ad:

[bannerView_ setDelegate:self];
- (void)adViewDidReceiveAd:(GADBannerView *)bannerView
Sent when loadRequest: has succeeded, this is a good opportunity to add the sender to the view hierarchy if it’s been hidden until now. To have a banner slide up on screen once loaded from your view controller, for example:
- (void)adViewDidReceiveAd:(GADBannerView *)bannerView {
  [UIView beginAnimations:@"BannerSlide" context:nil];
  bannerView.frame = CGRectMake(0.0,
                                self.view.frame.size.height -
                                bannerView.frame.size.height,
                                bannerView.frame.size.width,
                                bannerView.frame.size.height);
  [UIView commitAnimations];
}
- (void)adView:(GADBannerView *)view didFailToReceiveAdWithError:(GADRequestError *)error
Sent when loadRequest: has failed, typically because of network failure, an application configuration error, or a lack of ad inventory. You may wish to log these events for debugging:
- (void)adView:(GADBannerView *)bannerView
    didFailToReceiveAdWithError:(GADRequestError *)error {
  NSLog(@”adView:didFailToReceiveAdWithError:%@”, [error localizedDescription]);
}
- (void)adViewWillPresentScreen:(GADBannerView *)bannerView
Sent immediately before the user is presented with a full-screen ad UI in response to their touching the sender. At this point you should pause any animations, timers or other activities that assume user interaction and save app state, much like on UIApplicationDidEnterBackgroundNotification.

Typically the user simply browses the full-screen ad and dismisses it, generating adViewDidDismissScreen: and returning control to your app. If the banner’s action was either Click-to-App-Store or Click-to-iTunes or the user presses Home within the ad, however, your app will be backgrounded and potentially terminated.

In these cases under iOS 4.0+ the next method invoked will be your root view controller’s applicationWillResignActive: followed by adViewWillLeaveApplication:

- (void)adViewDidDismissScreen:(GADBannerView *)bannerView
Sent when the user has exited the sender’s full-screen UI
- (void)adViewWillDismissScreen:(GADBannerView *)bannerView
Sent immediately before the sender’s full-screen UI is dismissed, restoring your app and the root view controller. At this point you should restart any foreground activities paused as part of adViewWillPresentScreen:.
- (void)adViewWillLeaveApplication:(GADBannerView *)bannerView
Sent just before the application gets backgrounded or terminated as a result of the user touching a Click-to-App-Store or Click-to-iTunes banner. The normal UIApplicationDelegate notifications like applicationDidEnterBackground: arrive immediately before this.

During these methods you may consult GADBannerView.hasAutoRefreshed to determine if a refresh triggered the event.

Note that if you implement your delegate as a distinct object rather than a GADBannerView subclass you should be sure to set the GADBannerView's delegate property to nil before releasing the view.

- (void)dealloc {
  bannerView_.delegate = nil;

  // Don't release the bannerView_ if you are using ARC in your project
  [bannerView_ release];
  [super dealloc];
}

Objective-C delegates are not retained and may be messaged asynchronously before the delegating object is finally deallocated.

You can download an example project containing this code here.

Proceed to the next guide to learn about interstitial ads.

Windows Phone 8
  1. Ad properties
    1. Banner sizes
    2. Ad refresh
    3. Test ads
    4. Targeting
  2. AdView events
    1. Registering for AdView events

Ad properties

The ads that an AdView requests can be customized in a few ways.

Banner sizes

Google Mobile Ads supports the following sizes for Windows phones:

Size (WxH) Description Availability Format Constant
320x50 Standard Banner Windows Phone Banner
See table Smart Banner Windows Phone SmartBanner

Ad refresh

Banners auto-refresh if a refresh rate has been specified in your AdMob account on the server and may be programmatically refreshed by loading a new request. The current refresh rate range is 30-120 seconds.

Test ads

Use ForceTesting to enable test ads. You should utilize test ads during development to avoid generating false impressions:

AdRequest adRequest = new AdRequest();
adRequest.ForceTesting = true;

Setting the ForceTesting flag to true will enable test ads on all emulator requests.

Targeting

Location and demographic targeting information may also be specified. Out of respect for user privacy, Google asks that you only specify location and demographic data if that information is already being used by your application.

AdRequest adRequest = new AdRequest();
adRequest.Location = geoposition.Coordinate;  // where geoposition is a GeoPosition object.
adRequest.Gender = UserGender.Male;
adRequest.Birthday = DateTimeOffset.Parse("2/15/2012 6:00:00 AM -7:00");

AdView events

You may optionally track ad lifecycle events like request failures or "click-through" by hooking up all or some of the events on AdView to delegates. Do this by composing a delegate onto the necessary fields for the AdView.

Events for GoogleAds.AdView:

public event GoogleAds.AdEventHandler DismissingOverlay(object sender, AdEventArgs e)
Raised after an interstitial/overlay is dismissed.
public event GoogleAds.AdErrorEventHandler FailedToReceiveAd(object sender, AdErrorCode errorCode)
Sent when AdView's LoadAd has failed (typically because of network failure, an application configuration error, or a lack of ad inventory). You may wish to log these events for debugging.
public event GoogleAds.AdEventHandler LeavingApplication(object sender, AdEventArgs e)
Raised when an ad touch will launch a new application.
public event GoogleAds.AdEventHandler ReceivedAd(object sender, AdEventArgs e)
Raised when AdView's LoadAd has succeeded.
public event GoogleAds.AdEventHandler ShowingOverlay(object sender, AdEventArgs e)
Raised before an interstitial/overlay is shown.

Registering for AdView events

Use the += operator to compose a delegate onto the field.

bannerAd.FailedToReceiveAd += OnFailedToReceiveAd;
...
private void OnFailedToReceiveAd(object sender, AdErrorCode errorCode)
{
  System.Diagnostics.Debug.WriteLine("Failed to receive ad with error: " + errorCode.ErrorCode);
}

You can download a sample project that registers for AdView events here.

Proceed to the next guide to learn about interstitial ads.

Authentication required

You need to be signed in with Google+ to do that.

Signing you in...

Google Developers needs your permission to do that.