Ad slider

Native advertising is a type of ad whose layout can be defined at the app level. This feature allows you to change the visual style of ads and their placement, in the context of the app design specifics.

Ad rendering is performed with native platform tools, which enhances ad performance and quality.

Native ads enhance the overall ad experience, so you can show more ads while keeping users engaged. In the long run, this allows you to maximize your advertising revenue.

Prerequisite

  1. Follow the SDK integration steps described under Quick start.
  2. First, you need to initialize the advertising SDK.
  3. Make sure you have the latest Yandex Mobile Ads SDK version. If you're using mediation, update to the most recent single build version.

Implementation

Key steps for integrating native ads (slider):

  • Create and configure the SliderAdLoader ad loader.
  • Register the ad loader event listener: SliderAdLoadListener.
  • Load the ad.
  • Render the loaded ad.

Specifics of native ad integration

  1. All calls to Yandex Mobile Ads SDK methods must be made from the main thread.

  2. If the onAdFailedToLoad() callback returns an error, don't try to load a new ad again. If there's no other option, limit the number of ad load retries. This will help avoid constant unsuccessful requests and connection issues if there are limitations.

  3. We recommend keeping a strong reference to the SliderAd ad object and its loader SliderAdLoader throughout the lifecycle of the screen hosting interaction with the ad.

  4. Once the slider is loaded, you must render all its assets. You can get the list of assets available in the ad from the SliderAd object.

  5. The slider contains a set of ads, each of which needs to be displayed.

  6. Each ad within the slider contains a set of assets. You can get the list of available ad assets from the NativeAd advertising object. All required ad assets need to be rendered. You can find a description of ad assets on the Native ad assets page.

  7. The size of the ad container should be based on the ad content.

  8. Ads in the slider must be displayed within the same shared container: otherwise, ad impressions won't count.

  9. Video ads usually yield the best results. To display video ads, the size of the ad container and the MediaView component must be at least 300 × 160 dp (density-independent pixels).

  10. We recommend using a layout that includes all the possible components. In practical terms, such layouts result in higher conversion rates.

Loading the slider

To load the native ad slider, create a SliderAdLoader object.

The ad request parameters are configured via the AdRequest.Builder class object. You can improve ad relevance by passing parameters such as the ad unit ID, image loading method, and other relevant data as request parameters.

To receive notifications about ad loading results, create a SliderAdLoadListener instance and pass it to the loadSlider() method.

To load the ad, call the loadAd() method.

The example below shows how to load native ads from Activity:

class SliderNativeAdActivity : AppCompatActivity(R.layout.activity_slider_native_ad) {

    private val nativeAdView get() = binding.nativeAd.root

    private var sliderAdLoader: SliderAdLoader? = null

    private lateinit var binding: ActivityCustomNativeAdBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityCustomNativeAdBinding.inflate(layoutInflater)
        setContentView(binding.root)

        sliderAdLoader = SliderAdLoader(this)
        sliderAdLoader?.loadSlider(
            // Methods in the AdRequest.Builder class can be used here to specify individual options settings.
            AdRequest.Builder("your-ad-unit-id").build(),
            object : SliderAdLoadListener {
                override fun onSliderAdLoaded(p0: SliderAd) {
                    // The slider ad was loaded successfully. Now you can show loaded ads.
                }

                override fun onSliderAdFailedToLoad(p0: AdRequestError) {
                    // Ad failed to load with AdRequestError.
                    // Attempting to load a new ad from the onAdFailedToLoad() method is strongly discouraged.
                }
            })
    }
}
class CustomSliderNativeAdActivity extends AppCompatActivity {
    private NativeAdView mNativeAdView = mBinding.nativeAd.getRoot();

    @Nullable
    private SliderAdLoader mSliderAdLoader = null;

    private ActivityCustomNativeAdBinding mBinding;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding = ActivityCustomNativeAdBinding.inflate(getLayoutInflater());
        setContentView(mBinding.getRoot());

        mSliderAdLoader = new SliderAdLoader(this);
        // Methods in the AdRequest.Builder class can be used here to specify individual options settings.
        mSliderAdLoader.loadSlider(
                new AdRequest.Builder("your-ad-unit-id").build(),
                new SliderAdLoadListener() {
                    @Override
                    public void onSliderAdLoaded(@NonNull SliderAd sliderAd) {
                        // The slider ad was loaded successfully. Now you can show loaded ads.
                    }

                    @Override
                    public void onSliderAdFailedToLoad(@NonNull final AdRequestError error) {
                        // Ad failed to load with AdRequestError.
                        // Attempting to load a new ad from the onAdFailedToLoad() method is strongly discouraged.
                    }
                }
        );
    }
}

Rendering a native ad slider

Once the slider is loaded, you must render all its assets.

A successfully loaded slider contains one or more native ads having the same context. Ads in the slider must be displayed within the same shared container: otherwise, ad impressions won't count.

Manual setup for the layout of the native ads slider

When the template settings aren't enough to get what you're looking for, you can configure the layout for your native ad slider manually.

With this method, you can arrange the slider for your native ads by positioning ad components relative to each other. The ad may include both required and optional assets for display. For the full list, see Native ad assets.

Tip

For each ad in the slider, we recommend using a layout that includes a complete set of possible assets. In practical terms, such layouts result in higher conversion rates.

Link the SliderAd object to the root NativeAdView object and link each ad in the slider to the child NativeAdView object.

Each ad in the slider is laid out using a standard native advertising layout method.

Sample code
private fun showAd(sliderAd: SliderAd) {
    val sliderViewBinder = NativeAdViewBinder.Builder(sliderAdView).build()
    when (val sliderResult = sliderAd.bindSliderAd(sliderViewBinder)) {
        is AdBindingResult.Failure -> {
            //log error
        }
        AdBindingResult.Success -> {
            val nativeAds = sliderAd.nativeAds
            for (nativeAd in nativeAds) {
                val nativeAdView: NativeAdView =
                    mLayoutInflater.inflate(R.layout.widget_native_ad, sliderAdView, false)
                val viewBinder = NativeAdViewBinder.Builder(nativeAdView)
                    .setAgeView(age)
                    .setBodyView(body)
                    .setCallToActionView(callToAction)
                    .setDomainView(domain)
                    .setFaviconView(favicon)
                    .setFeedbackView(feedback)
                    .setIconView(icon)
                    .setMediaView(media)
                    .setPriceView(price)
                    .setRatingView(rating)
                    .setReviewCountView(reviewCount)
                    .setSponsoredView(sponsored)
                    .setTitleView(title)
                    .setWarningView(warning)
                    .build()
                nativeAd.bindNativeAd(viewBinder)
                sliderAdView.addView(nativeAdView)
            }
        }
    }
}
private void showAd(@NonNull final SliderAd sliderAd) {
    final NativeAdViewBinder sliderViewBinder = new NativeAdViewBinder.Builder(mSliderAdView).build();
    AdBindingResult sliderResult = sliderAd.bindSliderAd(sliderViewBinder);
    if (sliderResult instanceof AdBindingResult.Failure) {
        //log error
        return;
    }
    final List<NativeAd> nativeAds = sliderAd.getNativeAds();
    for (final NativeAd nativeAd : nativeAds) {
        final NativeAdView nativeAdView = mLayoutInflater.inflate(R.layout.widget_native_ad, mSliderAdView, false);
        final NativeAdViewBinder viewBinder = new NativeAdViewBinder.Builder(nativeAdView)
                .setAgeView(age)
                .setBodyView(body)
                .setCallToActionView(callToAction)
                .setDomainView(domain)
                .setFaviconView(favicon)
                .setFeedbackView(feedback)
                .setIconView(icon)
                .setMediaView(media)
                .setPriceView(price)
                .setRatingView(rating)
                .setReviewCountView(reviewCount)
                .setSponsoredView(sponsored)
                .setTitleView(title)
                .setWarningView(warning)
                .build();
        nativeAd.bindNativeAd(viewBinder);
        mSliderAdView.addView(nativeAdView);
    }
}

Testing the integration of the native ad slider

Using demo ad units for ad testing

We recommend using test ads to test your native ad slider integration and your app itself.

To guarantee that test ads are returned for every slider request, we created a special demo ad placement ID. designed to help you test your ad integration.

Demo adUnitId for the native ad slider: demo-native-slider-yandex.

Warning

Before publishing your app in the store, make sure to replace the demo placement ID with the real ID you obtained in the interface Boost.

Testing ad integration

You can check your native ad slider integration using the SDK's built-in analyzer.

The tool makes sure the slider is integrated properly and outputs a detailed report to the log. To view the report, search the keyword YandexAds in Logcat, a tool for debugging Android apps.

adb logcat -v brief '*:S YandexAds'

If the integration is successful, the following message is returned:

adb logcat -v brief '*:S YandexAds'
mobileads$ adb logcat -v brief '*:S YandexAds'
I/YandexAds(13719): [Integration] Slider native ad was integrated successfully

If you're having problems integrating your native ad slider, you'll get a detailed report on the issues and recommendations for how to fix them.