Loading and rendering ads
This is an archived version of the documentation. Actual documentation for all platforms can be found here.
Getting started
To ensure that the Yandex Mobile Ads SDK runs correctly, follow all the steps to attach an ad library.
Loading ads
Any call of the Mobile Ads SDK should be made from the main thread.
Create an instance of the NativeAdLoader class to get native ads.
Create a configuration for the
nativeAdRequestConfiguration
request using the NativeAdRequestConfiguration.Builder class. As the request parameters, you can use the ad unit ID, method for loading images, age, gender, and other data that might improve the quality of ad selection.To get notifications (ad loaded successfully or failed with an error), create an instance of NativeAdLoadListener and set it as an event listener for the ad loader.
Start the ad loading process.
To get an ad of the correct size, pass the maximum container width and height to an ad request using the setParameters method:
final NativeAdLoader loader = new NativeAdLoader(this);
final HashMap<String, String> parameters = new HashMap<String, String>(){{
put("preferable-height", "123");
put("preferable-width", "321");
}};
final NativeAdRequestConfiguration nativeAdRequestConfiguration =
new NativeAdRequestConfiguration.Builder("demo-native-app-yandex")
.setParameters(parameters).build();
NativeAdLoader mNativeAdLoader = new NativeAdLoader(this);
mNativeAdLoader.loadAd(nativeAdRequestConfiguration);
final NativeAdLoader nativeAdLoader = new NativeAdLoader(this);
nativeAdLoader.setNativeAdLoadListener(new NativeAdLoadListener() {
@Override
public void onAdLoaded(@NonNull final NativeAd nativeAd) {
//bind nativeAd
}
@Override
public void onAdFailedToLoad(@NonNull final AdRequestError error) {
//log error
}
});
final NativeAdRequestConfiguration nativeAdRequestConfiguration =
new NativeAdRequestConfiguration.Builder(AdUnitId).build();
nativeAdLoader.loadAd(nativeAdRequestConfiguration);
We recommend that you keep a strong reference to the ad and its loader throughout the lifecycle of the screen hosting the assets.
Rendering ads
When the ad is loaded, you must render all of its assets. You can get the list of assets available in the ad from the NativeAd ad object.
There are two ways to configure the layout of an ad:
Layout using a template
The easiest way to work with native ads is to use a standard layout template: all you need is a few lines of code in the basic version.
The template already has the complete set of required assets and defines their arrangement relative to each other. The template works with any supported type of native ad.
final NativeBannerView nativeBannerView = new NativeBannerView(this);
nativeBannerView.setAd(nativeAd);
You can customize the native ad template. Learn more in Layout using a template.
Layout without a template
When the template settings aren't enough to get the desired effect, you can configure native ads manually.
With this method, you can lay out native ads yourself by positioning ad elements in respect of each other. Your ad may contain both mandatory and optional display assets. You can find their full list in Native ad assets.
We recommend that you use a layout that includes the complete set of possible assets. Experience has shown that layouts with a complete set of assets are more clickable.
Provide a View for each ad asset using the NativeAdViewBinder.Builder class instance. The class accepts the NativeAdView container as an argument. All the ad components must be defined as a subview of this container.
Link the generated ad layout with the NativeAd native ad object.
final NativeAdViewBinder nativeAdViewBinder = new NativeAdViewBinder.Builder(mNativeAdView)
.setAgeView((TextView) findViewById(R.id.age))
.setBodyView((TextView) findViewById(R.id.body))
.setCallToActionView((TextView) findViewById(R.id.call_to_action))
.setDomainView((TextView) findViewById(R.id.domain))
.setFaviconView((ImageView) findViewById(R.id.favicon))
.setFeedbackView((TextView) findViewById(R.id.feedback))
.setIconView((ImageView) findViewById(R.id.icon))
.setMediaView((MediaView) findViewById(R.id.media))
.setPriceView((TextView) findViewById(R.id.price))
.setRatingView((MyRatingView) findViewById(R.id.rating))
.setReviewCountView((TextView) findViewById(R.id.review_count))
.setSponsoredView((TextView) findViewById(R.id.sponsored))
.setTitleView((TextView) findViewById(R.id.title))
.setWarningView((TextView) findViewById(R.id.warning))
.build();
try {
nativeAd.bindNativeAd(nativeAdViewBinder);
mNativeAdView.setVisibility(View.VISIBLE);
} catch (final NativeAdException exception) {
//log exception
}
Minimum size of an instance of the MediaView class that supports video playback: 300x160 or 160x300 dp (density-independent pixels).
To support video playback in native ad templates, we recommend setting the width for NativeBannerView to at least 300 dp. The correct height for mediaView will be calculated automatically based on the width to height ratio.
Loading multiple ads
Any call of the Mobile Ads SDK should be made from the main thread.
Yandex Mobile Ads SDK provides the option to load multiple ads in a single request (up to nine ads).
To make a bulk ad request, use the NativeBulkAdLoader class instance. The class instance provides the loadAds method with the COUNT argument where you can define the desired number of ads per request.
final NativeBulkAdLoader nativeBulkAdLoader = new NativeBulkAdLoader(this);
nativeBulkAdLoader.setNativeBulkAdLoadListener(new NativeBulkAdLoadListener() {
@Override
public void onAdsLoaded(@NonNull final List<NativeAd> nativeAds) {
for (final NativeAd nativeAd : nativeAds) {
//bind native ad
}
}
@Override
public void onAdsFailedToLoad(@NonNull final AdRequestError error) {
//log error
}
});
final NativeAdRequestConfiguration nativeAdRequestConfiguration =
new NativeAdRequestConfiguration.Builder(AdUnitId).build();
nativeBulkAdLoader.loadAds(nativeAdRequestConfiguration, COUNT);
Yandex Mobile Ads SDK doesn't guarantee that the requested number of ads will be loaded. The resulting array will contain from 0 to COUNT NativeAd objects. You can render all the received ad objects separately from each other using the above methods for laying out native ads.