Documentation

LevelPlay SDK - Android

Banner integration for Android

Integrate banner ads by initializing the SDK, creating banner ad objects post-initialization, and setting appropriate ad sizes and positions.
Read time 7 minutesLast updated 8 days ago

Banners are rectangular, system-initiated ads that can be either static or animated, and appear in a designated area around your live app content.
Note
This documentation is relevant for SDK 8.9.0+.

Prerequisites

  • Make sure that you have correctly integrated the LevelPlay SDK into your application. For details, refer to Integration checklists for Android.
  • Make sure to initialize the SDK using LevelPlay Initialization API.
  • Find the AdUnitID in LevelPlay dashboard.

Create banner ad object and set size

Create the banner ad object after you receive the
onInitSuccess
callback.
// Create ad configuration - optionalLevelPlayAdSize adSize = LevelPlayAdSize.BANNER;LevelPlayBannerAdView.Config adConfig = new LevelPlayBannerAdView.Config.Builder() .setAdSize(adSize) .setPlacementName("placementName") .build();// Create the banner view and set the ad unit idLevelPlayBannerAdView levelPlayBanner = new LevelPlayBannerAdView(context, "adUnitId", adConfig);

LevelPlayAdSize

Description

Dimensions 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 devices and tabletsDevice width X recommended height
Note
In LevelPlay SDK 9.0.0 and later, set the banner size and placement name with
LevelPlayBannerAdView.Config.Builder
when you create the banner.
LevelPlayBannerAdView
no longer has
setAdSize()
or
setPlacementName()
methods.
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 support the adaptive feature (Google, Yandex) return a height based on their optimization logic.
LevelPlayBannerAdView.Config.Builder configBuilder = new LevelPlayBannerAdView.Config.Builder();LevelPlayAdSize adSize = LevelPlayAdSize.createAdaptiveAdSize(context);// Required when using createAdaptiveAdSize()if (adSize != null) { configBuilder.setAdSize(adSize);}LevelPlayBannerAdView levelPlayBanner = new LevelPlayBannerAdView(context, "adUnitId", configBuilder.build());

Specific banner size

Use this option to set a specific banner size:
BANNER
,
LARGE
, or
MEDIUM_RECTANGLE
.
LevelPlayBannerAdView.Config adConfig = new LevelPlayBannerAdView.Config.Builder() .setAdSize(LevelPlayAdSize.MEDIUM_RECTANGLE) .build();LevelPlayBannerAdView levelPlayBanner = new LevelPlayBannerAdView(context, "adUnitId", adConfig);

Placements

LevelPlay supports placements in banners for reporting only. Set the placement name in the ad configuration when you create the banner, so it applies to all reloaded ads. Set the ad size on the same configuration.
// Set the ad size and the placement name on the same ad configurationLevelPlayBannerAdView.Config adConfig = new LevelPlayBannerAdView.Config.Builder() .setAdSize(adSize) .setPlacementName("placementName") .build();LevelPlayBannerAdView levelPlayBanner = new LevelPlayBannerAdView(context, "adUnitId", adConfig);

Set banner listener

Implement
LevelPlayBannerAdViewListener
in your code to get informed of ad delivery.
  • Set the listener before you load the banner ad.
  • Give each banner ad 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() {@Overridepublic void onAdLoaded(@NonNull LevelPlayAdInfo adInfo) {// Ad was loaded successfully }@Overridepublic void onAdLoadFailed(@NonNull LevelPlayAdError error) {// Ad load failed}@Overridepublic void onAdDisplayed(@NonNull LevelPlayAdInfo adInfo) {// Ad was displayed and visible on screen}@Overridepublic void onAdDisplayFailed(@NonNull LevelPlayAdInfo adInfo, @NonNull LevelPlayAdError error) {// Optional. Ad failed to be displayed on screen }@Overridepublic void onAdClicked(@NonNull LevelPlayAdInfo adInfo) {// Ad was clicked}@Overridepublic void onAdExpanded(@NonNull LevelPlayAdInfo adInfo) {// Optional. Ad is opened on full screen }@Overridepublic void onAdCollapsed(@NonNull LevelPlayAdInfo adInfo) {// Optional. Ad is restored to its original size }@Overridepublic void onAdLeftApplication(@NonNull LevelPlayAdInfo adInfo) // Optional. User pressed on the ad and was navigated out of the app });

LevelPlay ad info

The
LevelPlayAdInfo
parameter includes information about the loaded ad.

Load banner ad

To load a banner ad, use
loadAd
.
// Load the banner adlevelPlayBanner.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.
Note
When the banner is displayed again, it completes the remaining time until refresh from the point where it was paused.
  • pauseAutoRefresh
    : Pauses auto-refresh of the banner ad.
  • resumeAutoRefresh
    : Resumes auto-refresh of the banner ad after it has been paused.
// Pause refreshlevelPlayBanner.pauseAutoRefresh();// Resume refreshlevelPlayBanner.resumeAutoRefresh();

Destroy banner ad

To destroy a banner, call the
destroy
method.
You can't show a destroyed banner again. To display more ads, create a new
LevelPlayBannerAdView
object.
levelPlayBanner.destroy();

Full implementation example of banner ads

The following example creates and loads a banner ad with a fixed banner size.
public void createAndLoadBanner() {// Create ad configuration - optionalLevelPlayAdSize adSize = LevelPlayAdSize.BANNER;LevelPlayBannerAdView.Config adConfig = new LevelPlayBannerAdView.Config.Builder() .setAdSize(adSize) .setPlacementName("placementName") .build();// Create the banner view and set the ad unit idLevelPlayBannerAdView levelPlayBanner = new LevelPlayBannerAdView(context, "adUnitId", adConfig);// Add the banner view to the containerViewGroup adContainer = findViewById(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 adlevelPlayBanner.loadAd();// To get actual banner layout size (either custom/standard size or adaptive)int height = adSize.getHeight();int width = adSize.getWidth();}

LevelPlay mediation demo app

The Integration Demo application demonstrates how to integrate banner Ad Unit APIs in your app.
Verify your integration with the Integration Test Suite.

Additional resources