Adaptive banner

Types of adaptive banners

The Yandex Mobile Ads SDK has three types of adaptive banners:

Banner

Description

When to use

Sticky banner

A banner that is pinned to the screen edge and displayed on top of other elements. Banners of this type are unaffected by scrolling.

When you want a banner to "stick", which means to remain in its place and be shown on top of content even if a user scrolls the screen.

Inline banner

A banner that is embedded in your app (for example, in content or a list) as if it's a UI element.

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

Fixed banner

A banner that is fixed in a certain place or used for spaces with fixed height.

When you need to place a banner in a static location, for example, under a toolbar or at the bottom of the screen.

Banners of all types automatically update creatives every 60 seconds.

Code-wise, these banners only differ in how you set their sizes. 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:

  • Mustn't exceed 15% of the screen height.

  • Must be at least 50 dp.

All banner sizes must be specified 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.

Example of creating an adaptive banner with an XML file

class InlineBannerAdActivity : AppCompatActivity(), BannerAdEventListener {

    private var bannerAd: BannerAdView? = null
    private var bannerAdSize: BannerAdSize? = null
    private var currentAdUnitId: String? = null
    private lateinit var binding: ActivityInlineBannerAdBinding

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

        binding.btnLoadBanner.setOnClickListener {
            configureBannerAdSize()
            loadBannerAd()
        }
        binding.btnDestroyBanner.setOnClickListener {
            destroyBanner()
        }

        bannerAd = binding.banner
        configureBannerAdSize()
    }

    private fun configureBannerAdSize() {
        bannerAdSize = BannerAdSize.inlineSize(this@InlineBannerAdActivity, 350, 60)
    }

    fun loadBannerAd() {
        bannerAdSize?.let { bannerAdSize ->
            val selectedAdUnitId = "demo-banner-yandex" // Use R-M-XXXXXX-Y or demo-block (look for the description below)
            if (currentAdUnitId != selectedAdUnitId) {
                destroyBanner()
                createBanner(selectedAdUnitId, bannerAdSize)
            }
            val adRequest = AdRequest.Builder().build()
            bannerAd?.loadAd(adRequest)
        }
    }

    private fun createBanner(adUnitId: String, bannerAdSize: BannerAdSize) {
        bannerAd = BannerAdView(this).apply {
            setAdUnitId(adUnitId)
            setAdSize(bannerAdSize)
            setBannerAdEventListener(this@InlineBannerAdActivity)
        }
        val params = createLayoutParams(bannerAdSize)
        binding.root.addView(bannerAd, params)
    }

    private fun createLayoutParams(size: BannerAdSize): ConstraintLayout.LayoutParams {
        return ConstraintLayout.LayoutParams(
            ConstraintLayout.LayoutParams.MATCH_PARENT,
            ConstraintLayout.LayoutParams.WRAP_CONTENT,
        ).apply {
            bottomToBottom = ConstraintLayout.LayoutParams.PARENT_ID
        }
    }

    private fun destroyBanner() {
        bannerAd?.let {
            it.destroy()
            binding.root.removeView(it)
        }
        bannerAd = null
    }

    override fun onDestroy() {
        destroyBanner()
        super.onDestroy()
    }

    override fun onAdClicked() {}
    override fun onAdFailedToLoad(error: AdRequestError) {}
    override fun onAdLoaded() {}
    override fun onImpression(impressionData: ImpressionData?) {}
    override fun onLeftApplication() {}
    override fun onReturnedToApplication() {}
}
public class InlineBannerAdActivity extends AppCompatActivity implements BannerAdEventListener {

    private BannerAdView bannerAd = null;
    private BannerAdSize bannerAdSize = null;
    private String currentAdUnitId = null;
    private ActivityInlineBannerAdBinding binding;

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

