Banner ads

Warning.

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

A banner is a configurable ad that covers part of the screen and reacts to clicks.

  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 an instance of the AdView class, you need to load an ad.

To load an ad, use the loadAd method, which takes an object of the AdRequest class as an argument:
mBannerAdView.loadAd(adRequest);
Copied to clipboard

About loading ads

Use the AdRequest object to transmit the code received in the Adfox interface (for more information, see Help for Adfox):
// Code from the Adfox interface for working with direct campaigns.
Map<String, String> parameters = new HashMap<String, String>();
parameters.put("adf_ownerid", "example");
parameters.put("adf_p1", "example");
parameters.put("adf_p2", "example");
parameters.put("adf_pfc", "example");
parameters.put("adf_pfb", "example");
parameters.put("adf_plp", "example");
parameters.put("adf_pli", "example");
parameters.put("adf_pop", "example");
parameters.put("adf_pt", "example");
parameters.put("adf_pd", "example");
parameters.put("adf_pw", "example");
parameters.put("adf_pv", "example");
parameters.put("adf_prr", "example");
parameters.put("adf_pdw", "example");
parameters.put("adf_pdh", "example");
parameters.put("adf_puid1", "example");

final AdRequest adRequest = AdRequest.builder().withParameters(parameters).build();
Copied to clipboard

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>
...
public class BannerExample extends Activity {
    ...
    private static final String adUnitId = "YOUR_adUnitId";
    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(AdSize.FULL_WIDTH));
          
        // Code from the Adfox interface for working with direct campaigns.
        Map<String, String> parameters = new HashMap<String, String>();
        parameters.put("adf_ownerid", "example");
        parameters.put("adf_p1", "example");
        parameters.put("adf_p2", "example");
        parameters.put("adf_pfc", "example");
        parameters.put("adf_pfb", "example");
        parameters.put("adf_plp", "example");
        parameters.put("adf_pli", "example");
        parameters.put("adf_pop", "example");
        parameters.put("adf_pt", "example");
        parameters.put("adf_pd", "example");
        parameters.put("adf_pw", "example");
        parameters.put("adf_pv", "example");
        parameters.put("adf_prr", "example");
        parameters.put("adf_pdw", "example");
        parameters.put("adf_pdh", "example");
        parameters.put("adf_puid1", "example");
          
        // Creating an ad targeting object.
        final AdRequest adRequest = AdRequest.builder().withParameters(parameters).build();
          
        // Registering a listener for tracking events in the banner ad.
        mBannerAdView.setAdEventListener(new BannerAdEventListener() {
            @Override
            public void onAdLoaded() {
              ...
            }
        });
          
        // Loading ads.
        mBannerAdView.loadAd(adRequest);
    }
}   
Copied to clipboard

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