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 4 minutesLast updated 6 hours ago
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);
Banner Sizes
LevelPlayAdSize | Description | Dimensions in dp (Width X Height) |
---|---|---|
BANNER | Standard banner | 320 x 50 |
LARGE | Large banner | 320 x 90 |
MEDIUM_RECTANGLE | Medium Rectangular (MREC) | 300 x 250 |
Adaptive | Automatically renders ads to adjust size and orientation for mobile & tablets | Device width X recommended height |
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);
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 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 } );
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 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();
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()
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(); }