        binding.btnLoadBanner.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                configureBannerAdSize();
                loadBannerAd();
            }
        });

        binding.btnDestroyBanner.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                destroyBanner();
            }
        });

        bannerAd = binding.banner;
        configureBannerAdSize();
    }

    private void configureBannerAdSize() {
        bannerAdSize = BannerAdSize.inlineSize(this, 350, 60);
    }

    private void loadBannerAd() {
        if (bannerAdSize != null) {
            String selectedAdUnitId = "demo-banner-yandex"; // Use R-M-XXXXXX-Y or demo-block
            if (currentAdUnitId == null || !currentAdUnitId.equals(selectedAdUnitId)) {
                destroyBanner();
                createBanner(selectedAdUnitId, bannerAdSize);
                currentAdUnitId = selectedAdUnitId;
            }
            AdRequest adRequest = new AdRequest.Builder().build();
            if (bannerAd != null) {
                bannerAd.loadAd(adRequest);
            }
        }
    }

    private void createBanner(String adUnitId, BannerAdSize bannerAdSize) {
        bannerAd = new BannerAdView(this);
        bannerAd.setAdUnitId(adUnitId);
        bannerAd.setAdSize(bannerAdSize);
        bannerAd.setBannerAdEventListener(this);

        ConstraintLayout.LayoutParams params = createLayoutParams(bannerAdSize);
        binding.getRoot().addView(bannerAd, params);
    }

    private ConstraintLayout.LayoutParams createLayoutParams(BannerAdSize size) {
        ConstraintLayout.LayoutParams params = new ConstraintLayout.LayoutParams(
                ConstraintLayout.LayoutParams.MATCH_PARENT,
                ConstraintLayout.LayoutParams.WRAP_CONTENT
        );
        params.bottomToBottom = ConstraintLayout.LayoutParams.PARENT_ID;
        return params;
    }

    private void destroyBanner() {
        if (bannerAd != null) {
            bannerAd.destroy();
            binding.getRoot().removeView(bannerAd);
            bannerAd = null;
        }
    }

    @Override
    protected void onDestroy() {
        destroyBanner();
        super.onDestroy();
    }
    @Override
    public void onAdClicked() {}
    @Override
    public void onAdFailedToLoad(@Nullable AdRequestError error) {}
    @Override
    public void onAdLoaded() {}
    @Override
    public void onImpression(@Nullable ImpressionData impressionData) {}
    @Override
    public void onLeftApplication() {}
    @Override
    public void onReturnedToApplication() {}
}
<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:id="@+id/coordinatorLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".adunits.InlineBannerAdActivity">

    <Button
        android:id="@+id/btnLoadBanner"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Load Banner"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent" />

    <Button
        android:id="@+id/btnDestroyBanner"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Destroy Banner"
        app:layout_constraintTop_toBottomOf="@id/btnLoadBanner"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        android:layout_marginTop="16dp" />

    <com.yandex.mobile.ads.banner.BannerAdView
        android:id="@+id/banner"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

Example of creating an adaptive banner using a programmatically generated Activity

class InlineBannerAdActivity : AppCompatActivity(), BannerAdEventListener {

    private lateinit var bannerAd: BannerAdView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        val rootLayout = ConstraintLayout(this).apply {
            layoutParams = LayoutParams(MATCH_PARENT, MATCH_PARENT)
        }

        val loadButton = Button(this).apply {
            text = "Load Banner here"
            id = View.generateViewId()
        }

        val destroyButton = Button(this).apply {
            text = "Destroy Banner"
            id = View.generateViewId()
        }

        rootLayout.addView(loadButton, LayoutParams(WRAP_CONTENT, WRAP_CONTENT).apply {
            topToTop = LayoutParams.PARENT_ID
            bottomToBottom = LayoutParams.PARENT_ID
            startToStart = LayoutParams.PARENT_ID
            endToEnd = LayoutParams.PARENT_ID
        })

        rootLayout.addView(destroyButton, LayoutParams(WRAP_CONTENT, WRAP_CONTENT).apply {
            topToBottom = loadButton.id
            startToStart = LayoutParams.PARENT_ID
            endToEnd = LayoutParams.PARENT_ID
            topMargin = 24
        })

        setContentView(rootLayout)

        val bannerSize = BannerAdSize.inlineSize(this, 320, 50)

        loadButton.setOnClickListener {
            destroyBannerIfNeeded(rootLayout)

            bannerAd = BannerAdView(this).apply {
                id = View.generateViewId()
                setAdUnitId("demo-banner-yandex") // Use R-M-XXXXXX-Y or demo-block (look for the description below)
                setAdSize(bannerSize)
                setBannerAdEventListener(this@InlineBannerAdActivity)
            }

            rootLayout.addView(
                bannerAd,
                LayoutParams(MATCH_PARENT, WRAP_CONTENT).apply {
                    bottomToBottom = LayoutParams.PARENT_ID
                    bottomMargin = 32
                }
            )

            bannerAd.loadAd(AdRequest.Builder().build())
        }

