Skip to content

Commit 116d1b7

Browse files
Nicholas Ventimigliacopybara-github
authored andcommitted
Fixed #913 Jetpack Compose Native AdsClicks Not Working / Missing setNativeAd().
PiperOrigin-RevId: 813345246
1 parent b28b278 commit 116d1b7

File tree

2 files changed

+8
-2
lines changed
  • kotlin/advanced/JetpackComposeDemo
    • app/src/main/java/com/google/android/gms/example/jetpackcomposedemo/formats
    • compose-util/src/main/java/com/google/android/gms/compose_util

2 files changed

+8
-2
lines changed

kotlin/advanced/JetpackComposeDemo/app/src/main/java/com/google/android/gms/example/jetpackcomposedemo/formats/NativeScreen.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ fun DisplayNativeAdView(nativeAd: NativeAd) {
134134
val context = LocalContext.current
135135
Box(modifier = Modifier.padding(8.dp).wrapContentHeight(Alignment.Top)) {
136136
// Call the NativeAdView composable to display the native ad.
137-
NativeAdView {
137+
NativeAdView(nativeAd) {
138138
// Inside the NativeAdView composable, display the native ad assets.
139139
Column(Modifier.align(Alignment.TopStart).wrapContentHeight(Alignment.Top)) {
140140
// Display the ad attribution.

kotlin/advanced/JetpackComposeDemo/compose-util/src/main/java/com/google/android/gms/compose_util/NativeAdView.kt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,16 @@ internal val LocalNativeAdView = staticCompositionLocalOf<NativeAdView?> { null
4444
/**
4545
* This is the Compose wrapper for a NativeAdView.
4646
*
47+
* @param nativeAd The `NativeAd` object containing the ad assets to be displayed in this view.
4748
* @param modifier The modifier to apply to the native ad.
4849
* @param content A composable function that defines the rest of the native ad view's elements.
4950
*/
5051
@Composable
51-
fun NativeAdView(modifier: Modifier = Modifier, content: @Composable () -> Unit) {
52+
fun NativeAdView(
53+
nativeAd: NativeAd,
54+
modifier: Modifier = Modifier,
55+
content: @Composable () -> Unit,
56+
) {
5257
val localContext = LocalContext.current
5358
val nativeAdView = remember { NativeAdView(localContext).apply { id = View.generateViewId() } }
5459

@@ -81,6 +86,7 @@ fun NativeAdView(modifier: Modifier = Modifier, content: @Composable () -> Unit)
8186
},
8287
modifier = modifier,
8388
)
89+
SideEffect { nativeAdView.setNativeAd(nativeAd) }
8490
}
8591

8692
/**

0 commit comments

Comments
 (0)