public final class

AdView

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

Class Overview

The View to display banner ads. The ad size and ad unit ID must be set prior to calling loadAd(AdRequest).

Publishers using DoubleClick for Publishers or Search Ads for Apps should use PublisherAdView or SearchAdView, respectively.

Sample code:

 public class MyActivity extends Activity {
     private AdView mAdView;

     @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.
         mAdView = new AdView(this);
         mAdView.setAdSize(AdSize.SMART_BANNER);
         mAdView.setAdUnitId("myAdUnitId");

         // Create an ad request.
         AdRequest.Builder adRequestBuilder = new AdRequest.Builder();

         // Optionally populate the ad request builder.
         adRequestBuilder.addTestDevice(AdRequest.DEVICE_ID_EMULATOR);

         // Add the AdView to the view hierarchy.
         layout.addView(mAdView);

         // Start loading the ad.
         mAdView.loadAd(adRequestBuilder.build());

         setContentView(layout);
     }

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

         // Resume the AdView.
         mAdView.resume();
     }

     @Override
     public void onPause() {
         // Pause the AdView.
         mAdView.pause();

         super.onPause();
     }

     @Override
     public void onDestroy() {
         // Destroy the AdView.
         mAdView.destroy();

         super.onDestroy();
     }
 }

Summary

XML Attributes
Attribute Name Related Method Description
com.google.android.gms:adSize setAdSize(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
AdView(Context context)
Construct an AdView from code.
AdView(Context context, AttributeSet attrs)
Construct an AdView from an XML layout.
AdView(Context context, AttributeSet attrs, int defStyle)
Construct an AdView from an XML layout.
Public Methods
void destroy()
Destroy the ad view.
AdListener getAdListener()
Returns the AdListener for this ad view.
AdSize getAdSize()
Returns the size of the ad.
String getAdUnitId()
Returns the ad unit ID.
String getMediationAdapterClassName()
Returns the mediation adapter class name.
boolean isLoading()
Returns true if the ad is loading.
void loadAd(AdRequest adRequest)
Starts loading the ad on a background thread.
void pause()
Pauses any extra processing associated with this ad view.
void resume()
Resumes an ad view after a previous call to pause().
void setAdListener(AdListener adListener)
Sets an AdListener for this ad view.
void setAdSize(AdSize adSize)
Sets the size of the ad.
void setAdUnitId(String adUnitId)
Sets the ad unit ID.
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:adSize

Related Methods

com.google.android.gms:adUnitId

Related Methods

Public Constructors

public AdView (Context context)

Construct an AdView from code.

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

public AdView (Context context, AttributeSet attrs)

Construct an AdView from an XML layout.

Parameters
context Context
attrs AttributeSet

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

Construct an AdView from an XML layout.

Parameters
context Context
attrs AttributeSet
defStyle int

Public Methods

public void destroy ()

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

public AdListener getAdListener ()

Returns the AdListener for this ad view.

Returns
AdListener

public AdSize getAdSize ()

Returns the size of the ad. Returns null if setAdSize(AdSize) hasn't been called yet.

Related XML Attributes:

Returns
AdSize

public String getAdUnitId ()

Returns the ad unit ID.

Related XML Attributes:

Returns
String

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 boolean isLoading ()

Returns true if the ad is loading.

Returns
boolean

public void loadAd (AdRequest adRequest)

Starts loading the ad on a background thread.

Parameters
adRequest AdRequest
Throws
IllegalStateException If the size of the ad or the ad unit ID have not been set.

public void pause ()

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

public void resume ()

Resumes an ad view 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 ad view.

Parameters
adListener AdListener

public void setAdSize (AdSize adSize)

Sets the size of the ad.

Related XML Attributes:

Parameters
adSize AdSize
Throws
IllegalStateException If the size of the ad was already set.

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.

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