public final class

PublisherAdView

extends ViewGroup
java.lang.Object
   ↳ android.view.View
     ↳ android.view.ViewGroup
       ↳ com.google.android.gms.ads.doubleclick.PublisherAdView

Class Overview

The View to display banner ads for use with DoubleClick for Publishers (DFP). The ad size and ad unit ID must be set prior to calling loadAd(PublisherAdRequest).

Sample code:

 public class MyActivity extends Activity {
     private PublisherAdView mPublisherAdView;

     @Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);

         LinearLayout layout = new LinearLayout(this);
         layout.setOrientation(LinearLayout.VERTICAL);

         // Create a banner ad. The ad size and ad unit ID must be set before calling loadAd.
         mPublisherAdView = new PublisherAdView(this);
         mPublisherAdView.setAdSize(AdSize.SMART_BANNER);
         mPublisherAdView.setAdUnitId("myAdUnitId");

         // Create an ad request.
         PublisherAdRequest.Builder publisherAdRequestBuilder = new PublisherAdRequest.Builder();

         // Optionally populate the ad request builder.
         publisherAdRequestBuilder.addTestDevice(PublisherAdRequest.DEVICE_ID_EMULATOR);

         // Add the PublisherAdView to the view hierarchy.
         layout.addView(mPublisherAdView);

         // Start loading the ad.
         mPublisherAdView.loadAd(PublisherAdRequestBuilder.build());

         setContentView(layout);
     }

     @Override
     public void onResume() {
         super.onResume();

         // Resume the PublisherAdView.
         mPublisherAdView.resume();
     }

     @Override
     public void onPause() {
         // Pause the PublisherAdView.
         mPublisherAdView.pause();

         super.onPause();
     }

     @Override
     public void onDestroy() {
         // Destroy the PublisherAdView.
         mPublisherAdView.destroy();

         super.onDestroy();
     }
 }

Summary

XML Attributes
Attribute Name Related Method Description
com.google.android.gms:adSizes setAdSizes(AdSize)  
com.google.android.gms:adUnitId setAdUnitId(String)  
[Expand]
Inherited Constants
From class android.view.ViewGroup
From class android.view.View
[Expand]
Inherited Fields
From class android.view.View
Public Constructors
PublisherAdView(Context context)
Construct an PublisherAdView from code.
PublisherAdView(Context context, AttributeSet attrs)
Construct a PublisherAdView from an XML layout.
PublisherAdView(Context context, AttributeSet attrs, int defStyle)
Construct an PublisherAdView from an XML layout.
Public Methods
void destroy()
Destroy the PublisherAdView.
AdListener getAdListener()
Returns the AdListener for this PublisherAdView.
AdSize getAdSize()
Returns the size of the currently displayed banner ad.
AdSize[] getAdSizes()
Returns the ad sizes supported by this PublisherAdView.
String getAdUnitId()
Returns the ad unit ID.
AppEventListener getAppEventListener()
Returns the AppEventListener for this PublisherAdView.
String getMediationAdapterClassName()
Returns the mediation adapter class name.
OnCustomRenderedAdLoadedListener getOnCustomRenderedAdLoadedListener()
VideoController getVideoController()
Return the VideoController for this ad view.
VideoOptions getVideoOptions()
Return the VideoOptions for this ad view.
boolean isLoading()
Returns true if the ad is loading.
void loadAd(PublisherAdRequest publisherAdRequest)
Start loading the ad on a background thread.
void pause()
Pause any extra processing associated with this PublisherAdView.
void recordManualImpression()
Record a manual impression.
void resume()
Resume a PublisherAdView after a previous call to pause().
void setAdListener(AdListener adListener)
Sets an AdListener for this PublisherAdView.
void setAdSizes(AdSize... adSizes)
Sets the supported sizes of the banner ad.
void setAdUnitId(String adUnitId)
Sets the ad unit ID.
void setAppEventListener(AppEventListener appEventListener)
void setCorrelator(Correlator correlator)
Attach a Correlator to this PublisherAdView.
void setManualImpressionsEnabled(boolean manualImpressionsEnabled)
Enables manual impression reporting for DFP reservations.
void setOnCustomRenderedAdLoadedListener(OnCustomRenderedAdLoadedListener onCustomRenderedAdLoadedListener)
void setVideoOptions(VideoOptions videoOptions)
Set the VideoOptions for this ad view.
Protected Methods
void onLayout(boolean changed, int left, int top, int right, int bottom)
void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
[Expand]
Inherited Methods
From class android.view.ViewGroup
From class android.view.View
From class java.lang.Object
From interface android.view.ViewParent
From interface android.view.ViewManager
From interface android.graphics.drawable.Drawable.Callback
From interface android.view.KeyEvent.Callback
From interface android.view.accessibility.AccessibilityEventSource

XML Attributes

com.google.android.gms:adSizes

Related Methods

com.google.android.gms:adUnitId

Related Methods

Public Constructors

public PublisherAdView (Context context)

