Banner integration for Android

Read time 14 minutes

Banners are a rectangular, system-initiated ads that can be either static or animated, and are served in a designated area around your live app content.

Prerequisites

  • Make sure that you have correctly integrated the ironSource SDK into your application. Integration is outlined here.
  • Make sure to initialize the SDK using LevelPlay Initialization API.
  • Find the AdUnitID in LevelPlay dashboard.

Create Banner Ad Object and Set Size

The creation of the banner ad object should be done after receiving onInitSuccess callback.

// Create ad configuration - optional
LevelPlayAdSize adSize = LevelPlayAdSize.BANNER;
LevelPlayBannerAdView.Config adConfig = new LevelPlayBannerAdView.Config.Builder()
   .setAdSize(adSize)
   .setPlacementName("placementName")
   .build();
// Create the banner view and set the ad unit id
LevelPlayBannerAdView levelPlayBanner = new LevelPlayBannerAdView(context, "adUnitId", adConfig);
// Create ad configuration - optional
val adSize = LevelPlayAdSize.BANNER
val adConfig = LevelPlayBannerAdView.Config.Builder()
.setAdSize(adSize)
.setPlacementName("placementName")
.build()
// Create the banner view and set the ad unit id
val levelPlayBanner = LevelPlayBannerAdView(context, "adUnitId", adConfig)
LevelPlayAdSizeDescriptionDimensions in dp (Width X Height)
BANNERStandard banner320 x 50
LARGELarge banner320 x 90
MEDIUM_RECTANGLEMedium Rectangular (MREC)300 x 250
AdaptiveAutomatically renders ads to adjust size and orientation for mobile & tabletsDevice width X recommended height

To create the ad size follow one of these options:

Adaptive ad size that adjusts to the screen width (recommended):

This option returns BANNER or LEADERBOARD according to the device type. Networks that supports adaptive feature (Google, Yandex) will return the a height based on their optimization logic.

 LevelPlayAdSize adSize = LevelPlayAdSize.createAdaptiveAdSize(context);
val adSize = createAdaptiveAdSize(context)

Specific banner size: This option allows you to set specifically a banner size: BANNER, LARGE, MEDIUM_RECTANGLE.

levelPlayBanner.setAdSize(LevelPlayAdSize.MEDIUM_RECTANGLE);
levelPlayBanner.setAdSize(LevelPlayAdSize.MEDIUM_RECTANGLE) 

Placements

We support placements in banners for reporting only. Placements should be set before the loadAd, to affect all reloaded ads.

// Set the placement name
levelPlayBanner.setPlacementName("placementName");
// Set the placement name
levelPlayBanner.setPlacementName("placementName")	 

Set Banner Listener

Implement the LevelPlayBannerAdViewListener in your code to get informed of ad delivery.

  • It is recommended to set the listener before loading the banner ad.
  • Each banner ad should have its own listener implementation.
  • Callbacks run on the main thread.
  • When callbacks are not supported by all networks, they are marked as optional.
levelPlayBanner.setBannerListener(new LevelPlayBannerAdViewListener() {
@Override
public void onAdLoaded(@NonNull LevelPlayAdInfo adInfo) {
// Ad was loaded successfully 
}
@Override
public void onAdLoadFailed(@NonNull LevelPlayAdError error) {
// Ad load failed
}
@Override
public void onAdDisplayed(@NonNull LevelPlayAdInfo adInfo) {
// Ad was displayed and visible on screen
}
@Override
public void onAdDisplayFailed(@NonNull LevelPlayAdInfo adInfo, @NonNull LevelPlayAdError error) {
// Optional. Ad failed to be displayed on screen 
}
@Override
public void onAdClicked(@NonNull LevelPlayAdInfo adInfo) {
// Ad was clicked
}
@Override
public void onAdExpanded(@NonNull LevelPlayAdInfo adInfo) {
// Optional. Ad is opened on full screen 
}
@Override
public void onAdCollapsed(@NonNull LevelPlayAdInfo adInfo) {
// Optional. Ad is restored to its original size 
}
@Override
public void onAdLeftApplication(@NonNull LevelPlayAdInfo adInfo) 
// Optional. User pressed on the ad and was navigated out of the app 
}
); ```

</Tab>
<Tab title="Kotlin">
```kotlin
levelPlayBanner.setBannerListener(object: LevelPlayBannerAdViewListener {
override fun onAdLoaded(adInfo: LevelPlayAdInfo) {
// Ad was loaded successfully 
}
override fun onAdLoadFailed(error: LevelPlayAdError) {
// Ad load failed
}
override fun onAdDisplayed(adInfo: LevelPlayAdInfo) {
// Ad was displayed and visible on screen
}
override fun onAdDisplayFailed(adInfo: LevelPlayAdInfo, error: LevelPlayAdError) {
// Optional. Ad failed to be displayed on screen 
}
override fun onAdClicked(adInfo: LevelPlayAdInfo) {
// Ad was clicked
}
override fun onAdExpanded(adInfo: LevelPlayAdInfo) {
// Optional. Ad is opened on full screen
}
override fun onAdCollapsed(adInfo: LevelPlayAdInfo) {
// Optional. Ad is restored to its original size 
}
override fun onAdLeftApplication(adInfo: LevelPlayAdInfo) {
// Optional. User pressed on the ad and was navigated out of the app 
}
})	 ```
</Tab>
</Tabs>

#### LevelPlay Ad Info

The **LevelPlayAdInfo** parameter includes information about the loaded ad.

## Load Banner Ad

To load a banner ad use **loadAd**.

<Tabs>
<Tab title="Java">
```java
// Load the banner ad
levelPlayBanner.loadAd();
// Load the banner ad
levelPlayBanner.loadAd()

