Load Ads
Creating Fullscreen Ads π
The fullscreen APIs combine the existing Rewarded and Interstitial ad formats into a single integration experience.
To show a fullscreen ad, create a fullscreen ad load request using the Placement Name you set up on your Chartboost platform.
// Interstitial Ads
val interstitialRequest =
ChartboostMediationFullscreenAdLoadRequest(
placement = "YOUR_INTERSTITIAL_MEDIATION_PLACEMENT",
keywords = // Construct your keywords
)
// Rewarded Ads
val rewardedRequest =
ChartboostMediationFullscreenAdLoadRequest(
placement = "YOUR_REWARDED_MEDIATION_PLACEMENT",
keywords = // Construct your keywords
)
// Load the fullscreen ad
val result =
ChartboostMediationFullscreenAd.loadFullscreenAd(
context,
interstitialRequest, // or rewardedRequest
chartboostMediationFullscreenAdListener,
)
// Handle the ad load result
result.ad?.let { ad ->
// Success. Show the ad.
CoroutineScope(Main).launch {
ad.show(activity)
}
} ?: run {
// Failure
}
Ad Queueing π
Ad queueing is a new feature for SDK 4.9.0+ that allows queueing up multiple fullscreen ads to show in succession. This can reduce and potentially eliminate latency for ad experiences that require showing fullscreen ads back to back.
Banners and adaptive banners cannot be queued.
-
// Create a Chartboost Mediation fullscreen ad queue with the ad queue manager. The manager returns a ChartboostMediationFullscreenAdQueue. val queue: ChartboostMediationFullscreenAdQueue = ChartboostMediationFullscreenAdQueueManager.queue(context, placementName) // To listen to ad queue events, you can use the ChartboostMediationFullscreenAdQueueListener queue.adQueueListener = object : ChartboostMediationFullscreenAdQueueListener { override fun onFullScreenAdQueueUpdated( adQueue: ChartboostMediationFullscreenAdQueue, result: AdLoadResult, numberOfAdsReady: Int, ) { println( "Fullscreen ad queue updated${ if (result.error != null) { " with error: ${result.error?.cause}" } else { "" } }. Number of ads ready: $numberOfAdsReady", ) } override fun onFullscreenAdQueueExpiredAdRemoved( adQueue: ChartboostMediationFullscreenAdQueue, numberOfAdsReady: Int, ) { println("Fullscreen ad queue removed an expired ad. Number of ads ready: $numberOfAdsReady") } } // To start queueing ads, use the `start()` method. queue.start() // To check if the queue is running, query the read-only `isRunning` property. queue.isRunning // To check if there's an ad in the queue, use the `hasNextAd()` method. // To get an ad from the queue, use the `getNextAd()` method. CoroutineScope(Main).launch { if (queue.hasNextAd) { val ad = queue.nextAd() // Showing an ad needs to be in a suspend function. // Be mindful that ads will need to be reattached to your ChartboostMediationFullscreenAdListener if you are listening to ad cycle events. ad?.listener = fullscreenAdListener() ad?.show(context) } } // To stop the queue, use the `stop()` method. queue.stop() // Keywords can be set at any time, whether the queue is stopped or running. // The next ad request sent by the running queue will use the keywords set here. queue.keywords.set("keyword_key", "keyword_value") // To set a queue capacity, use the queueCapacity property. queue.queueCapacity = 3
-
// Create a Chartboost Mediation fullscreen ad queue with the ad queue manager. The manager returns a ChartboostMediationFullscreenAdQueue. ChartboostMediationFullscreenAdQueue queue = ChartboostMediationFullscreenAdQueueManager.queue(context, placementName) // To listen to ad queue events, you can use the ChartboostMediationFullscreenAdQueueListener final ChartboostMediationFullscreenAdQueueListener fullscreenAdQueueListener = new ChartboostMediationFullscreenAdQueueListener() { @Override public void onFullScreenAdQueueUpdated(@NonNull ChartboostMediationFullscreenAdQueue adQueue, @NonNull AdLoadResult result, int numberOfAdsReady) { System.out.println("Fullscreen ad queue has been updated for placement " + placementName + ". Number of ads ready to show: " + numberOfAdsReady); } @Override public void onFullscreenAdQueueExpiredAdRemoved(@NonNull ChartboostMediationFullscreenAdQueue adQueue, int numberOfAdsReady) { System.out.println("Fullscreen ad queue expired ad has been removed for placement " + placementName + ". Number of ads ready to show: " + numberOfAdsReady); } }; // Attach the created ChartboostMediationFullscreenAdQueueListener with the setAdQueueListener() method. queue.setAdQueueListener(fullscreenAdQueueListener); // To start queueing ads, use the `start()` method. queue.start() // To check if the queue is running, query the `isRunning()` method. queue.isRunning() // To check if there's an ad in the queue, use the `hasNextAd()` method. // To get an ad from the queue, use the `getNextAd()` method. if (queue.hasNextAd()) { ChartboostMediationFullscreenAd ad = queue.getNextAd() if (ad != null) { // Be mindful that ads will need to be reattached to your ChartboostMediationFullscreenAdListener if you are listening to ad cycle events. ad.setListener(fullscreenAdListener()) ad.showFullscreenAdFromJava(context, result -> {}); } else { System.out.println("Fullscreen ad is null. Load an ad first."); } } // To stop the queue, use the `stop()` method. queue.stop() // To set a queue capacity, use the `setQueueCapacity(int value)` method. // The dashboard's queue size settings can be overridden at runtime. // If this placement has a Queue Size setting of 2 and Max Queue Size // is 4 or 5, this line of code will increase this queue's size to // 4. But if Max Queue Size is smaller than the number passed to // setQueueCapacity, the queue size will only be increased up to the // allowed maximum. So if Max Queue Size is 3 then an error message // will be logged and this queue's size will be changed to 3. queue.setQueueCapacity(3)
Banner Ads π
Mediation SDK 4.6.0 introduces a new Adaptive Banner ad format, capable of serving flexible and fixed sized ads in the placement. The new Adaptive Banner ad format provides the following features:
- Choose whether to use Adaptive Ads or Fixed Ads in a given placement.
- Fixed Ads are supported in Adaptive Ad placements (backwards compatible).
- Know whether an ad is fixed or flexible and receive the dimensions of fixed ads.
- Align the ad horizontally and/or vertically.
- Resize the ad container to fit the ad or optionally discard oversized ads that are rendered in the container.
- Ad container can be in-line or on top of publisher content.
To use this new ad format, create a new Adaptive Banner placement in their platform and integrate with the new Adaptive Banner APIs.
Loading Banner Ads π
The API class ChartboostMediationBannerSize
will support both fixed and adaptive banners.
The load call will provide the creative size of successfully loaded ads. If the returned size is -1, then it is a flexible width returned by the partner.
Field | Description |
---|---|
STANDARD |
Static constant that returns a fixed standard banner with a size of 320x50. |
MEDIUM |
Static constant that returns a fixed medium/MREC banner with a size of 300x250. |
LEADERBOARD |
Static constant that returns a fixed leaderboard banner with a size of 728x90. |
adaptive2x1(width: Int) adaptive4x1(width: Int) adaptive6x1(width: Int) adaptive8x1(width: Int) adaptive10x1(width: Int) adaptive1x2(width: Int) adaptive1x3(width: Int) adaptive1x4(width: Int) adaptive9x16(width: Int) adaptive1x1(width: Int) |
Additional conveniences to create sizes based on the IAB ratios (e.g. 6:1, 1:1) with a width. For example, using the 6:1 convenience with a 600 width would return a size of 600x100. Note that these are max sizes, therefore smaller sizes or other aspect ratios may be served. |
Loading Adaptive Banner Ads π
// Make sure you're in a coroutine
CoroutineScope(Main).launch { ...
// Create a listener
val listener = object : ChartboostMediationBannerAdViewListener {
override fun onAdClicked(placement: String) {
// Fired when the banner is clicked
}
override fun onAdImpressionRecorded(placement: String) {
// Fired when a banner impression has occurred
}
override fun onAdViewAdded(
placement: String,
child: View?,
) {
// Fired when a view has been added to the banner
}
}
// Create an adaptive banner ad instance. Here is an example adaptive 6x1
val banner = ChartboostMediationBannerAdView(
context,
"[placement]",
ChartboostMediationBannerAdView.ChartboostMediationBannerSize.adaptive6x1(300),
listener
)
// Load an adaptive banner ad. This variable has the load details.
val adaptiveBannerResult = banner.load(ChartboostMediationBannerAdLoadRequest(
"[placement]",
keywords,
ChartboostMediationBannerAdView.ChartboostMediationBannerSize.adaptive6x1(300)
)
Loading Fixed Banner Ads π
// Make sure you're in a coroutine
CoroutineScope(Main).launch {
...
// Create a listener
val listener = object : ChartboostMediationBannerAdViewListener {
override fun onAdClicked(placement: String) {
// Fired when the banner is clicked
}
override fun onAdImpressionRecorded(placement: String) {
// Fired when a banner impression has occurred
}
override fun onAdViewAdded(
placement: String,
child: View?,
) {
// Fired when a view has been added to the banner
}
}
// Create a standard banner ad instance
val standardBanner = ChartboostMediationBannerAdView(
context,
"[placement]",
ChartboostMediationBannerAdView.ChartboostMediationBannerSize.STANDARD,
listener
)
// It is also possible to get it from the xml
val standardBanner = activity.findViewById(R.id.standard_banner) as ChartboostMediationBannerAdView
// Loading a standard banner. This variable has the load details.
val bannerLoadResult = standardBanner.load(ChartboostMediationBannerAdLoadRequest(
"[placement]",
keywords,
ChartboostMediationBannerAdView.ChartboostMediationBannerSize.STANDARD
)
// Create a medium rectangle banner ad instance
val mediumRectagle = ChartboostMediationBannerAdView(
context,
"[placement]",
ChartboostMediationBannerAdView.ChartboostMediationBannerSize.MEDIUM,
listener
)
// Load a medium rectangle. This variable has the load details.
val mediumRectangleLoadResult = mediumRectagle.load(ChartboostMediationBannerAdLoadRequest(
"[placement]",
keywords,
ChartboostMediationBannerAdView.ChartboostMediationBannerSize.MEDIUM
)
// Create a leaderboard ad instance
val leaderboard = ChartboostMediationBannerAdView(
context,
"[placement]",
ChartboostMediationBannerAdView.ChartboostMediationBannerSize.LEADERBOARD,
listener
)
// Load a leaderboard ad. This variable has the load details.
val leaderboardResult = leaderboard.load(ChartboostMediationBannerAdLoadRequest(
"[placement]",
keywords,
ChartboostMediationBannerAdView.ChartboostMediationBannerSize.LEADERBOARD
)
Resizing Adaptive Banner Container π
val density = context.resources.displayMetrics.density
ChartboostMediationBannerAdView.layoutParams = ViewGroup.LayoutParams(
300 * density,
50 * density
)
// If you want this to be automatic, just use WRAP_CONTENT for
// both width and height.
ChartboostMediationBannerAdView.layoutParams = ViewGroup.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT
)
// You can change this anytime. The next ad will use these sizes.
// To get the size of the currently showing creative, see:
ChartboostMediationBannerAdView.getCreativeSizeDips()
Discarding Oversized Ads π
The setShouldDiscardOversizedAds
will allow you the option to discard or allow ads larger than what is set for ChartboostMediationBannerSize
.
// To drop oversized ads
ChartboostMediation.setShouldDiscardOversizedAds(true)
// To allow oversized ads
ChartboostMediation.setShouldDiscardOversizedAds(false)
Clearing Loaded Ads π
For any unused ads, we recommend invalidating them to free memory space.
Note: Calling clearAd
will remove the current presented ad if any and the cached ad.
ChartboostMediationFullscreenAd.invalidate()
ChartboostMediationBannerAdView.clearAd()