Adaptive banners

Adaptive banners are banners that fit seamlessly into user-defined block sizes. Depending on how an adaptive banner is integrated, the optimal height is determined for the given width or the specified size of ad placement is used.

Note.

You can read about creating an ad block for an adaptive banner in the Advertising Network Help.

  1. Types of adaptive banners
  2. Creating Adview
  3. Loading ads
  4. Example of working with adaptive banners

Types of adaptive banners

Banner with set width

Features:

  1. An alternative to 320x50 banners (when determining the banner height, the aspect ratio of 320x50 is maintained).
  2. The banner is fixed in place at the top or bottom of the screen (set up in the app).
  3. The given banner width is used instead of the device screen width. This lets you take into account the display's features.
  4. The width of adaptive banners is set using the stickySize method.

Examples of displaying adaptive banners:

Banner with set width and height

Features:

  1. An adaptive banner fills up the entire block using the given width and height.
  2. The width and height of adaptive banners is set with the flexibleSize method.

Examples of displaying adaptive banners:

Creating Adview

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

    // Using an XML file to create an instance of mAdView.
            mAdView = (AdView) findViewById(R.id.banner_view);
    
    // Programmatically creating an instance of mAdView.
            mAdView = new AdView(this);
  2. Set the blockId using the setBlockId method.

    mAdView.setBlockId(blockId);

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

  3. Set the banner size using the setAdSize method.

    Banner with set width

    To set the width of an adaptive banner, call the stickySize method.

    mAdView.setAdSize(AdSize.stickySize(AdSize.FULL_WIDTH));
    Banner with set width and height

    To set the width and height of an adaptive banner, call the flexibleSize method.

    mAdView.setAdSize(AdSize.flexibleSize(width, height));
  4. After creating and configuring an instance of the AdView 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

After creating and configuring an instance of the AdView class, you need to load an ad. 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 adaptive banners

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

...
<LinearLayout>
    ...
    <com.yandex.mobile.ads.AdView
        android:id="@+id/ad_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
</LinearLayout>
...
...
public class BannerExample extends Activity {
    ...
    private static final String blockId = "Your_blockId";
    private AdView mAdView;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        ...
        // Creating an mAdView instance.
        mAdView = (AdView) findViewById(R.id.banner_view);
        mAdView.setBlockId(blockId);
        mAdView.setAdSize(AdSize.stickySize(AdSize.FULL_WIDTH));

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

        // Registering a listener for tracking events in the banner ad.
        mAdView.setAdEventListener(new AdEventListener.SimpleAdEventListener() {
            @Override
            public void onAdLoaded() {
                ...
            }
        });

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