|
| 1 | +// Copyright 2025 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package com.google.android.gms.example.jetpackcomposedemo.snippets |
| 16 | + |
| 17 | +import androidx.compose.foundation.layout.Box |
| 18 | +import androidx.compose.foundation.layout.Column |
| 19 | +import androidx.compose.foundation.layout.fillMaxHeight |
| 20 | +import androidx.compose.foundation.layout.fillMaxWidth |
| 21 | +import androidx.compose.foundation.layout.height |
| 22 | +import androidx.compose.foundation.layout.padding |
| 23 | +import androidx.compose.foundation.layout.wrapContentHeight |
| 24 | +import androidx.compose.material3.MaterialTheme |
| 25 | +import androidx.compose.material3.Text |
| 26 | +import androidx.compose.runtime.Composable |
| 27 | +import androidx.compose.ui.Alignment |
| 28 | +import androidx.compose.ui.Modifier |
| 29 | +import androidx.compose.ui.platform.LocalContext |
| 30 | +import androidx.compose.ui.unit.dp |
| 31 | +import com.example.jetpackcomposedemo.R |
| 32 | +import com.google.android.gms.ads.nativead.NativeAd |
| 33 | +import com.google.android.gms.compose_util.NativeAdAttribution |
| 34 | +import com.google.android.gms.compose_util.NativeAdHeadlineView |
| 35 | +import com.google.android.gms.compose_util.NativeAdMediaView |
| 36 | +import com.google.android.gms.compose_util.NativeAdView |
| 37 | + |
| 38 | +/** Kotlin code snippets for the developer guide. */ |
| 39 | +internal class NativeAdSnippets { |
| 40 | + |
| 41 | + // [START define_native_ad_view] |
| 42 | + @Composable |
| 43 | + /** Display a native ad with a user defined template. */ |
| 44 | + fun DisplayNativeAdView(nativeAd: NativeAd) { |
| 45 | + val context = LocalContext.current |
| 46 | + Box(modifier = Modifier.padding(8.dp).wrapContentHeight(Alignment.Top)) { |
| 47 | + // Call the NativeAdView composable to display the native ad. |
| 48 | + NativeAdView { |
| 49 | + // Inside the NativeAdView composable, display the native ad assets. |
| 50 | + Column(Modifier.align(Alignment.TopStart).wrapContentHeight(Alignment.Top)) { |
| 51 | + // Display the ad attribution. This is required. |
| 52 | + NativeAdAttribution(text = context.getString(R.string.attribution)) |
| 53 | + // Display the headline asset. This is required. |
| 54 | + nativeAd.headline?.let { |
| 55 | + NativeAdHeadlineView { Text(text = it, style = MaterialTheme.typography.headlineLarge) } |
| 56 | + } |
| 57 | + // Display the media asset. This is required. |
| 58 | + NativeAdMediaView(Modifier.fillMaxWidth().height(500.dp).fillMaxHeight()) |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + // [END define_native_ad_view] |
| 65 | + |
| 66 | + // [START load_native_ad_for_display] |
| 67 | + @Composable |
| 68 | + fun loadAndDisplayNativeAd() { |
| 69 | + var nativeAd by remember { mutableStateOf<NativeAd?>(null) } |
| 70 | + val context = LocalContext.current |
| 71 | + var isDisposed by remember { mutableStateOf(false) } |
| 72 | + |
| 73 | + DisposableEffect(Unit) { |
| 74 | + // Load the native ad when we launch this screen |
| 75 | + loadNativeAd( |
| 76 | + context = context, |
| 77 | + onAdLoaded = { ad -> |
| 78 | + // Handle the native ad being loaded. |
| 79 | + if (!isDisposed) { |
| 80 | + nativeAd = ad |
| 81 | + } else { |
| 82 | + // Destroy the native ad if loaded after the screen is disposed. |
| 83 | + ad.destroy() |
| 84 | + } |
| 85 | + }, |
| 86 | + ) |
| 87 | + // Destroy the native ad to prevent memory leaks when we dispose of this screen. |
| 88 | + onDispose { |
| 89 | + isDisposed = true |
| 90 | + nativeAd?.destroy() |
| 91 | + nativeAd = null |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + // Display the native ad view with a user defined template. |
| 96 | + nativeAd?.let { adValue -> displayNativeAdView(adValue) } |
| 97 | + } |
| 98 | + |
| 99 | + fun loadNativeAd(context: Context, onAdLoaded: (NativeAd) -> Unit) { |
| 100 | + val adLoader = |
| 101 | + AdLoader.Builder(context, NATIVE_AD_UNIT_ID) |
| 102 | + .forNativeAd { nativeAd -> onAdLoaded(nativeAd) } |
| 103 | + .withAdListener( |
| 104 | + object : AdListener() { |
| 105 | + override fun onAdFailedToLoad(error: LoadAdError) { |
| 106 | + Log.e(TAG, "Native ad failed to load: ${error.message}") |
| 107 | + } |
| 108 | + |
| 109 | + override fun onAdLoaded() { |
| 110 | + Log.d(TAG, "Native ad was loaded.") |
| 111 | + } |
| 112 | + |
| 113 | + override fun onAdImpression() { |
| 114 | + Log.d(TAG, "Native ad recorded an impression.") |
| 115 | + } |
| 116 | + |
| 117 | + override fun onAdClicked() { |
| 118 | + Log.d(TAG, "Native ad was clicked.") |
| 119 | + } |
| 120 | + } |
| 121 | + ) |
| 122 | + .build() |
| 123 | + adLoader.loadAd(AdRequest.Builder().build()) |
| 124 | + } |
| 125 | + |
| 126 | + // [END load_native_ad_for_display] |
| 127 | + |
| 128 | + private companion object { |
| 129 | + // Test ad unit IDs. |
| 130 | + // For more information, |
| 131 | + // see https://developers.google.com/admob/android/test-ads. |
| 132 | + // and https://developers.google.com/ad-manager/mobile-ads-sdk/android/test-ads. |
| 133 | + const val AD_UNIT_ID = "ca-app-pub-3940256099942544/2247696110" |
| 134 | + const val VIDEO_AD_UNIT_ID = "ca-app-pub-3940256099942544/1044960115" |
| 135 | + const val ADMANAGER_AD_UNIT_ID = "/21775744923/example/native" |
| 136 | + const val ADMANAGER_VIDEO_AD_UNIT_ID = "/21775744923/example/native-video" |
| 137 | + } |
| 138 | +} |
0 commit comments