With a few changes to your code, you can combine native and banner ads in your ad requests.
Prerequisites
- Version 11.0.0 or higher of the Google Mobile Ads SDK
- Complete the Get Started guide
Loading the ad
Custom-rendered native ads are loaded via the AdLoader
class, which has its own
AdLoader.Builder
class to customize it during creation. By adding listeners
to the AdLoader
while building it, an app specifies which types of ad formats
it is ready to receive. The AdLoader
then requests just those types.
The AdLoader
object can also be configured to make ad requests that can
result in either a banner ad or a native ad. Adding an
OnPublisherAdViewLoadedListener
to the AdLoader
while building it specifies
that banner ads should compete with native ads to fill the request.
The following code demonstrates how to build an AdLoader
that can load either a
native or banner ad in a single request:
AdLoader adLoader = new AdLoader.Builder(context, "/6499/example/nativeandbanner") .forUnifiedNativeAd(new UnifiedNativeAd.OnUnifiedNativeAdLoadedListener() { @Override public void onUnifiedNativeAdLoaded(UnifiedNativeAd unifiedNativeAd) { // Show the ad. } }) .forPublisherAdView(new OnPublisherAdViewLoadedListener() { @Override public void onPublisherAdViewLoaded(PublisherAdView adView) { // Show the banner ad. } }, AdSize.BANNER, AdSize.MEDIUM_RECTANGLE ...) .withAdListener(new AdListener() { @Override public void onAdFailedToLoad(int errorCode) { // Handle the failure by logging, altering the UI, and so on. } }) .withPublisherAdViewOptions(new PublisherAdViewOptions.Builder() // Methods in the PublisherAdViewOptions.Builder class can be // used here to specify individual options settings. .build()) .build();
The forPublisherAdView
method above prepares the AdLoader
to receive banner
ads. A variable-length list of valid ad sizes must be specified alongside a
OnPublisherAdViewLoadedListener
when invoking forPublisherAdView
. To make a
valid ad request, at least one valid ad size must be specified. When a banner
ad has loaded successfully, the specified listener object's
onPublisherAdViewLoaded
method is called.
Setting AdViewOptions
The last function included in the creation of the AdLoader
above is another
optional method, withPublisherAdViewOptions()
:
.withPublisherAdViewOptions(new PublisherAdViewOptions.Builder()
// Methods in the PublisherAdViewOptions.Builder class can be
// used here to specify individual banner options settings.
.build()
The PublisherAdViewOptions
object allows publishers to set specific options
for banners loaded by the AdLoader
. They are built by the
PublisherAdViewOptions.Builder
class, which offers the following methods to
use when creating an instance:
setManualImpressionsEnabled()
- Enables manual impression reporting for Google Ad Manager reservations. Apps
using manual impressions can determine for themselves when an impression should
be recorded, and can do so by calling
PublisherAdView.recordManualImpression()
. setAppEventListener()
- Sets an
AppEventListener
for the returnedPublisherAdView
. App events are messages sent from a creative's JavaScript to the displaying app.