        destroyButton.setOnClickListener {
            destroyBannerIfNeeded(rootLayout)
        }
    }

    private fun destroyBannerIfNeeded(container: ConstraintLayout) {
        if (::bannerAd.isInitialized) {
            bannerAd.destroy()
            container.removeView(bannerAd)
        }
    }

    override fun onDestroy() {
        if (::bannerAd.isInitialized) {
            bannerAd.destroy()
            (bannerAd.parent as? ConstraintLayout)?.removeView(bannerAd)
        }
        super.onDestroy()
    }
    override fun onAdClicked() {}
    override fun onAdFailedToLoad(error: AdRequestError) {}
    override fun onAdLoaded() {}
    override fun onImpression(impressionData: ImpressionData?) {}
    override fun onLeftApplication() {}
    override fun onReturnedToApplication() {}
}
public class InlineBannerAdActivity extends AppCompatActivity implements BannerAdEventListener {

    private BannerAdView bannerAd;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        final ConstraintLayout rootLayout = new ConstraintLayout(this);
        rootLayout.setLayoutParams(new LayoutParams(
                LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));

        final Button loadButton = new Button(this);
        loadButton.setText("Load Banner here");
        loadButton.setId(View.generateViewId());

        final Button destroyButton = new Button(this);
        destroyButton.setText("Destroy Banner");
        destroyButton.setId(View.generateViewId());

        LayoutParams loadBtnParams = new LayoutParams(
                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        loadBtnParams.topToTop = LayoutParams.PARENT_ID;
        loadBtnParams.bottomToBottom = LayoutParams.PARENT_ID;
        loadBtnParams.startToStart = LayoutParams.PARENT_ID;
        loadBtnParams.endToEnd = LayoutParams.PARENT_ID;
        rootLayout.addView(loadButton, loadBtnParams);

        LayoutParams destroyBtnParams = new LayoutParams(
                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        destroyBtnParams.topToBottom = loadButton.getId();
        destroyBtnParams.startToStart = LayoutParams.PARENT_ID;
        destroyBtnParams.endToEnd = LayoutParams.PARENT_ID;
        destroyBtnParams.topMargin = 24;
        rootLayout.addView(destroyButton, destroyBtnParams);

        setContentView(rootLayout);

        final BannerAdSize bannerSize = BannerAdSize.inlineSize(this, 320, 50);

        loadButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                destroyBannerIfNeeded(rootLayout);

                bannerAd = new BannerAdView(InlineBannerAdActivity.this);
                bannerAd.setId(View.generateViewId());
                bannerAd.setAdUnitId("demo-banner-yandex"); // Use R-M-XXXXXX-Y or demo-block
                bannerAd.setAdSize(bannerSize);
                bannerAd.setBannerAdEventListener(InlineBannerAdActivity.this);

                LayoutParams bannerParams = new LayoutParams(
                        LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
                bannerParams.bottomToBottom = LayoutParams.PARENT_ID;
                bannerParams.bottomMargin = 32;
                rootLayout.addView(bannerAd, bannerParams);

                bannerAd.loadAd(new AdRequest.Builder().build());
            }
        });

        destroyButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                destroyBannerIfNeeded(rootLayout);
            }
        });
    }

    private void destroyBannerIfNeeded(ConstraintLayout container) {
        if (bannerAd != null) {
            bannerAd.destroy();
            container.removeView(bannerAd);
            bannerAd = null;
        }
    }

    @Override
    protected void onDestroy() {
        if (bannerAd != null) {
            bannerAd.destroy();
            View parent = (View) bannerAd.getParent();
            if (parent instanceof ConstraintLayout) {
                ((ConstraintLayout) parent).removeView(bannerAd);
            }
            bannerAd = null;
        }
        super.onDestroy();
    }
    @Override
    public void onAdClicked() {}
    @Override
    public void onAdFailedToLoad(@Nullable AdRequestError error) {}
    @Override
    public void onAdLoaded() {}
    @Override
    public void onImpression(@Nullable ImpressionData impressionData) {}
    @Override
    public void onLeftApplication() {}
    @Override
    public void onReturnedToApplication() {}
}

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