Hide
DFP Android Guides

Banner Ad Customization

Introduction

In the Quick Start guide, you learned how to deploy the SDK and display a test banner ad. This guide goes into further customizations that are possible with banner ads.

The following ad formats are supported:

Size (WxH) Description Availability AdSize Constant
320x50 Standard Banner Phones and Tablets BANNER
320x100 Large Banner Phones and Tablets LARGE_BANNER
300x250 IAB Medium Rectangle Phones and Tablets MEDIUM_RECTANGLE
468x60 IAB Full-Size Banner Tablets FULL_BANNER
728x90 IAB Leaderboard Tablets LEADERBOARD
Screen width x 32|50|90 Smart Banner Phones and Tablets SMART_BANNER

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

Back to top

Smart Banners

Smart Banners allow for the rendering of full width ads across different devices and orientations by "smartly" sizing ad units according to screen size. Three ad heights (in dp, density-independent pixel) are available:

  • 32 - phones in landscape
  • 50 - phones in portrait
  • 90 - tablets in either orientation

More specifically, if the screen height of a device is between 400 and 720, an ad height of 50 is used in both orientations; for screen heights greater than 720, a 90 ad height is used.

When an image ad won't take up the entire allotted space for the banner, we'll center the image and use a hexagonal textile filler (see image) to fill up the remaining space. Note that AdSense backfill ads will be centered and have "transparent" filler.

To use Smart Banners, specify the constant SMART_BANNER for the ad size:

PublisherAdView adView = new PublisherAdView(this);
adView.setAdSize(AdSize.SMART_BANNER);

Back to top

Custom ad size

In addition to the standard AdMob ad units, DFP allows you to serve any sized ad unit into an application. Note that the ad size (width, height) defined for an ad request should match the dimensions of the ad view displayed on the application, i.e. PublisherAdView.

Example:

// Define custom AdSize of 250x250 for PublisherAdView

AdSize customAdSize = new AdSize(250, 250);
PublisherAdView adView = new PublisherAdView(this);
adView.setAdSizes(customAdSize);

Back to top

Multiple ad sizes

DFP allows you to specify multiple ad sizes which may be eligible to serve into a PublisherAdView. Before implementing this feature in the SDK, create a line item targeting the same ad unit which is associated with different size creatives.

In your application, simply pass multiple AdSize parameters into setAdSizes:

PublisherAdView adView = new PublisherAdView(this);
adView.setAdSizes(AdSize.BANNER, new AdSize(120, 20), new AdSize(250, 250));

If PublisherAdView changes size at refresh time, your layout should be able to automatically adapt to the new size.

If you need to change your supported ad sizes at any point in your application, simply call setAdSizes with the new list of sizes.

// Drop support for 120x20 ad size.
adView.setAdSizes(new AdSize(250, 250), AdSize.BANNER);

The PublisherAdView will default to the size passed in the first parameter until the next ad returns.

Back to top

Custom targeting

You can pass custom key-value pairs to target DFP campaigns (line items). 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.

// Example: Pass custom targeting "Age=25", "Color=Blue,Green,Red"
private PublisherAdRequest createRequest() {
  Bundle bundle = new Bundle();
  bundle.putString("Age", "25");
  bundle.putString("Color", "Blue,Green,Red");
  return  new PublisherAdRequest.Builder()
      .addNetworkExtras(new AdMobExtras(bundle))
      .build();
}

Back to top

Location targeting

DFP can use precise location to improve geotargeting. The location is set using setLocation() method.

Here's an example of how to set location before loading the ad request:

Location location = new Location("");
// New York
location.setLatitude(40.764316);
location.setLongitude(-73.977245);
location.setAccuracy(100);
PublisherAdRequest request = new PublisherAdRequest.Builder()
    .setLocation(location)
    .build();
adView.loadAd(request);

Back to top

Publisher provided identifiers

You can set a publisher provided identifier (PPID) for use in frequency capping, audience segmentation and targeting, sequential ad rotation, and other audience-based ad delivery controls across devices.

Here's an example of setting the PPID:

PublisherAdRequest request = new PublisherAdRequest.Builder()
    .setPublisherProvidedId("AB123456789")
    .build();

Back to top

Content URL

To provide a URL for content related to your app, you can call PublisherAdRequest.Builder.setContentUrl when building a PublisherAdRequest:

PublisherAdRequest requestWithContent = new PublisherAdRequest.Builder()
    .setContentUrl("http://googleadsdeveloper.blogspot.com/2014/03/monetizing-unity-mobile-apps-just-got.html")
    .build();

Back to top

Manual impression counting

You can manually send impression pings to DFP if you have special conditions for when an impression should be recorded. This can be done by first enabling a PublisherAdRequest for manual impressions prior to loading an ad:

PublisherAdRequest request = new PublisherAdRequest.Builder()
    .setManualImpressionsEnabled(true)
    .build();

When you determine that an ad has been successfully returned and is on screen, you can manually record an impression:

adView.recordManualImpression();

Back to top

App events

App events allow you to create ads that can send messages to their application code. The application can then take actions based on these messages.

You can listen for DFP specific app events using AppEventListener. These events may occur at any time during the ad's lifecycle, even before AdListener's onAdLoaded() is called.

public interface AppEventListener {
  void onAppEvent(String name, String info);
}

void onAppEvent(String name, String info) is called when an app event occurs in an ad.

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

import com.google.android.gms.ads.doubleclick.*;

public class BannerExample extends Activity implements AppEventListener {
}

and then passed to the PublisherAdView:

adView.setAppEventListener(this);

Here is an example showing how to change the background color of your app depending on an app event with a name of color:

@Override
public void onAppEvent(String name, String info) {
  if ("color".equals(name)) {
    if ("green".equals(info) {
      // Set background color to green.
    } else if ("blue".equals(info) {
      // Set background color to blue.
    } else {
      // Set background color to black.
    }
  }
}

And, here is the corresponding creative that sends color app event messages to the Listener:

<html>
<head>
  <script src="//media.admob.com/api/v1/google_mobile_app_ads.js"></script>
  <script>
    // Send a color=green event when ad loads.
    admob.events.dispatchAppEvent("color", "green");

    handleClick = function() {
      // Send a color=blue event when ad is clicked.
      admob.events.dispatchAppEvent("color", "blue");
    };
  </script>
  <style>
    #ad {
      width: 320px;
      height: 50px;
      top: 0px;
      left: 0px;
      font-size: 24pt;
      font-weight: bold;
      position: absolute;
      background: black;
      color: white;
      text-align: center;
    }
  </style>
</head>
<body>
  <div id="ad" onClick="handleClick()">Carpe diem!</div>
</body>
</html>

Back to top

Ad lifecycle event callbacks

You may optionally track ad lifecycle events like request failures or "click-through" by passing an object to PublisherAdView.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 can 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).

Back to top

What's next