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 Block ID using the setBlockId method.

    mAdView.setBlockId(BLOCK_ID);

    Block ID 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.

    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 an object of the AdRequest class as an argument:
mAdView.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 the Help for ADFOX Mobile):
// 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.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 BLOCK_ID = "YOUR_BLOCK_ID";
    private AdView mAdView;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        ...
        // Creating an mAdView instance.
        mAdView = (AdView) findViewById(R.id.banner_view);
        mAdView.setBlockId(BLOCK_ID);
        mAdView.setAdSize(AdSize.BANNER_320x50);

        // 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 the ad targeting object.
        final AdRequest adRequest = AdRequest.builder().withParameters(parameters).build();

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

        // Loading the ad.
        mAdView.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 Block ID:
  • 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