A banner is a rectangular ad format with a predefined size, typically inserted at the bottom of the screen, within content, or in a content feed. The RTB-Stack SDK supports both HTML and VAST video content for banner ads.
Setting up a banner ad in your Android application involves two main steps:
BannerAdView to Layout.Let's explore the details of each step:
The BannerAdView class is used to display banner ads within your Android application. This view can be added to your existing layout either programmatically or via XML. Before implementation, ensure you have a Tag ID, which is required for displaying ads. The Tag ID can be any string, such as "Display_300x250", and can later be used in reporting to filter and analyze ad performance.
Adding BannerAdView Using XML: Insert the BannerAdView XML code into your layout file. Here is an example of how you might configure it:
<com.rtbstack.sdk.BannerAdView
android:id="@+id/banner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:tag_id="YOUR TAG ID"
android:auto_refresh_interval="30"
android:opens_native_browser="true"
android:adWidth="320"
android:adHeight="50"
android:should_reload_on_resume="true"
android:expands_to_fit_screen_width="false" />
Adding BannerAdView Programmatically:
RelativeLayout rl = (RelativeLayout)(findViewById(R.id.mainview));
BannerAdView av = new BannerAdView(this);
LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, 100);
av.setAdSize(320, 50);
av.setLayoutParams(lp);
av.setTagID("12345");
rl.addView(av);
av.loadAd();
Important Notes:
BannerAdView properties such as "adWidth", "adHeight", and "tag_id" are correctly set to match the specifications provided by your ad network.auto_refresh_interval property controls how often the ad refreshes. Set it according to your needs or as advised by your ad network.should_reload_on_resume property ensures that the ad reloads when the app resumes from the background, which can be useful for keeping ads up-to-date.Here is a basic example of how to load a banner ad in the onCreate() method of an Activity:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SDKSettings.setRequestBaseUrl("Your endpoint request url");
BannerAdView adView = new BannerAdView(this);
adView.setTagID("Your custom tag id");
adView.setAdSize(300, 250);
adView.loadAd();
LinearLayout container = findViewById(R.id.container);
container.addView(adView);
}
Important Notes:
To receive events from a banner, set the AdListener:
adView.setAdListener(new AdListener() {
@Override
public void onAdLoaded(AdView adView) {
}
@Override
public void onAdLoaded(NativeAdResponse nativeAdResponse) {
}
@Override
public void onAdRequestFailed(AdView adView, ResultCode resultCode) {
}
@Override
public void onAdExpanded(AdView adView) {
}
@Override
public void onAdCollapsed(AdView adView) {
}
@Override
public void onAdClicked(AdView adView) {
}
@Override
public void onAdClicked(AdView adView, String s) {
}
});
BannerAdView allows configuring the following options:
ArrayList adSizes = new ArrayList<>();
adSizes.add(new AdSize(320, 50));
adSizes.add(new AdSize(300, 250));
adView.setAdSizes(adSizes);
// open url in in-app browser(WebView)
adView.setClickThroughAction(ANClickThroughAction.OPEN_SDK_BROWSER);
// open url in device browser
adView.setClickThroughAction(ANClickThroughAction.OPEN_DEVICE_BROWSER);
// return url in AdListener.onAdClicked() callback
adView.setClickThroughAction(ANClickThroughAction.RETURN_URL);
adView.setResizeAdToFitContainer(true);
// enable auto refresh
adView.setAutoRefresh(true);
// disable auto refresh
adView.setAutoRefresh(false);
adView.setAutoRefreshInterval(15000);
TransitionType class:bav.setTransitionType(TransitionType.FADE);
bav.setTransitionDuration(1500);
onAdLoaded and onAdRequestFailed callbacks. If set to true, then the first request triggers an onAdLoaded callback but the next auto refresh requests will not trigger an onAdLoaded callback. If set to true and the first request triggers an onAdRequestFailed, then the next auto refresh requests will trigger onAdRequestFailed until onAdLoaded is called. If set to false, then the first and next auto refresh requests will trigger onAdLoaded or onAdRequestFailed callbacks:bav.setShouldCallEventsOnAutoRefresh(false)