Links

Banner Ads

This guide shows how to display Banner Ads in your application.

What are Banner Ads?

Banner Ad Units display a rectangular creative within an app. They occupy a portion of the app layout and stay on the screen as the users interact with the app.

How to Display Banner Ads

Integrate & Initialize HyprMX

Before you start, make sure you have correctly integrated and initialized the HyprMX SDK. To integrate and initialize HyprMX, see our Setup Guide.

Initialize a BannerView

BannerViews are initialized with their placement name and ad size.

Placement Name

HyprMX utilizes placements to load and display an ad. Placements are used to represent a point in your application's workflow where you'd like to show an ad and placement names are a string that identify individual banner placements in your application.
If you don't have your placement information, please reach out to your account manager to configure your banner placements. In return, they will provide the names of the new placements.

Ad Size

Banners are initialized using one of the pre-defined ad sizes.
Constant
Size
HyprMXAdSizeShort
300x50
HyprMXAdSizeBanner
320x50
HyprMXAdSizeMediumRectangle
300x250
HyprMXAdSizeLeaderboard
728x90
HyprMXAdSizeSkyScraper
160x600
To initialize your banner view you can use XML, code integration, or a combination of the two.
Code Initialization
When constructing a new banner view, the activity context must be used.
Java
Kotlin
HyprMXBannerView bannerView = new HyprMXBannerView(activityContext, null, "placement", HyprMXBannerSize.HyprMXAdSizeBanner.INSTANCE);
val bannerView = HyprMXBannerView(context = activityContext, placementName = "placementName", adSize = HyprMXBannerSize.HyprMXAdSizeBanner)
XML Initialization
The banner view can be initialized in xml as seen below with the attributes app:hyprMXAdSize and app:hyprMXPlacementName.
XML
<com.hyprmx.android.sdk.banner.HyprMXBannerView
android:id="@+id/bannerView"
android:layout_width="320dp"
android:layout_height="50dp"
android:layout_margin="16dp"
app:hyprMXAdSize="HyprMXAdSizeBanner"
app:hyprMXPlacementName="placementName" />
Code Setting of Name/Size
Java
Kotlin
bannerView.setPlacementName("placementName");
bannerView.setAdSize(HyprMXBannerSize.HyprMXAdSizeBanner.INSTANCE);
bannerView.placementName = "placementName"
bannerView.adSize = HyprMXBannerSize.HyprMXAdSizeBanner

Adding Banners to your View

Add your BannerView to your View using XML or code and configure its layout to match your requested ad size.
Example Using XML:
XML
<com.hyprmx.android.sdk.banner.HyprMXBannerView
android:id="@+id/bannerView"
android:layout_width="320dp"
android:layout_height="50dp"
android:layout_margin="16dp"
app:hyprMXAdSize="HyprMXAdSizeBanner"
app:hyprMXPlacementName="placementName" />
Example using Code:
Java
Kotlin
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(320, 50);
params.gravity = Gravity.CENTER_HORIZONTAL;
bannerView.setLayoutParams(params);
linearLayout.addView(bannerView);
val linearLayout:LinearLayout = findViewById(R.id.example_linear_layout)
bannerView.apply {
val params = LinearLayout.LayoutParams(320, 50)
params.gravity = Gravity.CENTER_HORIZONTAL
layoutParams = params
}
linearLayout.addView(bannerView)

Loading Advertisements

To load a banner ad:
Java
Kotlin
bannerView.loadAd();
bannerView.loadAd()
Your bannerView will load an ad and render it in its view. To receive ad load success and other lifecycle callbacks, implement the HyprMXBannerListener and set the listener on your bannerView.

Setting a HyprMXBannerListener (Optional)

To receive lifecycle callbacks on your HyprMXBannerView, implement the HyprMXBannerListener.
Java
Kotlin
bannerView.setListener(listener);
bannerView.listener = listener
Implement the HyprMXBannerListener:
Java
Kotlin
/**
* The ad has successfully been loaded
*/
@Override
public void onAdLoaded(@NotNull HyprMXBannerView ad) {}
/**
* The ad failed to load
*
* @param ad The HyprMXBannerView that loaded the ad
* @param error Error indicating why the ad failed to load
*/
@Override
public void onAdFailedToLoad(@NotNull HyprMXBannerView ad, @NotNull HyprMXErrors error) {}
/**
* A click event has occurred on the banner
* @param ad The HyprMXBannerView that had the click event
*/
@Override
public void onAdClicked(@NotNull HyprMXBannerView ad) {}
/**
* The click event on the banner has transition to an overlay
* @param ad The HyprMXBannerView that had the click event
*/
@Override
public void onAdOpened(@NotNull HyprMXBannerView ad) {}
/**
* The opened overlay has been dismissed
* @param ad The HyprMXBannerView that triggered the overlay
*/
@Override
public void onAdClosed(@NotNull HyprMXBannerView ad) {}
/**
* The click event on the banner will open a third party application to
* process the click
*
* @param ad The HyprMXBannerView that had the click event
*/
@Override
public void onAdLeftApplication(@NotNull HyprMXBannerView ad) {}
/**
* The ad has successfully been loaded
*/
fun onAdLoaded(ad: HyprMXBannerView)
/**
* The ad failed to load
*
* @param ad The HyprMXBannerView that loaded the ad
* @param error Error indicating why the ad failed to load
*/
fun onAdFailedToLoad(ad: HyprMXBannerView, error: HyprMXErrors)
/**
* A click event has occurred on the banner
* @param ad The HyprMXBannerView that had the click event
*/
fun onAdClicked(ad: HyprMXBannerView)
/**
* The click event on the banner has transition to an overlay
* @param ad The HyprMXBannerView that had the click event
*/
fun onAdOpened(ad: HyprMXBannerView)
/**
* The opened overlay has been dismissed
* @param ad The HyprMXBannerView that triggered the overlay
*/
fun onAdClosed(ad: HyprMXBannerView)
/**
* The click event on the banner will open a third party application to
* process the click
*
* @param ad The HyprMXBannerView that had the click event
*/
fun onAdLeftApplication(ad: HyprMXBannerView)

Updating Ad Size / Reloading

You can reload another banner at any time by calling loadAd() on the bannerView. You can also update your adSize before loading a new ad by updating the bannerView.adSize property.

Destroying

When the hosting Activity or Fragment is destroyed, or you are done with the banner view and it is no longer visible and attached, be sure to also destroy the HyprMXBannerView by calling:
Java
Kotlin
bannerView.destroy();
bannerView.destroy()