Android Jetpack Compose Example
FuseAdView
can be used in Compose either by creating a custom wrapper or using ComposeFuseAdView
provided by SDK.
Simple use of ComposeFuseAdView
@Composable
fun CustomView(code: String) {
LazyColumn(
horizontalAlignment = Alignment.CenterHorizontally,
) {
item {
Text("Fuse ad view", style = MaterialTheme.typography.headlineLarge)
Spacer(Modifier.height(8.dp))
ComposeFuseAdView(code)
Spacer(Modifier.height(8.dp))
Text("Some other content", style = MaterialTheme.typography.bodyMedium)
}
}
}
Advanced use of ComposeFuseAdView
ComposeFuseAdView
has several extra parameters which can be provided to modify default behaviour:
loadingBehaviour
- modifies behaviour ofComposeFuseAdView
when a new ad is loading (more information).errorBehaviour
- modifies behaviour ofComposeFuseAdView
when there is a loading error (more information).params
- extra parameters including custom targeting, content mapping or publisher provided identifiers.
@Composable
fun CustomView(code: String) {
var params = FuseAdViewParams(publisherProvidedId = "ppid")
LazyColumn(
horizontalAlignment = Alignment.CenterHorizontally,
) {
item {
Text("Fuse ad view", style = MaterialTheme.typography.headlineLarge)
Spacer(Modifier.height(8.dp))
ComposeFuseAdView(
code = code,
loadingBehaviour = FuseAdViewBehaviour.ProgressBar,
errorBehaviour = FuseAdViewBehaviour.Hide,
params = params,
modifier = Modifier.border(1.dp, Color.Gray),
)
Spacer(Modifier.height(8.dp))
Text("Some other content", style = MaterialTheme.typography.bodyMedium)
}
}
}
Custom wrapper
Example of creating a custom wrapper for FuseAdView
@Composable
fun FuseAdViewComposable(
code: String,
modifier: Modifier = Modifier,
lifecycleOwner: LifecycleOwner = LocalLifecycleOwner.current,
loadingBehaviour: FuseAdViewBehaviour = FuseSDK.defaultLoadingBehaviour,
errorBehaviour: FuseAdViewBehaviour = FuseSDK.defaultErrorBehaviour,
params: FuseAdViewParams? = null,
) {
key(code, loadingBehaviour, errorBehaviour, params) {
var adView: FuseAdView? = null
DisposableEffect(lifecycleOwner) {
onDispose {
adView?.destroy()
}
}
AndroidView(
modifier = modifier,
factory = { context ->
FuseAdView(
context,
code = code,
loadingBehaviour = loadingBehaviour,
errorBehaviour = errorBehaviour,
params = params,
).also {
adView = it
}
},
update = {
adView = it
},
)
}
}