Pause and Resume Banner Refresh

You can pause banner refresh in your code if the refresh value was defined in the platform. Use the following methods to stop the automatic refresh of the banner ad, or re-enable it after pausing.

  • pauseAutoRefresh - pauses auto-refresh of the banner ad.
  • resumeAutoRefresh - resumes auto-refresh of the banner ad after it has been paused.
// Pause refresh
levelPlayBanner.pauseAutoRefresh();
// Resume refresh
levelPlayBanner.resumeAutoRefresh();
// Pause refresh
levelPlayBanner.pauseAutoRefresh()
// Resume refresh
levelPlayBanner.resumeAutoRefresh()

Destroy Banner Ad

To destroy a banner, call the destroy method.

A destroyed banner can no longer be shown again. To display more ads, create a new LevelPlayBannerAdView object.

levelPlayBanner.destroy()
levelPlayBanner.destroy();

Full Implementation Example of Banner Ads

Here is an example for creating and loading a banner ad using adaptive banner size.

public void createAndLoadBanner() {
// Create ad configuration - optional
LevelPlayAdSize adSize = LevelPlayAdSize.BANNER;
LevelPlayBannerAdView.Config adConfig = new LevelPlayBannerAdView.Config.Builder()
   .setAdSize(adSize)
   .setPlacementName("placementName")
   .build();

// Create the banner view and set the ad unit id
LevelPlayBannerAdView levelPlayBanner = new LevelPlayBannerAdView(context, "adUnitId", adConfig);

// Add the banner view to the container
ViewGroup adContainer = findViewById(android.R.id.adContainer);
adContainer.addView(levelPlayBanner);
levelPlayBanner.setBannerListener(new LevelPlayBannerAdViewListener() {
   @Override
   public void onAdLoaded(@NonNull LevelPlayAdInfo adInfo) {
       // Ad was loaded successfully 
   }
   @Override
   public void onAdLoadFailed(@NonNull LevelPlayAdError error) {
       // Ad load failed
   }
   @Override
   public void onAdDisplayed(@NonNull LevelPlayAdInfo adInfo) {
       // Ad was displayed and visible on screen
   }
   @Override
   public void onAdDisplayFailed(@NonNull LevelPlayAdInfo adInfo, @NonNull LevelPlayAdError error) {
       // Ad failed to be displayed on screen
   }
   @Override
   public void onAdClicked(@NonNull LevelPlayAdInfo adInfo) {
       // Ad was clicked
   }
   @Override
   public void onAdExpanded(@NonNull LevelPlayAdInfo adInfo) {
       // Ad is opened on full screen
   }
   @Override
   public void onAdCollapsed(@NonNull LevelPlayAdInfo adInfo) {
       // Ad is restored to its original size
   }
   @Override
   public void onAdLeftApplication(@NonNull LevelPlayAdInfo adInfo) {
       // User pressed on the ad and was navigated out of the app 
   }
});

// Load the banner ad
levelPlayBanner.loadAd();

// To get actual banner layout size (either custom/standard size or adaptive)
int height = adSize.getHeight();
int width = adSize.getWidth();
}
public fun createAndLoadBanner() {
// Create ad configuration - optional
val adSize = LevelPlayAdSize.BANNER
val adConfig = LevelPlayBannerAdView.Config.Builder()
.setAdSize(adSize)
.setPlacementName("placementName")
.build()
// Create the banner view and set the ad unit id
val levelPlayBanner = LevelPlayBannerAdView(context, "adUnitId", adConfig)

// Add the banner view to the container
val adContainer = findViewById<ViewGroup>(R.id.adContainer)
adContainer.addView(levelPlayBanner)
levelPlayBanner.setBannerListener(object: LevelPlayBannerAdViewListener {
override fun onAdLoaded(adInfo: LevelPlayAdInfo) {
   // Ad was loaded successfully 
}
override fun onAdLoadFailed(error: LevelPlayAdError) {
   // Ad load failed
}
override fun onAdDisplayed(adInfo: LevelPlayAdInfo) {
   // Ad was displayed and visible on screen
}
override fun onAdDisplayFailed(adInfo: LevelPlayAdInfo, error: LevelPlayAdError) {
   // Ad failed to be displayed on screen
}
override fun onAdClicked(adInfo: LevelPlayAdInfo) {
   // Ad was clicked
}
override fun onAdExpanded(adInfo: LevelPlayAdInfo) {
   // Ad is opened on full screen
}
override fun onAdCollapsed(adInfo: LevelPlayAdInfo) {
   // Ad is restored to its original size
}
override fun onAdLeftApplication(adInfo: LevelPlayAdInfo) {
   // User pressed on the ad and was navigated out of the app 
}
})

// Load the banner ad
levelPlayBanner.loadAd()

// Get width and height for the container
val width = adSize.getWidth()
val height = adSize.getHeight()
}

LevelPlay Mediation Demo App

The Integration Demo application demonstrates how to integrate banner Ad Unit APIs in your app.

Download Android Demo Application

Verify your integration with our Integration Test Suite.

Next steps

Follow our integration guides to integrate additional Banner Ad networks or configure additional ad formats: