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).

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) The size of the ad. 
com.google.android.gms:adUnitId setAdUnitId(String) The ad unit ID. 
[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 AdView.
AdListener getAdListener()
Returns the AdListener for this AdView.
AdSize getAdSize()
Returns the size of the banner ad.
String getAdUnitId()
Returns the ad unit ID.
void loadAd(AdRequest adRequest)
Start loading the ad on a background thread.
void pause()
Pause any extra processing associated with this AdView.
void resume()
Resume an AdView after a previous call to pause().
void setAdListener(AdListener adListener)
Set an AdListener for this AdView.
void setAdSize(AdSize adSize)
Sets the size of the banner 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.graphics.drawable.Drawable.Callback
From interface android.view.KeyEvent.Callback
From interface android.view.ViewManager
From interface android.view.ViewParent
From interface android.view.accessibility.AccessibilityEventSource

XML Attributes

com.google.android.gms:adSize

The size of the ad. It must be one of BANNER, FULL_BANNER, LEADERBOARD, MEDIUM_RECTANGLE, SMART_BANNER, WIDE_SKYSCRAPER, or <width>x<height>.

Must be a string value, using '\\;' to escape characters such as '\\n' or '\\uxxxx' for a unicode character.

This may also be a reference to a resource (in the form "@[package:]type:name") or theme attribute (in the form "?[package:][type:]name") containing a value of this type.

This is a private symbol.

Related Methods

com.google.android.gms:adUnitId

The ad unit ID.

Must be a string value, using '\\;' to escape characters such as '\\n' or '\\uxxxx' for a unicode character.

This may also be a reference to a resource (in the form "@[package:]type:name") or theme attribute (in the form "?[package:][type:]name") containing a value of this type.

This is a private symbol.

Related Methods

Public Constructors

public AdView (Context context)

Construct an AdView from code.

Parameters
context The Context the AdView is running in.

public AdView (Context context, AttributeSet attrs)

Construct an AdView from an XML layout.

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

Construct an AdView from an XML layout.

Public Methods

public void destroy ()

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

public AdListener getAdListener ()

Returns the AdListener for this AdView.

public AdSize getAdSize ()

Returns the size of the banner ad.

public String getAdUnitId ()

Returns the ad unit ID.

public void loadAd (AdRequest adRequest)

Start loading the ad on a background thread.

Throws
IllegalArgumentException 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 AdView. This method should be called in the parent Activity's onPause() method.

public void resume ()

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

public void setAdListener (AdListener adListener)

Set an AdListener for this AdView.

public void setAdSize (AdSize adSize)

Sets the size of the banner ad.

Related XML Attributes
Throws
IllegalArgumentException If the size of the banner ad was already set.

public void setAdUnitId (String adUnitId)

Sets the ad unit ID.

Related XML Attributes
Throws
IllegalArgumentException If the ad unit ID was already set.

Protected Methods

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

protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec)