Banner

The Yandex Mobile Ads SDK has three types of banners:

Banner

Description

When to use

Sticky

A sticky banner that attaches to the screen edge and appears above other elements. It stays stable while scrolling.

When the banner should “stick” and stay above content even when the user scrolls.

Inline

A banner embedded in the UI—in lists, content, and so on.

When the banner should be part of the content: in a news feed, between posts, or as a list item.

Fixed

Used for fixed banners or when the height of the placement is fixed.

When you need simple static placement—for example under a toolbar or at the bottom.

All types automatically refresh the creative every 60 seconds.

In code, the types differ only in how you set their size. Examples:

BannerAdSize.stickySize(context: Context, widthDp: Int) 
BannerAdSize.inlineSize(widthDp: Int, heightDp: Int)
BannerAdSize.fixedSize(widthDp: Int)

Note

Examples showing how all the format types work are available in the demo project.

Entity descriptions:

Entity

Description

BannerAdSize

The banner height:

  • must not exceed 15% of the screen height;

  • must be at least 50 dp.

All banner dimensions must be specified strictly in dp.

adUnitId

Use:

  • development mode — to work with demo ad units;

  • production mode — to work with R-M-XXXXXX-Y (for the actual ID, check the Yandex Advertising Network interface). R-M-XXXXXX-Y is a template for your actual ad unit ID that will be used to receive various creatives.

Implementation

Main steps for integrating an adaptive inline banner:

  • Create and configure a view for displaying banner ads.
  • Register a callback listener.
  • Load the ad.
  • Pass additional settings if you work through the Adfox system.

Adaptive inline banner specifics

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

  2. For video ads to display correctly, hardware acceleration must be enabled. It is on by default, but some apps disable it. If yours does, enable hardware acceleration for activities that show ads.

  3. If you receive an error in the onAdFailedToLoad() callback, do not try to load a new ad again immediately. If you still need to retry, limit how many times you reload. That helps avoid endless failed requests and connectivity issues when the network is limited.

  4. For adaptive inline banners to work correctly, make your layouts adaptive. Ignoring this can cause incorrect ad rendering.

  5. Adaptive inline banners work best when they use the full available width. In most cases that is the full screen width of the device. Account for padding and safe areas in your UI.

  6. The adaptive inline banner is meant for scrollable content. The banner can be as tall as the device screen or limited to a maximum height, depending on the API.

  7. To obtain the ad size, use BannerAdSize.inline(context, adWidth, maxAdHeight) with the context, the available width of the ad container, and the maximum allowed ad height.

  8. A BannerAdSize computed with BannerAdSize.inline(context, adWidth, maxAdHeight) carries technical data for the backend to pick efficient ad dimensions. The ad height may change on each load. Actual width and height are available after a successful load callback.

Adding the ad view to the app layout

To show banner ads, add BannerAdView to your app layout—either programmatically or via an XML file.

Example of adding BannerAdView to an app screen layout:

# activity.xml
...
<com.yandex.mobile.ads.banner.BannerAdView
        android:id="@+id/ad_container_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
...

You can also create a BannerAdView programmatically:

val bannerAd = BannerAdView(this)
final BannerAdView bannerAd = new BannerAdView(this);

Loading and displaying ads

After you create BannerAdView and add it to the screen, load the ad. Before loading an adaptive inline banner, compute the ad size for each device.

The SDK API does this automatically with BannerAdSize.inline(context, adWidth, maxAdHeight). Pass the context, the available width of the ad container, and the maximum allowed ad height. Adaptive inline banners work best when they use the full screen width; account for padding and safe areas:

private val adSize: BannerAdSize
    get() {
        val screenHeight = resources.displayMetrics.run { heightPixels / density }.roundToInt()
        // Calculate the width of the ad, taking into account the padding in the ad container.
        var adWidthPixels = binding.adContainerView.width
        if (adWidthPixels == 0) {
            // If the ad hasn't been laid out, default to the full screen width
            adWidthPixels = resources.displayMetrics.widthPixels
        }
        val adWidth = (adWidthPixels / resources.displayMetrics.density).roundToInt()
        val maxAdHeight = screenHeight / 2

        return BannerAdSize.inline(context, adWidth, maxAdHeight)
    }
@NonNull
private BannerAdSize getAdSize() {
    final DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
    final int screenHeight = Math.round(displayMetrics.heightPixels / displayMetrics.density);
    // Calculate the width of the ad, taking into account the padding in the ad container.
    int adWidthPixels = mBinding.adContainerView.getWidth();
    if (adWidthPixels == 0) {
        // If the ad hasn't been laid out, default to the full screen width
        adWidthPixels = displayMetrics.widthPixels;
    }
    final int adWidth = Math.round(adWidthPixels / displayMetrics.density);
    // Determine the maximum allowable ad height. The current value is given as an example.
    final int maxAdHeight = screenHeight / 2;

    return BannerAdSize.inline(this, adWidth, maxAdHeight);
}

You also need an Activity Context and the adUnitId from the Yandex Advertising Network interface.

To receive load success/failure and track adaptive inline banner lifecycle events, set a BannerAdEventListener on BannerAdView.

You can extend the ad request via AdRequest.Builder() by passing user interests, page context, location, or other data. Extra contextual data in the request can significantly improve ad quality.

The example below shows how to load an adaptive inline banner. After a successful load, the banner displays automatically:

class AdaptiveInlineBannerAdActivity : AppCompatActivity(R.layout.activity_inline_banner_ad) {
    private var bannerAd: BannerAdView? = null
    private lateinit var binding: ActivityInlineBannerAdBinding

    private val adSize: BannerAdSize
        get() {
            val screenHeight = resources.displayMetrics.run { heightPixels / density }.roundToInt()
            // Calculate the width of the ad, taking into account the padding in the ad container.
            var adWidthPixels = binding.adContainerView.width
            if (adWidthPixels == 0) {
                // If the ad hasn't been laid out, default to the full screen width
                adWidthPixels = resources.displayMetrics.widthPixels
            }
            val adWidth = (adWidthPixels / resources.displayMetrics.density).roundToInt()
            // Determine the maximum allowable ad height. The current value is given as an example.
            val maxAdHeight = screenHeight / 2

            return BannerAdSize.inline(context, adWidth, maxAdHeight)
        }

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

        // Since we're loading the banner based on the adContainerView size,
        // we need to wait until this view is laid out before we can get the width
        binding.adContainerView.viewTreeObserver.addOnGlobalLayoutListener(object :
            ViewTreeObserver.OnGlobalLayoutListener {
            override fun onGlobalLayout() {
                binding.adContainerView.viewTreeObserver.removeOnGlobalLayoutListener(this);
                bannerAd = loadBannerAd(adSize)
            }
        })
    }

    private fun loadBannerAd(adSize: BannerAdSize): BannerAdView {
        return binding.banner.apply {
            setAdSize(adSize)
            setBannerAdEventListener(object : BannerAdEventListener {
                override fun onAdLoaded() {
                    // If this callback occurs after the activity is destroyed, you
                    // must call destroy and return or you may get a memory leak.
                    // Note `isDestroyed` is a method on Activity.
                    if (isDestroyed) {
                        bannerAd?.destroy()
                        return
                    }
                }

                override fun onAdFailedToLoad(adRequestError: AdRequestError) {
                    // Ad failed to load with AdRequestError.
                    // Attempting to load a new ad from the onAdFailedToLoad() method is strongly discouraged.
                }

                override fun onAdClicked() {
                    // Called when a click is recorded for an ad.
                }

                override fun onImpression(impressionData: ImpressionData?) {
                    // Called when an impression is recorded for an ad.
                }
            })
            loadAd(
                AdRequest.Builder("your-ad-unit-id")
                    // Methods in the AdRequest.Builder class can be used here to specify individual options settings.
                    .build()
            )
        }
    }
}
public class AdaptiveInlineBannerAdActivity extends AppCompatActivity {
    @Nullable
    private BannerAdView mBannerAd = null;
    private ActivityInlineBannerAdBinding mBinding;

