Banner ads occupy a spot within an app's layout, either at the top or bottom of the device screen. They stay on screen while users are interacting with the app, and can refresh automatically after a certain period of time.
This guide gets you started with anchored adaptive banner ads. Anchored adaptive banners optimizes the ad size for each device using an ad width you specify.
Anchored adaptive banner ads are fixed aspect ratio ads rather than fixed size ads. The aspect ratio is similar to 320x50. Once you specify the full width available, the Google Mobile Ads SDK returns an ad with optimal height for that width. The optimal height for the ad remains constant across different ad requests, and the content surrounding the ad stays in place when the ad refreshes.
Always test with test ads
When building and testing your apps, make sure you use test ads rather than live, production ads. Failure to do so can lead to suspension of your account.
The easiest way to load test ads is to use our dedicated test ad unit ID for banners:
/21775744923/example/adaptive-banner
The test ad units are configured to return test ads for every request, and you're free to use them in your own apps while coding, testing, and debugging. Just make sure you replace them with your own ad unit IDs before publishing your app.
Get the ad size
To request a banner ad with the correct ad size, follow these steps:
Get the width of the device's screen in density-independent pixels (dp) using
MediaQuery.of(context)
. If you don't want to use the full screen width, you can set your own width.Use the appropriate static method on the
AdSize
class to get anAdSize
object. For example, useAdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize(int width)
to get the ad size for the current orientation.
// Get an AnchoredAdaptiveBannerAdSize before loading the ad.
final size = await AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize(
MediaQuery.sizeOf(context).width.truncate());
Load an ad
The following example instantiates a banner ad:
class BannerExampleState extends State<BannerExample>{
AdManagerBannerAd? _bannerAd;
bool _isLoaded = false;
// TODO: replace this test ad unit with your own ad unit.
final adUnitId = '/21775744923/example/adaptive-banner';
/// Loads a banner ad.
void loadAd() async {
// Get an AnchoredAdaptiveBannerAdSize before loading the ad.
final size = await AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize(
MediaQuery.sizeOf(context).width.truncate());
_bannerAd = AdManagerBannerAd(
adUnitId: adUnitId,
request: const AdManagerAdRequest(),
size: size,
listener: AdManagerBannerAdListener(
// Called when an ad is successfully received.
onAdLoaded: (ad) {
debugPrint('$ad loaded.');
setState(() {
_isLoaded = true;
});
},
// Called when an ad request failed.
onAdFailedToLoad: (ad, err) {
debugPrint('AdManagerBannerAd failed to load: $error');
// Dispose the ad here to free resources.
ad.dispose();
},
),
)..load();
}
}
Banner ad events
Through the use of AdManagerBannerAdListener
, you can listen for lifecycle events, such
as when an ad is loaded. This example implements each method and logs a message
to the console:
class BannerExampleState extends State<BannerExample> {
AdManagerBannerAd? _bannerAd;
bool _isLoaded = false;
// TODO: replace this test ad unit with your own ad unit.
final adUnitId = '/21775744923/example/adaptive-banner';
/// Loads a banner ad.
void loadAd() async {
// Get an AnchoredAdaptiveBannerAdSize before loading the ad.
final size = await AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize(
MediaQuery.sizeOf(context).width.truncate());
_bannerAd = AdManagerBannerAd(
adUnitId: adUnitId,
request: const AdManagerAdRequest(),
size: size,
listener: AdManagerBannerAdListener(
// Called when an ad is successfully received.
onAdLoaded: (ad) {
debugPrint('$ad loaded.');
setState(() {
_isLoaded = true;
});
},
// Called when an ad request failed.
onAdFailedToLoad: (ad, err) {
debugPrint('AdManagerBannerAd failed to load: $error');
// Dispose the ad here to free resources.
ad.dispose();
},
// Called when an ad opens an overlay that covers the screen.
onAdOpened: (Ad ad) {},
// Called when an ad removes an overlay that covers the screen.
onAdClosed: (Ad ad) {},
// Called when an impression occurs on the ad.
onAdImpression: (Ad ad) {},
),
)..load();
}
}
Display a banner ad
To display a AdManagerBannerAd
as a widget, you must instantiate an AdWidget
with a supported ad after calling load()
. You can create the widget before
calling load()
, but load()
must be called before adding it to the widget
tree.
AdWidget
inherits from Flutter's Widget class and can be used like any other
widget. On iOS, make sure you place the widget in a widget with a specified
width and height. Otherwise, your ad may not be displayed. A AdManagerBannerAd
can be placed in a container with a size that matches the ad:
if (_bannerAd != null) {
Align(
alignment: Alignment.bottomCenter,
child: SafeArea(
child: SizedBox(
width: _bannerAd!.size.width.toDouble(),
height: _bannerAd!.size.height.toDouble(),
child: AdWidget(ad: _bannerAd!),
),
),
)
}
An ad must be disposed when access to it is no longer needed. The best practice
for when to call dispose()
is either after the AdWidget
is removed from the
widget tree or in the AdManagerBannerAdListener.onAdFailedToLoad()
callback.
That's it! Your app is now ready to display banner ads.
Scrolling limitation on Android 9 and below
We are aware that some older or less powerful devices running Android 9 or earlier may have suboptimal performance when displaying inline banner ads within scrolling views. We recommend only using these types of banners on Android 10 or later. Fixed position banners such as anchored banners are not affected and can be used with optimal performance on all Android API levels.
Learn about other banner types
Familiarize yourself with other types of banners defined in this section for your Flutter application.
Inline adaptive banners
Inline adaptive banners have variable height and are larger, taller banners compared to anchored adaptive banners. Inline adaptive banners are recommended over anchored adaptive banner ads for apps that place banner ads in scrollable content. For more details, see inline adaptive banners.
Collapsible banners
Collapsible banner ads are banner ads that are initially presented as a larger overlay, with a button to collapse the ad to a smaller size. Consider using this banner to further optimize your performance. For more details, see collapsible banner ads.