Skip to end of metadata
Go to start of metadata

Show Banners on Android

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

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 RelativeLayoutLinearLayout, 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.

  • No labels