    public AdaptiveInlineBannerAdActivity() {
        super(R.layout.activity_inline_banner_ad);
    }

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

        // Since we're loading the banner based on the adContainerView size,
        // we need to wait until this view is laid out before we can get the width
        mBinding.adContainerView.getViewTreeObserver().addOnGlobalLayoutListener(
                new ViewTreeObserver.OnGlobalLayoutListener() {
                    @Override
                    public void onGlobalLayout() {
                        mBinding.adContainerView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                        mBannerAd = loadBannerAd(getAdSize());
                    }
                }
        );
    }

    @NonNull
    private BannerAdSize getAdSize() {
        final DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
        final int screenHeight = Math.round(displayMetrics.heightPixels / displayMetrics.density);
        // Calculate the width of the ad, taking into account the padding in the ad container.
        int adWidthPixels = mBinding.adContainerView.getWidth();
        if (adWidthPixels == 0) {
            // If the ad hasn't been laid out, default to the full screen width
            adWidthPixels = displayMetrics.widthPixels;
        }
        final int adWidth = Math.round(adWidthPixels / displayMetrics.density);
        // Determine the maximum allowable ad height. The current value is given as an example.
        final int maxAdHeight = screenHeight / 2;

        return BannerAdSize.inline(this, adWidth, maxAdHeight);
    }

    @NonNull
    private BannerAdView loadBannerAd(@NonNull final BannerAdSize adSize) {
        final BannerAdView bannerAd = mBinding.banner;
        bannerAd.setAdSize(adSize);
        bannerAd.setBannerAdEventListener(new BannerAdEventListener() {
            @Override
            public void onAdLoaded() {
                // If this callback occurs after the activity is destroyed, you
                // must call destroy and return or you may get a memory leak.
                // Note `isDestroyed` is a method on Activity.
                if (isDestroyed() && mBannerAd != null) {
                    mBannerAd.destroy();
                }
            }

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

            @Override
            public void onAdClicked() {
                // Called when a click is recorded for an ad.
            }

            @Override
            public void onImpression(@Nullable ImpressionData impressionData) {
                // Called when an impression is recorded for an ad.
            }
        });
        final AdRequest adRequest = new AdRequest.Builder("your-ad-unit-id")
                // Methods in the AdRequest.Builder class can be used here to specify individual options settings.
                .build();
        bannerAd.loadAd(adRequest);
        return bannerAd;
    }
}

Releasing resources

If a callback fires after the Activity lifecycle has ended, release resources by calling destroy() on the ad object:

private fun loadBannerAd(adSize: BannerAdSize): BannerAdView {
    return binding.banner.apply {
        setBannerAdEventListener(object : BannerAdEventListener {
            override fun onAdLoaded() {
                // If this callback occurs after the activity is destroyed, you
                // must call destroy and return or you may get a memory leak.
                // Note `isDestroyed` is a method on Activity.
                if (isDestroyed) {
                    bannerAd?.destroy()
                    return
                }
            }
            ...
        })
        ...
    }
}
@NonNull
private BannerAdView loadBannerAd(@NonNull final BannerAdSize adSize) {
    final BannerAdView bannerAd = mBinding.banner;
    bannerAd.setBannerAdEventListener(new BannerAdEventListener() {
        @Override
        public void onAdLoaded() {
            // If this callback occurs after the activity is destroyed, you
            // must call destroy and return or you may get a memory leak.
            // Note `isDestroyed` is a method on Activity.
            if (isDestroyed() && mBannerAd != null) {
                mBannerAd.destroy();
            }
        }
        ...
    });
    ...
    return bannerAd;
}

Checking integration

Build and run your project. You can check if the integration is successful by searching the YandexAds keyword in Logcat:

[Integration] Ad type banner was integrated successfully