To get an inline adaptive ad size object for the chosen orientation, use a
static method on the ad size class:
The following example demonstrates these steps:
Kotlin
private
fun
loadAd
()
{
// Create an inline adaptive ad size. 320 is a placeholder value.
// Replace 320 with your banner container width.
val
adSize
=
AdSize
.
getCurrentOrientationInlineAdaptiveBannerAdSize
(
this
,
320
)
// Step 1 - Create a BannerAdRequest object with ad unit ID and size.
val
adRequest
=
BannerAdRequest
.
Builder
(
" AD_UNIT_ID
"
,
adSize
).
build
()
// Step 2 - Load the ad.
BannerAd
.
load
(
adRequest
,
object
:
AdLoadCallback<BannerAd>
{
override
fun
onAdLoaded
(
ad
:
BannerAd
)
{
// Assign the loaded ad to the BannerAd object.
bannerAd
=
ad
// Step 3 - Call BannerAd.getView() to get the View and add it
// to view hierarchy on the UI thread.
activity
?.
runOnUiThread
{
binding
.
bannerViewContainer
.
addView
(
ad
.
getView
(
requireActivity
()))
}
}
override
fun
onAdFailedToLoad
(
loadAdError
:
LoadAdError
)
{
bannerAd
=
null
}
}
)
}
Java
private
void
loadAd
()
{
// Create an inline adaptive ad size. 320 is a placeholder value.
// Replace 320 with your banner container width.
AdSize
adSize
=
AdSize
.
getCurrentOrientationInlineAdaptiveBannerAdSize
(
this
,
320
);
// Step 1 - Create a BannerAdRequest object with ad unit ID and size.
BannerAdRequest
adRequest
=
new
BannerAdRequest
.
Builder
(
" AD_UNIT_ID
"
,
adSize
).
build
();
// Step 2 - Load the ad.
BannerAd
.
load
(
adRequest
,
new
AdLoadCallback<BannerAd>
()
{
@Override
public
void
onAdLoaded
(
@NonNull
BannerAd
ad
)
{
// Assign the loaded ad to the BannerAd object.
bannerAd
=
ad
;
// Step 3 - Call BannerAd.getView() to get the View and add it
// to view hierarchy on the UI thread.
if
(
getActivity
()
!=
null
)
{
getActivity
()
.
runOnUiThread
(()
-
>
binding
.
bannerViewContainer
.
addView
(
ad
.
getView
(
getActivity
())));
}
}
@Override
public
void
onAdFailedToLoad
(
@NonNull
LoadAdError
adError
)
{
bannerAd
=
null
;
}
});
}
When implementing adaptive banners in your app, note these points:
- The inline adaptive banner sizes work best when using the full available
width. In most cases, this size is the full width of the device screen in
use, or the full width of the banner's parent content. You must know the width
of the view to place in the ad, the device width, the parent content width,
and applicable safe areas.
Orient inline adaptive banner size
To preload an inline adaptive banner ad for a specific orientation, use the
following methods:
If your app supports both portrait and landscape views, and you want to preload
an adaptive banner ad in the current orientation, use AdSize.getCurrentOrientationInlineAdaptiveBannerAdSize(Context context, int width)
This method loads an ad in the current orientation.
Limit inline adaptive banner height
By default, inline adaptive banners instantiated without a maxHeight
value
have a maxHeight
equal to the device height. To limit the inline adaptive
banner height, use the AdSize.getInlineAdaptiveBannerAdSize(int width, int maxHeight)
method.
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License
, and code samples are licensed under the Apache 2.0 License
. For details, see the Google Developers Site Policies
. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2025-12-15 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-12-15 UTC."],[],[]]