Banner Ad format

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 Banner Ads

Setting up a banner ad in your Android application involves two main steps:

  1. Add BannerAdView to Layout.
  2. Load the Banner ad.

Let's explore the details of each step:

Step 1: Add BannerAdView to Layout

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:

  • Ensure that the BannerAdView properties such as "adWidth""adHeight", and "tag_id" are correctly set to match the specifications provided by your ad network.
  • The auto_refresh_interval property controls how often the ad refreshes. Set it according to your needs or as advised by your ad network.
  • The 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.

Step 2: Load Ad

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:

  • Endpoint Request URL: Obtain this from the Edit Endpoint window as described in the following article.
  • Custom Tag ID: Use a random string value as a Tag ID for reporting purposes.

Listen for Events

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) {
   }
});

Banner Options

BannerAdView allows configuring the following options:

  • Define multiple supported sizes:
ArrayList adSizes = new ArrayList<>();
adSizes.add(new AdSize(320, 50));
adSizes.add(new AdSize(300, 250));
adView.setAdSizes(adSizes);
  • Open an ad’s URL in an in-app browser, device browser, or return URL:
// 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);
  • Resize a banner view to fit a container:
adView.setResizeAdToFitContainer(true);
  • Enable or disable ad auto-refresh:
// enable auto refresh
adView.setAutoRefresh(true);
// disable auto refresh
adView.setAutoRefresh(false);
  • Set auto-refresh interval in milliseconds:
adView.setAutoRefreshInterval(15000);
  • Add appearing animation. There are NONE, RANDOM, FADE, PUSH, MOVEIN, and REVEAL animations in the TransitionType class:
bav.setTransitionType(TransitionType.FADE);
bav.setTransitionDuration(1500);
  • Set whether or not auto refresh requests should trigger 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)
Updated on October 30, 2024