I've been finding sample apps on how to display native ads on compose, but i couldn't find one. Any solutions?
How to display Native ads via Jetpack compose?
Asked Answered
Use AndroidViewBinding
to wrap NativeAdView
.
// build.gradle.kts
dependencies {
implementation("androidx.compose.ui:ui-viewbinding:1.2.0-beta03")
}
Create ViewBinding which contains NativeAdView.
@Composable
fun NativeMediumAd() {
AndroidViewBinding(factory = LayoutNativeAdBinding::inflate) {
// Initialize NativeAdView
val adView = root.also { adView ->
adView.advertiserView = tvAdvertiser
adView.bodyView = tvBody
adView.callToActionView = btnCta
adView.headlineView = tvHeadline
adView.iconView = ivAppIcon
adView.priceView = tvPrice
adView.starRatingView = rtbStars
adView.storeView = tvStore
adView.mediaView = mvContent
}
// Request Ad
val adLoader = AdLoader.Builder(adView.context, "NATIVE_AD_KEY")
.forNativeAd { nativeAd ->
nativeAd.advertiser?.let { advertiser ->
tvAdvertiser.text = advertiser
}
nativeAd.body?.let { body ->
tvBody.text = body
}
nativeAd.callToAction?.let { cta ->
btnCta.text = cta
}
nativeAd.headline?.let { headline ->
tvHeadline.text = headline
}
nativeAd.icon?.let { icon ->
ivAppIcon.setImageDrawable(icon.drawable)
}
nativeAd.price?.let { price ->
tvPrice.text = price
}
nativeAd.starRating?.let { rating ->
rtbStars.rating = rating.toFloat()
}
nativeAd.store?.let { store ->
tvStore.text = store
}
adView.setNativeAd(nativeAd)
})
.withNativeAdOptions(NativeAdOptions.Builder().build())
.build()
adLoader.loadAd(AdRequest.Builder().build())
}
}
Full source code:
The links are dead. COuld you please share the working ones? Thanks. –
Couchant
@MarkDelphi here's the valid link: github.com/yoonseopshin/kicpa-wordquiz/blob/develop/core/… –
Dunlin
@THank you so much! –
Couchant
You should use AndroidView
to wrap AdMob AdView
From the docs:
To include a view element or hierarchy, use the
AndroidView
composable.AndroidView
is passed a lambda that returns a View.AndroidView
also provides an update callback that is called when the view is inflated. TheAndroidView
recomposes whenever aState
read within the callback changes.
AndroidView(
factory = { context: Context ->
AdView(context).apply {
// config AdView
}
},
update = { adview ->
}
)
I try to integrate facebook native ads in my compose but ads visible on second launch not in first launch –
Inventory
© 2022 - 2024 — McMap. All rights reserved.