Enabling banner ads
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.
Banner types
Features:
- The specified banner width is used. The height is selected automatically.
- The width of banners is set using the stickySize method.
- The banner height shouldn't exceed 15% of the device height and should be at least 50 dp.
Examples of displaying banners:
Features:
- A banner fills up the entire unit using the set maximum sizes.
- The width and height of a banner is set using the flexibleSize(int width, int height) method.
Examples of displaying banners:
Enabling a banner
Creating BannerAdview
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);
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.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));
To set the width and height of a banner, call the flexibleSize(int width, int height) method.
mBannerAdView.setAdSize(AdSize.flexibleSize(width, height));
Restriction. Banner size requirements when displaying video adsMinimum size of a banner that supports video playback is 300x160 or 160x300 dp (density-independent pixels).
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
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>
...
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(maxBannerWidthDp));
// Creating an ad targeting object.
final AdRequest adRequest = new AdRequest.Builder().build();
// Registering a listener for tracking events in the banner ad.
mBannerAdView.setAdEventListener(new BannerAdEventListener() {
@Override
public void onAdLoaded() {
...
}
@Override
public void onAdFailedToLoad(AdRequestError adRequestError) {
...
}
@Override
public void onLeftApplication() {
...
}
@Override
public void onReturnedToApplication() {
...
}
});
// Loading ads.
mBannerAdView.loadAd(adRequest);
}
}
If an ad is integrated this way, the banner appears after the app starts:
- demo-banner-yandex