SearchAdView

public final classSearchAdViewextendsViewGroup

TheViewto display search banner ads for use with Search Ads for Apps. The ad size and ad unit ID must be set prior to calling loadAd(DynamicHeightSearchAdRequest).

Sample code:

public class MyActivity extends Activity {
private SearchAdView mSearchAdView;

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

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

// Create a search ad. The ad size and ad unit ID must be set before calling loadAd.
mSearchAdView = new SearchAdView(this);
mSearchAdView.setAdSize(AdSize.SEARCH);
mSearchAdView.setAdUnitId( "myAdUnitId" );

// Create an ad request.
DynamicHeightSearchAdRequest.Builder builder =
new DynamicHeightSearchAdRequest.Builder();

// Set the query.
builder.setQuery( "flowers" );

// Optionally populate the ad request builder.
builder.setAdTest(true);
builder.setNumber(2);
builder.setCssWidth(300); // Equivalent to "width" CSA parameter.

// Add the SearchAdView to the view hierarchy.
layout.addView(mSearchAdView);

// Start loading the ad.
mSearchAdView.loadAd(builder.build());

setContentView(layout);
}

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

// Resume the SearchAdView.
mSearchAdView.resume();
}

@Override
public void onPause() {
// Pause the SearchAdView.
mSearchAdView.pause();

super.onPause();
}

@Override
public void onDestroy() {
// Destroy the SearchAdView.
mSearchAdView.destroy();

super.onDestroy();
}
}

XML Attribute Summary

Inherited Constant Summary

Inherited Field Summary

Public Constructor Summary

SearchAdView(Context context)
Construct aSearchAdView from code.
SearchAdView(Context context,AttributeSet attrs)
Construct aSearchAdView from an XML layout.
SearchAdView(Context context,AttributeSet attrs, int defStyle)
Construct aSearchAdView from an XML layout.

Public Method Summary

void
destroy()
Destroy theSearchAdView.
AdListener
getAdListener()
Returns theAdListener for thisSearchAdView.
AdSize
getAdSize()
Returns the size of the banner ad.
String
getAdUnitId()
Returns the ad unit ID.
void
loadAd(DynamicHeightSearchAdRequest dynamicHeightSearchAdRequest)
Start loading the ad on a background thread.
void
loadAd(SearchAdRequest searchAdRequest)
Start loading the ad on a background thread.
void
pause()
Pause any extra processing associated with thisSearchAdView.
void
resume()
Resume anSearchAdView after a previous call to pause().
void
setAdListener(AdListener adListener)
Sets anAdListener for thisSearchAdView.
void
setAdSize(AdSizeadSize)
Sets the size of the banner ad.
void
setAdUnitId(StringadUnitId)
Sets the ad unit ID.

Protected Method Summary

void
onLayout(boolean changed, int left, int top, int right, int bottom)
void
onMeasure(int widthMeasureSpec, int heightMeasureSpec)

Inherited Method Summary

XML Attributes

AdsAttrs_adSize

AdsAttrs_adUnitId

Public Constructors

publicSearchAdView(Context context)

Construct aSearchAdView from code.

Parameters
context TheContext theSearchAdView is running in.

publicSearchAdView(Contextcontext, AttributeSet attrs)

Construct aSearchAdView from an XML layout.

publicSearchAdView(Contextcontext, AttributeSet attrs, int defStyle)

Construct aSearchAdView from an XML layout.

Public Methods

public voiddestroy()

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

publicAdListener getAdListener()

Returns theAdListener for thisSearchAdView.

publicAdSize getAdSize()

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

publicString getAdUnitId()

Returns the ad unit ID.

public voidloadAd(DynamicHeightSearchAdRequest dynamicHeightSearchAdRequest)

Start loading the ad on a background thread.

Throws
IllegalStateException If the size of ad is anything other than AdSize.SEARCH, or the ad unit ID has not been set.

public voidloadAd(SearchAdRequest searchAdRequest)

Start loading the ad on a background thread.

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

public voidpause()

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

public voidresume()

Resume anSearchAdView after a previous call topause(). This method should be called in the parent Activity'sActivity.onResume() method.

public voidsetAdListener(AdListener adListener)

Sets anAdListener for thisSearchAdView.

public voidsetAdSize(AdSize adSize)

Sets the size of the banner ad.

Throws
IllegalStateException If the size of the banner ad was already set.

public voidsetAdUnitId(String adUnitId)

Sets the ad unit ID.

Throws
IllegalStateException If the ad unit ID was already set.

Protected Methods

protected voidonLayout(boolean changed, int left, int top, int right, int bottom)

protected voidonMeasure(int widthMeasureSpec, int heightMeasureSpec)

Attribute Name
AdsAttrs_adSize
AdsAttrs_adUnitId