Enabling banner ads

Warning.

This is an archived version of the documentation. Actual documentation for all platforms can be found here.

  1. Banner types
  2. Enabling a banner
  3. Example of working with banner ads

Banner types

Features:

  1. The specified banner width is used. The height is selected automatically.
  2. The width of banners is set using the stickySize method.
  3. The banner height shouldn't exceed 15% of the device height and should be at least 50 dp.

Examples of displaying banners:

Enabling a banner

Creating BannerAdview

  1. Add an object of the BannerAdView class to the project using an XML file or programmatically.

    // Creating an mBannerAdView instance using an XML file.
            mBannerAdView = (BannerAdView) findViewById(R.id.banner_view);
    
    // Creating an mBannerAdView instance programmatically.
            mBannerAdView = new BannerAdView(this);
  2. Set the AdUnitId using the setAdUnitId method.

    mBannerAdView.setAdUnitId(<AdUnitId>)

    AdUnitId is a unique identifier in R-M-XXXXXX-Y format, which is assigned in the Partner Interface.

  3. Set the banner size using the setAdSize method.

    To set the width of a banner, call the stickySize(int width) method, where width is the maximum banner width.

    mBannerAdView.setAdSize(AdSize.stickySize(width));
    Restriction. Banner size requirements when displaying video ads

    Minimum size of a banner that supports video playback is 300x160 or 160x300 dp (density-independent pixels).

  4. After creating and configuring an instance of the BannerAdView class, you can set an AdEventListener on the ad object for tracking events (opening or closing the ad, exiting the app, and loading the ad successfully or unsuccessfully).

Loading ads

Note.

Any call of the Mobile Ads SDK should be made from the main thread.

After creating and configuring the BannerAdView class object, load the ads. To load an ad, use the loadAd method, which takes the AdRequest object as a parameter (or Builder, which optionally accepts ad targeting data).

Example of working with banner ads

The following code demonstrates creating and configuring the AdView object, registering a listener, and loading a banner:

...
<LinearLayout>
    ...
    <com.yandex.mobile.ads.banner.BannerAdView
        android:id="@+id/banner_ad_view"
        android:layout_width=“match_parent”
        android:layout_height="wrap_content"/>
</LinearLayout>
...
private BannerAdView mBannerAdView;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        ...
        // Creating an mAdView instance.
        mBannerAdView = (BannerAdView) findViewById(R.id.banner_ad_view);
        mBannerAdView.setAdUnitId(AdUnitId);
        mBannerAdView.setAdSize(AdSize.stickySize(maxBannerWidthDp));

        // Creating an ad targeting object.
        final AdRequest adRequest = new AdRequest.Builder().build();

        // Registering a listener for tracking events in the banner ad.
        mBannerAdView.setAdEventListener(new BannerAdEventListener() {
            @Override
            public void onAdLoaded() {
                ...
            }
            
            @Override
            public void onAdFailedToLoad(AdRequestError adRequestError) {
                ...
            }

            @Override
            public void onLeftApplication() {
                ...
            }

            @Override
            public void onReturnedToApplication() {
                ...
            }
        });

        // Loading ads.
        mBannerAdView.loadAd(adRequest);
    }
}

If an ad is integrated this way, the banner appears after the app starts:

To see how the banner ad will be displayed in the app, use a demo AdUnitId:
  • demo-banner-yandex