Enabling banner ads

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.


    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.

    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:

public class BannerExample extends Activity {
    private static final String blockId = "Your_blockId";
    private AdView mAdView;
    public void onCreate(Bundle savedInstanceState) {
        // Creating an instance of mAdView.
        mAdView = (AdView) findViewById(R.id.banner_view);

        // 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() {
            public void onAdLoaded() {

        // Loading ads.

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 blockId:
  • R-M-DEMO-300x250
  • R-M-DEMO-300x250-context
  • R-M-DEMO-300x300-context
  • R-M-DEMO-320x50
  • R-M-DEMO-320x50-app_install
  • R-M-DEMO-320x100-context
  • R-M-DEMO-728x90