Construct an PublisherAdView from code.

Parameters
context Context: The Context the PublisherAdView is running in.

public PublisherAdView (Context context, AttributeSet attrs)

Construct a PublisherAdView from an XML layout.

Parameters
context Context
attrs AttributeSet

public PublisherAdView (Context context, AttributeSet attrs, int defStyle)

Construct an PublisherAdView from an XML layout.

Parameters
context Context
attrs AttributeSet
defStyle int

Public Methods

public void destroy ()

Destroy the PublisherAdView. This method should be called in the parent Activity's onDestroy() method. No other methods should be called on the PublisherAdView after destroy() is called.

public AdListener getAdListener ()

Returns the AdListener for this PublisherAdView.

Returns
AdListener

public AdSize getAdSize ()

Returns the size of the currently displayed banner ad. Returns null if setAdSizes(AdSize...) hasn't been called yet. See getAdSizes() for the ad sizes supported by this PublisherAdView.

Returns
AdSize

public AdSize[] getAdSizes ()

Returns the ad sizes supported by this PublisherAdView. See getAdSize() for the size of the currently displayed banner ad.

Related XML Attributes:

Returns
AdSize[]

public String getAdUnitId ()

Returns the ad unit ID.

Related XML Attributes:

Returns
String

public AppEventListener getAppEventListener ()

Returns the AppEventListener for this PublisherAdView.

Returns
AppEventListener

public String getMediationAdapterClassName ()

Returns the mediation adapter class name. In the case of a mediated ad response, this is the name of the class that was responsible for performing the ad request and rendering the ad. For non-mediated responses, this value will be null.

Returns
String

public OnCustomRenderedAdLoadedListener getOnCustomRenderedAdLoadedListener ()

public VideoController getVideoController ()

Return the VideoController for this ad view.

Returns
VideoController

public VideoOptions getVideoOptions ()

Return the VideoOptions for this ad view.

Returns
VideoOptions

public boolean isLoading ()

Returns true if the ad is loading.

Returns
boolean

public void loadAd (PublisherAdRequest publisherAdRequest)

Start loading the ad on a background thread.

Parameters
publisherAdRequest PublisherAdRequest
Throws
IllegalStateException If the size of the banner ad or the ad unit ID have not been set.

public void pause ()

Pause any extra processing associated with this PublisherAdView. This method should be called in the parent Activity's onPause() method.

public void recordManualImpression ()

Record a manual impression. setManualImpressionsEnabled(boolean) must be enabled for this method to have any effect.

public void resume ()

Resume a PublisherAdView after a previous call to pause(). This method should be called in the parent Activity's onResume() method.

public void setAdListener (AdListener adListener)

Sets an AdListener for this PublisherAdView.

Parameters
adListener AdListener

public void setAdSizes (AdSize... adSizes)

Sets the supported sizes of the banner ad. In most cases, only one ad size will be specified.

Multiple ad sizes can be specified if your application can appropriately handle multiple ad sizes. For example, your application might call getAdSize() during the onAdLoaded() callback and change the layout according to the size of the ad that was loaded. If multiple ad sizes are specified, the PublisherAdView will assume the size of the first ad size until an ad is loaded.

This method also immediately resizes the currently displayed ad, so calling this method after an ad has been loaded is not recommended unless you know for certain that the content of the ad will render correctly in the new ad size. This can be used if an ad needs to be resized after it has been loaded. If more than one ad size is specified, the currently displayed ad will be resized to the first ad size.

Related XML Attributes:

Parameters
adSizes AdSize
Throws
IllegalArgumentException If adSizes is null or empty.

public void setAdUnitId (String adUnitId)

Sets the ad unit ID.

Related XML Attributes:

Parameters
adUnitId String
Throws
IllegalStateException If the ad unit ID was already set.

public void setAppEventListener (AppEventListener appEventListener)

Sets an AppEventListener for this PublisherAdView.

Parameters
appEventListener AppEventListener

public void setCorrelator (Correlator correlator)

Attach a Correlator to this PublisherAdView. Passing a null value to detach any Correlator from this PublisherAdView.

Parameters
correlator Correlator

public void setManualImpressionsEnabled (boolean manualImpressionsEnabled)

Enables manual impression reporting for DFP reservations.

Parameters
manualImpressionsEnabled boolean

public void setOnCustomRenderedAdLoadedListener (OnCustomRenderedAdLoadedListener onCustomRenderedAdLoadedListener)

Sets an OnCustomRenderedAdLoadedListener for this PublisherAdView.

Parameters
onCustomRenderedAdLoadedListener OnCustomRenderedAdLoadedListener

public void setVideoOptions (VideoOptions videoOptions)

Set the VideoOptions for this ad view.

Parameters
videoOptions VideoOptions

Protected Methods

protected void onLayout (boolean changed, int left, int top, int right, int bottom)

Parameters
changed boolean
left int
top int
right int
bottom int

protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec)

Parameters
widthMeasureSpec int
heightMeasureSpec int