Skip to end of metadata
Go to start of metadata

Show Banners

This page has instructions and code samples for showing banner ads on Android and iOS.

As of version 4.0, impressions are now being counted on the client side when the ad is rendered. Previously, impressions were counted on the server side. Please reach out to Xandr Support if you have any questions.

On This Page

Android

You can configure your banner ad view using XML, Java, or a mixture of the two. The XML and Java equivalents are listed below.

  • opensdk:placement_id or adview.setPlacementID(String placementID): The placement ID associated with your app's inventory. You must include a placement ID or an error will be thrown. Example: "123456".

    Beginning with version RC2.8, you can also use an inventory code and member ID to request an ad (placement ID is still supported). Currently this is only available from Java (not XML). Note that if both inventory code and placement ID are passed in, the inventory code will be passed to the server instead of the placement ID.

  • opensdk:auto_refresh_interval or adview.setAutoRefreshInterval(long interval): The interval, in milliseconds, at which the ad view will request new ads, if autorefresh is enabled. The minimum period is 15 seconds. The default period is 30 seconds. Set this to 0 to disable autorefresh. Example: "60000".

  • opensdk:ad_width or adview.setAdSize(int width, int height): The width of the advertisement to request from the server. If this is not set, the SDK requests an ad of at least android:layout_width. Note that the Java method setAdSize sets the width and height at the same time. Example: "320".

  • opensdk:ad_height or adview.setAdSize(int width, int height): The height of the view. Note that the Java method setAdSize sets the width and height at the same time. Example: "50dp".

If you're using XML, you must add the xmlns:opensdk namespace attribute describing your application to your layout tag; for example this might be a RelativeLayout, LinearLayout, or FrameLayout.

Here's how to declare a banner in XML. Note that you must replace "YOUR PLACEMENT ID" below with an active placement ID.

If you're using both XML and Java (loadAdOffScreen/loadAd) to define BannerAdView, define auto_refresh_interval in either XML or Java. Do not define auto_refresh_interval in both.

To show ads, you must set up a BannerAdView. The only required method is setPlacementID, but it's a good idea to set the ad size. Note that the height and width you specify here must match the size of the ad placement. The code below shows a banner ad (and assumes that your placement ID points at a 300x50 placement).

This simple example doesn't take advantage of all of the capabilities provided by the SDK - for example, you can also pass in the user's age and gender, as well as whether an ad click should open the device's native browser.

Avoid Potential Memory Leaks

When a view within a fragment or activity is permanently removed, you must call the AdView.destroy() method from the UI thread. Not following this directive may cause memory leaks.


iOS

To show banner ads, include our banner ad view header file. Then, create a banner ad view object; you'll need to pass in a frame, placement ID, and size. Add it as a subview of the current view, and you should start seeing ads.

Don't rasterize views containing the banner ad view

You should not set the shouldRasterize property on any view which contains the ANBannerAdView, since it can hurt drawing performance.

This happens because the shouldRasterize property should only be set on a view whose view hierarchy isn't actively changing. However, the WebView used by ANBannerAdView to show ads has a view hierarchy that may be frequently changing.

This simple example doesn't take advantage of all of the capabilities provided by the SDK - for example, you can also pass in the user's age and gender, as well as whether an ad click should open the device's native browser.

You can use member ID and inventory code instead of a placement ID

The banner code sample below shows how to request ads using the placement ID. Beginning with version RC2.8, you can initialize banners using a combination of member ID and inventory code instead (placement ID is still supported). Here are the methods:

Swift developers will want to use the following code:

Related Topics