Banner ads

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

To enable a banner:

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);
    Block ID is a unique identifier in the adf-XXXXXX/YYYYYY format, which is assigned in the Partner interface.
    Attention.

    In the mobile mediation interface, the Block ID is called the location ID.

  3. Set the banner size using the setAdSize method.

    mAdView.setAdSize(AdSize.BANNER_320x50);
    Restriction. Requirements for banner size when displaying video ads

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

    The following banner sizes are supported:

    Size Device AdSize constant
    320 x 50 Phones and tablets BANNER_320x50
    320 x 100 Phones and tablets BANNER_320x100
    300 x 250 Phones and tablets BANNER_300x250
    300 x 300 Phones and tablets BANNER_300x300

    240 x 400

    Phones and tablets BANNER_240x400

    400 x 240

    Phones and tablets BANNER_400x240

    728 x 90

    Tablets BANNER_728x90
  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 banner ads

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

...
<LinearLayout>
    ...
    <com.yandex.mobile.ads.AdView
        android:id="@+id/banner_view"
        android:layout_width="match_parent"
        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 a mAdView instance.
        mAdView = (AdView) findViewById(R.id.banner_view);
        mAdView.setBlockId(blockId);
        mAdView.setAdSize(AdSize.BANNER_320x50);

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

        // Register a listener to track events that occur in a banner ad.
        mAdView.setAdEventListener(new AdEventListener.SimpleAdEventListener() {
            @Override
            public void onAdLoaded() {
                ...
            }
        });

        // Loading the ad.
        mAdView.loadAd(adRequest);
    }
}

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