Adaptive sticky banner
An adaptive sticky banner is a small, automatically updated ad placed at the bottom or top of the app screen. The banner doesn't overlap the main app content and is often used in gaming apps.
The adaptive sticky banner delivers maximum performance by optimizing the ad size for each device. This ad type lets developers set a maximum allowable ad width, though the optimal ad size is still determined automatically. The height of the adaptive sticky banner shouldn't exceed 15% of the screen height.
Appearance
This guide shows how to integrate adaptive sticky banners into Flutter apps. Besides code samples and instructions, it also contains format-specific recommendations and links to additional resources.
Prerequisite
- Follow the Yandex Mobile Ads Flutter plugin integration steps described under Quick start.
- Make sure that you have the latest version of the Yandex Mobile Ads Flutter plugin. If you're using mediation, update to the most recent single build version.
Implementation
Key steps to integrate an adaptive sticky banner:
- Create and configure a widget for displaying banner ads.
- Register a callback listener.
- Load the ad.
Features of adaptive sticky banner integration
-
We strongly advise against attempting to load a new ad when receiving an error in the
onAdFailedToLoad()method. If you need to load an ad fromonAdFailedToLoad, limit retry attempts to avoid recurring failed ad requests in case of network connection constraints. -
Adaptive sticky banners work best when utilizing the full available width. In most cases, this will be the full width of the device screen. Be sure to consider the padding parameters set in your app and the display's safe area.
-
To get the size of the ad, use the
BannerAdSize.stickySize(adWidth)method, which accepts the available width of the ad container as the argument. -
The
BannerAdSizeobject, which is calculated using theBannerAdSize.stickySize(adWidth)method, contains constant ad width and height values for identical devices. When testing your app's layout on a specific device, you can be sure that the ad size for that device will remain the same. Use theBannerAdSize::getCalculatedBannerAdSize()method to get the actual width and height of the ad. -
The height of an adaptive sticky banner never exceeds 15% of the screen height and can't be less than 50 dp.
Adding an ad widget to the app layout
To display banner ads, add AdWidget to your app layout.
Example of adding AdWidget to an app screen layout:
@override
Widget build(BuildContext context) {
return Scaffold(
body: Align(
alignment: Alignment.bottomCenter,
child: AdWidget(bannerAd: banner),
),
);
}
Loading and rendering ads
Once you created and added AdWidget to the app screen, you need to load the ads. The ad size must also be calculated for each device before loading an adaptive sticky banner.
That is performed automatically via the SDK API: BannerAdSize.sticky(width: screenWidth).
As an argument, pass the maximum permissible width of the ad container. We recommend using the entire width of the device screen or the width of the parent container. Be sure to consider the padding parameters set in your app and the display's safe area:
BannerAdSize getAdSize() {
final screenWidth = MediaQuery.of(context).size.width.round();
return BannerAdSize.sticky(width: screenWidth);
}
To load ads, you need the ad unit ID you obtained in the Yandex Advertising Network interface (adUnitId).
To notify when ads load or fail to load and to track the life cycle of adaptive sticky banners, set callback functions when creating the BannerAd class.
You can expand ad request parameters through AdRequest(), by passing user interests, contextual page data, location, or other additional info. Adding extra context to ad requests can greatly improve ad relevance. To learn more, see Ad targeting.
The following example shows how to load an adaptive sticky banner. Once loaded, the banner is displayed automatically:
class _MyHomePageState extends State<MyHomePage> {
late BannerAd banner;
var isBannerAlreadyCreated = false;
_loadAd() async {
banner = _createBanner();
setState(() {
isBannerAlreadyCreated = true;
});
// if banner is already created, you can just call
// banner.loadAd(adRequest: const AdRequest());
}
BannerAdSize _getAdSize() {
final screenWidth = MediaQuery.of(context).size.width.round();
return BannerAdSize.sticky(width: screenWidth);
}
_createBanner() {
return BannerAd(
adUnitId: 'R-M-XXXXXX-Y', // or 'demo-banner-yandex'
adSize: _getAdSize(),
adRequest: const AdRequest(),
onAdLoaded: () {
// The ad was loaded successfully. Now it will be shown.
},
onAdFailedToLoad: (error) {
// Ad failed to load with AdRequestError.
// Attempting to load a new ad from the onAdFailedToLoad() method is strongly discouraged.
},
onAdClicked: () {
// Called when a click is recorded for an ad.
},
onLeftApplication: () {
// Called when user is about to leave application (e.g., to go to the browser), as a result of clicking on the ad.
},
onReturnedToApplication: () {
// Called when user returned to application after click.
},
onImpression: (impressionData) {
// Called when an impression is recorded for an ad.
}
);
}
@override
initState() {
super.initState();
MobileAds.initialize();
_loadAd();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Align(
alignment: Alignment.bottomCenter,
child: isBannerAlreadyCreated ? AdWidget(bannerAd: banner) : null,
),
);
}
}
Releasing resources
If a callback occurs after the widget is destroyed, call the destroy() function for the used ad object to release resources:
_createBanner() {
return BannerAd(
adUnitId: 'R-M-XXXXXX-Y', // or 'demo-banner-yandex'
adSize: getAdSize(),
adRequest: const AdRequest(),
onAdLoaded: () {
// The ad was loaded successfully. Now it will be shown.
if (!mounted) {
banner.destroy();
return;
}
},
);
}
Testing adaptive sticky banner integration
Using demo ad units for ad testing
We recommend using test ads to test your adaptive sticky banner integration and your app itself.
To make sure that test ads are returned for each ad request, we created a special demo ad placement ID designed to help you test your ad integration.
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 interface Yandex Advertising Network.
For the list of all available demo ad placement IDs, see Demo ad units for testing.
Testing ad integration
You can check your adaptive sticky banner integration using the SDK's built-in analyzer.
This tool checks whether ads are enabled properly and outputs a detailed report to a log. To view the report, search 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 you're having problems integrating banner ads, you'll get a detailed report on the issues and recommendations for how to fix them.
Using demo ad units for ad testing
We recommend using test ads to test your ad integration and your app itself.
To make sure that test ads are returned for each ad request, we created a special demo ad placement ID designed to help you test your ad integration.
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 interface Yandex Advertising Network.
For the list of all available demo ad placement IDs, see Demo ad units for testing.
Testing ad integration
You can test your ad integration using the native Console tool.
To view detailed logs, call the MobileAds class's enableLogging method.
MobileAds.enableLogging()
To view SDK logs, go to the Console tool and set Subsystem = com.mobile.ads.ads.sdk. You can also filter logs by category and error level.
If you're having problems integrating ads, you'll get a detailed report on the issues and recommendations for how to fix them.
Additional resources
- Link to pub.dev.