Customize error state

There is a way to customize how FuseAdView behaves if the ad fails to load.

You can provide errorBehaviour parameter when creating a new instance of FuseAdView.

Display Placeholder Image

When this option is selected, a provided image is displayed in the middle of the view if an error occurs while loading an ad.

NOTE: If the image is bigger than FuseAdView, it will be adjusted to fit the bounds, if it is smaller - it will be rendered as is.

Android:

val image = AppCompatResources.getDrawable(context, <image_resource>)
val adView = FuseAdView(
    context,
    code = "zone_code",
    errorBehaviour = FuseAdViewBehaviour.Background(image),
)

iOS:

let image = UIImage(named: "<image_resource>")
let adView = FuseAdView(
    code: "zone_code",
    errorBehaviour: .background(image: image)
)

Hide On Error

When this option is selected, a view will be completely hidden if an error occurs while loading an ad.

val adView = FuseAdView(
    context,
    code = "zone_code",
    errorBehaviour = FuseAdViewBehaviour.Hide,
)

iOS:

let adView = FuseAdView(
    code: "zone_code",
    errorBehaviour: .hide,
)

Display Custom View

When this option is selected, a provided closure is used to build an arbitrary view which is then placed inside FuseAdView.

val errorBehaviour = FuseAdViewBehaviour.CustomView(
    viewBuilder = { ctx, parent ->
        TextView(ctx).apply {
            text = "Loading"
            layoutParams = ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT,
            )
        }
    },
)
val adView = FuseAdView(
    context,
    code = "zone_code",
    errorBehaviour = errorBehaviour,
)

// Compose
ComposeFuseAdView(
    code = "zone_code",
    errorBehaviour = errorBehaviour,
)

NOTE: layoutParams will define how custom view will be laid out inside FuseAdView. Irrespective of custom view size, the height of FuseAdView will not change and will remain based on the requested ad sizes, unless the size is dynamic (fluid or native). In this case FuseAdView will adopt the height of the custom view provided.

iOS:

private func buildCustomView() -> UIView {
    let label = UILabel()
    label.text = "Loading"
    label.textAlignment = .center
    return label
}

let adView = FuseAdView(
    code: "zone_code",
    errorBehaviour: .customView(viewBuilder: buildCustomView)
)

// SwiftUI
FuseAdViewRepresentable(
    code: "zone_code",
    errorBehaviour: .customView(viewBuilder: buildCustomView)
)

NOTE: On iOS the custom view is pinned to all the sides of FuseAdView. If the zone uses fixed ad sizes, then FuseAdView will adopt the height of the largest ad size and the custom view will be centred inside it. If the zone uses dynamic ad sizes (fluid or native) then FuseAdView will adopt the height of the custom view provided.

Global Settings

errorBehaviour parameter is optional. If it is not provided, default value will be taken from the FuseSDK object.

If you want to provide default behaviour which would be applied to all FuseAdViews across the app, set the following value on the FuseSDK object:

Android:

FuseSDK.defaultErrorBehaviour = FuseAdViewBehaviour.Hide

iOS:

FuseSDK.shared.defaultErrorBehaviour = .hide

If this value is not provided, it will default to do nothing.