Banner Ads

Initialize HyprMX

To integrate and Initialize HyprMX, see our Setup Guide.

Initialize a BannerView

BannerViews are initialized with their placement name and ad size.

Placement Name

Placement names are a string that identify individual banner placements in your application. 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 can be 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
Java
Kotlin
1
HyprMXBannerView bannerView = new HyprMXBannerView(context, null, "placement", HyprMXBannerSize.HyprMXAdSizeBanner.INSTANCE);
Copied!
1
val bannerView = HyprMXBannerView(context = context, placementName = "placementName", adSize = HyprMXBannerSize.HyprMXAdSizeBanner)
Copied!
XML Initialization
The banner view can be initialized in xml as seen below. The attributes app:hyprMXAdSize and app:hyprMXPlacementName
XML
1
<com.hyprmx.android.sdk.banner.HyprMXBannerView
2
android:id="@+id/bannerView"
3
android:layout_width="320dp"
4
android:layout_height="50dp"
5
android:layout_margin="16dp"
6
app:hyprMXAdSize="HyprMXAdSizeBanner"
7
app:hyprMXPlacementName="placementName" />
Copied!
Code Setting of Name/Size
Jave
Kotlin
1
bannerView.setPlacementName("placementName");
2
bannerView.setAdSize(HyprMXBannerSize.HyprMXAdSizeBanner.INSTANCE);
Copied!
1
bannerView.placementName = "placementName"
2
bannerView.adSize = HyprMXBannerSize.HyprMXAdSizeBanner
Copied!

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
1
<com.hyprmx.android.sdk.banner.HyprMXBannerView
2
android:id="@+id/bannerView"
3
android:layout_width="320dp"
4
android:layout_height="50dp"
5
android:layout_margin="16dp"
6
app:hyprMXAdSize="HyprMXAdSizeBanner"
7
app:hyprMXPlacementName="placementName" />
Copied!
Example using Code:
Java
Kotlin
1
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(320, 50);
2
params.gravity = Gravity.CENTER_HORIZONTAL;
3
bannerView.setLayoutParams(params);
4
linearLayout.addView(bannerView);
Copied!
1
val linearLayout:LinearLayout = findViewById(R.id.example_linear_layout)
2
bannerView.apply {
3
val params = LinearLayout.LayoutParams(320, 50)
4
params.gravity = Gravity.CENTER_HORIZONTAL
5
layoutParams = params
6
}
7
linearLayout.addView(bannerView)
Copied!

Loading Advertisements

To load a banner ad:
Java
Kotlin
1
bannerView.loadAd();
Copied!
1
bannerView.loadAd()
Copied!
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
1
bannerView.setListener(listener);
Copied!
1
bannerView.listener = listener
Copied!
Implement the HyprMXBannerListener:
Java
Kotlin
1
/**
2
* The ad has successfully been loaded
3
*/
4
@Override
5
public void onAdLoaded(@NotNull HyprMXBannerView ad) {}
6
7
/**
8
* The ad failed to load
9
*
10
* @param ad The HyprMXBannerView that loaded the ad
11
* @param error Error indicating why the ad failed to load
12
*/
13
@Override
14
public void onAdFailedToLoad(@NotNull HyprMXBannerView ad, @NotNull HyprMXErrors error) {}
15
16
/**
17
* A click event has occurred on the banner
18
* @param ad The HyprMXBannerView that had the click event
19
*/
20
@Override
21
public void onAdClicked(@NotNull HyprMXBannerView ad) {}
22
23
/**
24
* The click event on the banner has transition to an overlay
25
* @param ad The HyprMXBannerView that had the click event
26
*/
27
@Override
28
public void onAdOpened(@NotNull HyprMXBannerView ad) {}
29
30
/**
31
* The opened overlay has been dismissed
32
* @param ad The HyprMXBannerView that triggered the overlay
33
*/
34
@Override
35
public void onAdClosed(@NotNull HyprMXBannerView ad) {}
36
37
/**
38
* The click event on the banner will open a third party application to
39
* process the click
40
*
41
* @param ad The HyprMXBannerView that had the click event
42
*/
43
@Override
44
public void onAdLeftApplication(@NotNull HyprMXBannerView ad) {}
Copied!
1
/**
2
* The ad has successfully been loaded
3
*/
4
fun onAdLoaded(ad: HyprMXBannerView)
5
6
/**
7
* The ad failed to load
8
*
9
* @param ad The HyprMXBannerView that loaded the ad
10
* @param error Error indicating why the ad failed to load
11
*/
12
fun onAdFailedToLoad(ad: HyprMXBannerView, error: HyprMXErrors)
13
14
/**
15
* A click event has occurred on the banner
16
* @param ad The HyprMXBannerView that had the click event
17
*/
18
fun onAdClicked(ad: HyprMXBannerView)
19
20
/**
21
* The click event on the banner has transition to an overlay
22
* @param ad The HyprMXBannerView that had the click event
23
*/
24
fun onAdOpened(ad: HyprMXBannerView)
25
26
/**
27
* The opened overlay has been dismissed
28
* @param ad The HyprMXBannerView that triggered the overlay
29
*/
30
fun onAdClosed(ad: HyprMXBannerView)
31
32
/**
33
* The click event on the banner will open a third party application to
34
* process the click
35
*
36
* @param ad The HyprMXBannerView that had the click event
37
*/
38
fun onAdLeftApplication(ad: HyprMXBannerView)
39
Copied!

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
1
bannerView.destroy();
Copied!
1
bannerView.destroy()
Copied!
Last modified 1d ago