This guide covers loading an anchored adaptive banner ad into an Android app.
Prerequisites
- Complete the Get started guide .
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 Android banners:
ca-app-pub-3940256099942544/9214589741
It's been specially configured to return test ads for every request, and you can use it in your own apps while coding, testing, and debugging. Just make sure you replace it with your own ad unit ID before publishing your app.
For more information about how GMA Next-Gen SDK test ads work, see Enable test ads .
Define the ad view
XML Layout
Add a view to your layout XML file to serve as the container for your anchored adaptive banner ad:
<?xml
version="1.0"
encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/banner_view_container"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">
</FrameLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
Jetpack Compose
Include an AndroidView
element within the Compose UI and define a mutableStateOf<BannerAd?>
variable for holding the banner ad:
// Initialize required variables.
val
context
=
LocalContext
.
current
var
bannerAdState
by
remember
{
mutableStateOf<BannerAd?>(
null
)
}
// The AdView is placed at the bottom of the screen.
Column
(
modifier
=
modifier
.
fillMaxSize
(),
verticalArrangement
=
Arrangement
.
Bottom
)
{
bannerAdState
?.
let
{
bannerAd
-
>
Box
(
modifier
=
Modifier
.
fillMaxWidth
())
{
// Display the ad within an AndroidView.
AndroidView
(
modifier
=
modifier
.
wrapContentSize
(),
factory
=
{
bannerAd
.
getView
(
requireActivity
())
},
)
}
}
}
Load an ad
- Create a
BannerAdRequestobject with an ad unit ID and an anchored adaptive ad size. - Call
BannerAd.load(). - Add
BannerAd.getView()to the view hierarchy.
Kotlin
import
com.google.android.libraries.ads.mobile.sdk.banner.AdSize
import
com.google.android.libraries.ads.mobile.sdk.banner.BannerAd
import
com.google.android.libraries.ads.mobile.sdk.banner.BannerAdEventCallback
import
com.google.android.libraries.ads.mobile.sdk.banner.BannerAdRefreshCallback
import
com.google.android.libraries.ads.mobile.sdk.banner.BannerAdRequest
import
com.google.android.libraries.ads.mobile.sdk.common.AdLoadCallback
import
com.google.android.libraries.ads.mobile.sdk.common.LoadAdError
class
MainActivity
:
Activity
()
{
private
var
bannerAd
:
BannerAd?
=
null
private
lateinit
var
binding
:
ActivityMainBinding
private
lateinit
var
adSize
:
AdSize
private
lateinit
var
bannerViewContainer
:
FrameLayout
override
fun
onCreate
(
savedInstanceState
:
Bundle?)
{
super
.
onCreate
(
savedInstanceState
)
binding
=
ActivityMainBinding
.
inflate
(
layoutInflater
)
setContentView
(
binding
.
root
)
// 320 is a placeholder value. Replace 320 with your banner container width.
adSize
=
AdSize
.
getCurrentOrientationAnchoredAdaptiveBannerAdSize
(
this
,
320
)
// Give the banner container a placeholder height to avoid a sudden layout
// shifts when the ad loads.
bannerViewContainer
=
binding
.
bannerViewContainer
val
bannerLayoutParams
=
bannerViewContainer
.
layoutParams
bannerLayoutParams
.
height
=
adSize
.
getHeightInPixels
(
requireContext
())
bannerViewContainer
.
layoutParams
=
bannerLayoutParams
// Step 1 - Create a BannerAdRequest object with ad unit ID and size.
val
adRequest
=
BannerAdRequest
.
Builder
(
"ca-app-pub-3940256099942544/9214589741"
,
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
import
com.google.android.libraries.ads.mobile.sdk.banner.AdSize
;
import
com.google.android.libraries.ads.mobile.sdk.banner.BannerAd
;
import
com.google.android.libraries.ads.mobile.sdk.banner.BannerAdEventCallback
;
import
com.google.android.libraries.ads.mobile.sdk.banner.BannerAdRefreshCallback
;
import
com.google.android.libraries.ads.mobile.sdk.banner.BannerAdRequest
;
import
com.google.android.libraries.ads.mobile.sdk.common.AdLoadCallback
;
import
com.google.android.libraries.ads.mobile.sdk.common.LoadAdError
;
public
class
MainActivity
extends
AppCompatActivity
{
private
BannerAd
bannerAd
;
private
ActivityMainBinding
binding
;
private
AdSize
adSize
;
private
FrameLayout
bannerViewContainer
;
protected
void
onCreate
(
Bundle
savedInstanceState
)
{
super
.
onCreate
(
savedInstanceState
);
binding
=
ActivityMainBinding
.
inflate
(
getLayoutInflater
());
setContentView
(
binding
.
getRoot
());
// 320 is a placeholder value. Replace 320 with your banner container width.
adSize
=
AdSize
.
getCurrentOrientationAnchoredAdaptiveBannerAdSize
(
this
,
320
);
// Give the banner container a placeholder height to avoid a sudden layout
// shifts when the ad loads.
bannerViewContainer
=
binding
.
bannerViewContainer
;
LayoutParams
bannerLayoutParams
=
bannerViewContainer
.
getLayoutParams
();
bannerLayoutParams
.
height
=
adSize
.
getHeightInPixels
(
this
);
bannerViewContainer
.
setLayoutParams
(
bannerLayoutParams
);
// Step 1 - Create a BannerAdRequest object with ad unit ID and size.
BannerAdRequest
adRequest
=
new
BannerAdRequest
.
Builder
(
"ca-app-pub-3940256099942544/9214589741"
,
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.
runOnUiThread
(
()
-
>
binding
.
bannerViewContainer
.
addView
(
ad
.
getView
(
MainActivity
.
this
)));
}
@Override
public
void
onAdFailedToLoad
(
@NonNull
LoadAdError
adError
)
{
bannerAd
=
null
;
}
});
}
}
Jetpack Compose
// Request an anchored adaptive banner with a width of 360.
val
adSize
=
AdSize
.
getCurrentOrientationAnchoredAdaptiveBannerAdSize
(
requireContext
(),
360
)
// Load the ad when the screen is active.
val
coroutineScope
=
rememberCoroutineScope
()
val
isPreviewMode
=
LocalInspectionMode
.
current
LaunchedEffect
(
context
)
{
bannerAdState
?.
destroy
()
if
(
!
isPreviewMode
)
{
coroutineScope
.
launch
{
when
(
val
result
=
BannerAd
.
load
(
BannerAdRequest
.
Builder
(
AD_UNIT_ID
,
adSize
).
build
()))
{
is
AdLoadResult
.
Success
-
>
{
bannerAdState
=
result
.
ad
}
is
AdLoadResult
.
Failure
-
>
{
showToast
(
"Banner failed to load."
)
Log
.
w
(
Constant
.
TAG
,
"Banner ad failed to load:
$
result
.error"
)
}
}
}
}
}
Refresh an ad
If you configured your ad unit to refresh, you don't need to request another ad when the ad fails to load. GMA Next-Gen SDK respects any refresh rate you specified in the AdMob UI. If you haven't enabled refresh, issue a new request. For more details on ad unit refresh, such as setting a refresh rate, see Use automatic refresh for Banner ads .
Release an ad resource
When you are finished using a banner ad, you can release the banner ad's resources.
To release the ad's resource, you remove the ad from the view hierarchy and drop all its references:
Kotlin
// Remove banner from view hierarchy.
val
parentView
=
adView
?.
parent
if
(
parentView
is
ViewGroup
)
{
parentView
.
removeView
(
adView
)
}
// Destroy the banner ad resources.
adView
?.
destroy
()
// Drop reference to the banner ad.
adView
=
null
Java
// Remove banner from view hierarchy.
if
(
adView
.
getParent
()
instanceof
ViewGroup
)
{
((
ViewGroup
)
adView
.
getParent
()).
removeView
(
adView
);
}
// Destroy the banner ad resources.
adView
.
destroy
();
// Drop reference to the banner ad.
adView
=
null
;
Jetpack Compose
// Destroy the ad when the screen is disposed.
DisposableEffect
(
Unit
)
{
onDispose
{
bannerAdState
?.
destroy
()
}
}
Ad events
You can listen for a number of events in the ad's lifecycle, including ad impression and click, as well as ad opening and closing. It is recommended to set the callback before showing the banner.Kotlin
BannerAd
.
load
(
BannerAdRequest
.
Builder
(
"ca-app-pub-3940256099942544/9214589741"
,
adSize
).
build
(),
object
:
AdLoadCallback<BannerAd>
{
override
fun
onAdLoaded
(
ad
:
BannerAd
)
{
ad
.
adEventCallback
=
object
:
BannerAdEventCallback
{
override
fun
onAdImpression
()
{
// Banner ad recorded an impression.
}
override
fun
onAdClicked
()
{
// Banner ad recorded a click.
}
override
fun
onAdShowedFullScreenContent
()
{
// Banner ad showed.
}
override
fun
onAdDismissedFullScreenContent
()
{
// Banner ad dismissed.
}
override
fun
onAdFailedToShowFullScreenContent
(
fullScreenContentError
:
FullScreenContentError
)
{
// Banner ad failed to show.
}
}
}
// ...
}
)
Java
BannerAd
.
load
(
new
BannerAdRequest
.
Builder
(
"ca-app-pub-3940256099942544/9214589741"
,
adSize
).
build
(),
new
AdLoadCallback<BannerAd>
()
{
@Override
public
void
onAdLoaded
(
@NonNull
BannerAd
ad
)
{
ad
.
setAdEventCallback
(
new
BannerAdEventCallback
()
{
@Override
public
void
onAdImpression
()
{
// Banner ad recorded an impression.
}
@Override
public
void
onAdClicked
()
{
// Banner ad recorded a click.
}
@Override
public
void
onAdShowedFullScreenContent
()
{
// Banner ad showed.
}
@Override
public
void
onAdDismissedFullScreenContent
()
{
// Banner ad dismissed.
}
@Override
public
void
onAdFailedToShowFullScreenContent
(
@NonNull
FullScreenContentError
fullScreenContentError
)
{
// Banner ad failed to show.
}
});
// ...
}
});
Ad refresh callback
The BannerAdRefreshCallback
handles ad refreshing events if you use automatic refresh
for banner ads. Make sure to set the callback before the you add the ad view to
your view hierarchy. For details on ad refreshing, see Refresh an ad
.
Kotlin
BannerAd
.
load
(
BannerAdRequest
.
Builder
(
"ca-app-pub-3940256099942544/9214589741"
,
adSize
).
build
(),
object
:
AdLoadCallback<BannerAd>
{
override
fun
onAdLoaded
(
ad
:
BannerAd
)
{
ad
.
bannerAdRefreshCallback
=
object
:
BannerAdRefreshCallback
{
// Set the ad refresh callbacks.
override
fun
onAdRefreshed
()
{
// Called when the ad refreshes.
}
override
fun
onAdFailedToRefresh
(
loadAdError
:
LoadAdError
)
{
// Called when the ad fails to refresh.
}
}
// ...
}
}
)
Java
BannerAd
.
load
(
new
BannerAdRequest
.
Builder
(
"ca-app-pub-3940256099942544/9214589741"
,
adSize
).
build
(),
new
AdLoadCallback<BannerAd>
()
{
@Override
public
void
onAdLoaded
(
@NonNull
BannerAd
ad
)
{
ad
.
setBannerAdRefreshCallback
(
// Set the ad refresh callbacks.
new
BannerAdRefreshCallback
()
{
@Override
public
void
onAdRefreshed
()
{
// Called when the ad refreshes.
}
@Override
public
void
onAdFailedToRefresh
(
@NonNull
LoadAdError
adError
)
{
// Called when the ad fails to refresh.
}
});
// ...
}
});
Hardware acceleration for video ads
In order for video ads to show successfully in your banner ad views, hardware acceleration must be enabled.
Hardware acceleration is enabled by default, but some apps may choose to disable
it. If this applies to your app, we recommend enabling hardware acceleration for Activity
classes that use ads.
Enable hardware acceleration
If your app does not behave properly with hardware acceleration turned on
globally, you can control it for individual activities as well. To enable or
disable hardware acceleration, you can use the android:hardwareAccelerated
attribute for the <application>
and <activity>
elements in your AndroidManifest.xml
. The following example enables hardware
acceleration for the entire app but disables it for one activity:
<application
android:hardwareAccelerated="true">
<!--
For
activities
that
use
ads,
hardwareAcceleration
should
be
true.
-->
<activity
android:hardwareAccelerated="true"
/>
<!--
For
activities
that
don't
use
ads,
hardwareAcceleration
can
be
false.
-->
<activity
android:hardwareAccelerated="false"
/>
</application>
See the Hardware acceleration guide for more information about options for controlling hardware acceleration. Note that individual ad views cannot be enabled for hardware acceleration if the activity is disabled, so the activity itself must have hardware acceleration enabled.
Download and run the example app that demonstrates the use of the GMA Next-Gen SDK.

