Adaptive sticky banner
An adaptive sticky banner is a small, automatically updated ad placed at the bottom or top of the app screen. It doesn't overlap the main content and is often used in gaming apps.
The adaptive sticky banner delivers maximum performance by optimizing the ad size for each device. With this ad type, developers can set the maximum allowable ad width, and the system determines the optimal ad size automatically.
Appearance
This guide covers the process of integrating adaptive sticky banners into Android apps using the Jetpack Compose extension. Besides code samples and instructions, it contains recommendations and links to additional resources.
Prerequisite
- Follow the SDK integration steps described under Quick start.
- First, you need to initialize the advertising SDK.
- Make sure you have the latest Yandex Mobile Ads SDK version. If you're using mediation, update to the most recent single build version.
To use Jetpack Compose, add the following dependency to build.gradle.kts:
dependencies {
implementation("com.yandex.android:mobileads:8.5.0")
implementation("com.yandex.android:mobileads-compose:8.5.0")
// Compose BOM (at least 2024.01.00)
implementation(platform("androidx.compose:compose-bom:2025.03.00"))
}
Implementation
Key steps to integrate an adaptive sticky banner:
- Use a
Bannercomposable with theBannerSize.Stickysize. - If needed, pass
BannerEventsto track ad events.
Features of adaptive sticky banner integration
-
All calls to Yandex Mobile Ads SDK methods must be made from the main thread.
-
If the
onAdFailedToLoad()callback returns an error, don't try to load a new ad again. If you have to, limit the number of ad loading retries to avoid unsuccessful requests and connection issues. -
Adaptive sticky banners work best when utilizing the full available width. In most cases, this will be the full width of the device screen. Consider the padding parameters set in your app and the display's safe area.
-
Set the ad size using
BannerSize.Sticky(width), where the width is passed asDp(for example,320.dp) and the height is calculated automatically. The context for this calculation is resolved automatically within the composable. -
If you have mediation enabled, wait for initialization to complete before calculating the size of the sticky banner. Until the initialization is complete, only a preliminary size is available, which may change after obtaining the accurate settings.
-
The size of a sticky banner remains constant for a given device. When testing the app's layout on a specific device, you can be sure that the ad size for that device will remain the same.
-
The height of an adaptive sticky banner must be at least 50 dp but no more than 15% of the screen height.
-
Create the banner state with
rememberBannerAdState()and trigger loading by explicitly callingloadAd(). If theadSizechanges, theBannerAdViewis recreated automatically.
Loading and rendering ads
To display a sticky banner, create the state using rememberBannerAdState() with the BannerSize.Sticky size, then pass it to the Banner composable. Trigger loading by explicitly calling loadAd().
To load ads, you need the ad placement ID obtained in the Yandex Advertising Network interface.
You can expand the ad request parameters using AdRequest.Builder(). To do this, pass information about the user's interests, page context, location, and other additional data in the request. Context can greatly improve ad relevance. To learn more, see Ad targeting.
Sample adaptive sticky banner
Once loaded, the banner is displayed automatically:
@Composable
fun MyScreen() {
val bannerState = rememberBannerAdState(
adSize = BannerSize.Sticky(width = 320.dp),
)
LaunchedEffect(Unit) {
bannerState.loadAd(AdRequest.Builder("your-ad-unit-id").build())
}
Banner(
state = bannerState,
modifier = Modifier.fillMaxWidth(),
)
}
To track ad events, pass BannerEvents when creating the state:
@Composable
fun MyScreen() {
val bannerState = rememberBannerAdState(
adSize = BannerSize.Sticky(width = 320.dp),
events = BannerEvents(
onAdLoaded = { Log.d("YandexAds", "Banner loaded") },
onAdFailedToLoad = { error -> Log.e("YandexAds", error.description) },
onAdClicked = { Log.d("YandexAds", "Banner click") },
onImpression = { data -> Log.d("YandexAds", "Impression: ${data?.rawData}") },
),
)
LaunchedEffect(Unit) {
bannerState.loadAd(AdRequest.Builder("your-ad-unit-id").build())
}
Banner(
state = bannerState,
modifier = Modifier.fillMaxWidth(),
)
}
If you serve ads through Adfox, then after the banner ad response, the campaignId, bannerId, and placeId data can be accessed from the BannerAdView objects using the adAttributes property of the AdAttributes type.
Testing adaptive sticky banner integration
Using demo ad units for ad testing
Use test ads to check your adaptive sticky banner integration and the app itself. To make sure that test ads are returned for each ad request, you can use a special demo ad placement ID.
Demo adUnitId: demo-banner-yandex.
Warning
Before publishing your app in the store, make sure to replace the demo placement ID with the real ID you obtained in the Yandex Advertising Network interface.
For the list of all available demo ad placement IDs, see Demo ad units for testing.
Testing ad integration
You can check if your adaptive sticky banners are integrated correctly using the SDK's built-in analyzer. A detailed report with the test results will appear in the log.
To view the report, search for the keyword “YandexAds” in Logcat, a tool for debugging Android apps.
adb logcat -v brief '*:S YandexAds'
If the integration is successful, the following message is returned:
adb logcat -v brief '*:S YandexAds'
mobileads$ adb logcat -v brief '*:S YandexAds'
I/YandexAds(13719): [Integration] Ad type banner was integrated successfully
If there are any banner integration issues, you'll get a detailed issue report and troubleshooting recommendations.
Additional resources
- Link to GitHub.