|
14 | 14 |
|
15 | 15 | package com.google.android.gms.snippets |
16 | 16 |
|
| 17 | +import android.app.Activity |
17 | 18 | import android.content.Context |
| 19 | +import android.view.LayoutInflater |
| 20 | +import android.view.View |
| 21 | +import android.widget.FrameLayout |
| 22 | +import android.widget.ImageView |
18 | 23 | import com.google.android.gms.ads.AdListener |
19 | 24 | import com.google.android.gms.ads.AdLoader |
20 | 25 | import com.google.android.gms.ads.AdRequest |
21 | 26 | import com.google.android.gms.ads.LoadAdError |
22 | 27 | import com.google.android.gms.ads.admanager.AdManagerAdRequest |
| 28 | +import com.google.android.gms.ads.nativead.MediaView |
23 | 29 | import com.google.android.gms.ads.nativead.NativeAd |
24 | 30 | import com.google.android.gms.ads.nativead.NativeAdOptions |
| 31 | +import com.google.android.gms.example.apidemo.databinding.NativeAdBinding |
25 | 32 | import kotlinx.coroutines.CoroutineScope |
26 | 33 | import kotlinx.coroutines.Dispatchers |
27 | 34 | import kotlinx.coroutines.launch |
@@ -97,20 +104,162 @@ internal class NativeAdSnippets { |
97 | 104 | // [END handle_ad_loaded] |
98 | 105 | } |
99 | 106 |
|
| 107 | + private fun addNativeAdView( |
| 108 | + activity: Activity, |
| 109 | + nativeAd: NativeAd, |
| 110 | + layoutInflater: LayoutInflater, |
| 111 | + frameLayout: FrameLayout, |
| 112 | + ) { |
| 113 | + // [START add_ad_view] |
| 114 | + activity.runOnUiThread { |
| 115 | + // Inflate the native ad view and add it to the view hierarchy. |
| 116 | + val nativeAdBinding = NativeAdBinding.inflate(layoutInflater) |
| 117 | + val adView = nativeAdBinding.root |
| 118 | + |
| 119 | + // Display and register the native ad asset views here. |
| 120 | + displayNativeAd(nativeAd, nativeAdBinding) |
| 121 | + |
| 122 | + // Remove all old ad views and add the new native. |
| 123 | + frameLayout.removeAllViews() |
| 124 | + // Add the new native ad view to the view hierarchy. |
| 125 | + frameLayout.addView(adView) |
| 126 | + } |
| 127 | + // [END add_ad_view] |
| 128 | + } |
| 129 | + |
| 130 | + // [START display_native_ad] |
| 131 | + private fun displayNativeAd(nativeAd: NativeAd, nativeAdBinding: NativeAdBinding) { |
| 132 | + // [START populate_native_ad_view] |
| 133 | + // Populate all native ad view assets with the native ad. |
| 134 | + nativeAdBinding.adMedia.mediaContent = nativeAd.mediaContent |
| 135 | + nativeAdBinding.adAdvertiser.text = nativeAd.advertiser |
| 136 | + nativeAdBinding.adBody.text = nativeAd.body |
| 137 | + nativeAdBinding.adCallToAction.text = nativeAd.callToAction |
| 138 | + nativeAdBinding.adHeadline.text = nativeAd.headline |
| 139 | + nativeAdBinding.adAppIcon.setImageDrawable(nativeAd.icon?.drawable) |
| 140 | + nativeAdBinding.adPrice.text = nativeAd.price |
| 141 | + nativeAd.starRating?.toFloat().also { value -> |
| 142 | + if (value != null) { |
| 143 | + nativeAdBinding.adStars.rating = value |
| 144 | + } |
| 145 | + } |
| 146 | + nativeAdBinding.adStore.text = nativeAd.store |
| 147 | + // [END populate_native_ad_view] |
| 148 | + |
| 149 | + // [START hide_native_ad_view_assets] |
| 150 | + // Hide all native ad view assets that are not returned within the native ad. |
| 151 | + if (nativeAd.body == null) { |
| 152 | + nativeAdBinding.adBody.visibility = View.INVISIBLE |
| 153 | + } else { |
| 154 | + nativeAdBinding.adBody.text = nativeAd.body |
| 155 | + nativeAdBinding.adBody.visibility = View.VISIBLE |
| 156 | + } |
| 157 | + |
| 158 | + if (nativeAd.callToAction == null) { |
| 159 | + nativeAdBinding.adCallToAction.visibility = View.INVISIBLE |
| 160 | + } else { |
| 161 | + nativeAdBinding.adCallToAction.text = nativeAd.callToAction |
| 162 | + nativeAdBinding.adCallToAction.visibility = View.VISIBLE |
| 163 | + } |
| 164 | + |
| 165 | + if (nativeAd.icon == null) { |
| 166 | + nativeAdBinding.adAppIcon.visibility = View.GONE |
| 167 | + } else { |
| 168 | + nativeAdBinding.adAppIcon.setImageDrawable(nativeAd.icon?.drawable) |
| 169 | + nativeAdBinding.adAppIcon.visibility = View.VISIBLE |
| 170 | + } |
| 171 | + |
| 172 | + if (nativeAd.price == null) { |
| 173 | + nativeAdBinding.adPrice.visibility = View.INVISIBLE |
| 174 | + } else { |
| 175 | + nativeAdBinding.adPrice.text = nativeAd.price |
| 176 | + nativeAdBinding.adPrice.visibility = View.VISIBLE |
| 177 | + } |
| 178 | + |
| 179 | + if (nativeAd.store == null) { |
| 180 | + nativeAdBinding.adStore.visibility = View.INVISIBLE |
| 181 | + } else { |
| 182 | + nativeAdBinding.adStore.text = nativeAd.store |
| 183 | + nativeAdBinding.adStore.visibility = View.VISIBLE |
| 184 | + } |
| 185 | + |
| 186 | + if (nativeAd.starRating == null) { |
| 187 | + nativeAdBinding.adStars.visibility = View.INVISIBLE |
| 188 | + } else { |
| 189 | + nativeAdBinding.adStars.rating = nativeAd.starRating!!.toFloat() |
| 190 | + nativeAdBinding.adStars.visibility = View.VISIBLE |
| 191 | + } |
| 192 | + |
| 193 | + if (nativeAd.advertiser == null) { |
| 194 | + nativeAdBinding.adAdvertiser.visibility = View.INVISIBLE |
| 195 | + } else { |
| 196 | + nativeAdBinding.adAdvertiser.text = nativeAd.advertiser |
| 197 | + nativeAdBinding.adAdvertiser.visibility = View.VISIBLE |
| 198 | + } |
| 199 | + // [END hide_native_ad_view_assets] |
| 200 | + |
| 201 | + // [START register_native_ad_assets] |
| 202 | + // Register all native ad assets with the native ad view. |
| 203 | + val nativeAdView = nativeAdBinding.root |
| 204 | + nativeAdView.advertiserView = nativeAdBinding.adAdvertiser |
| 205 | + nativeAdView.bodyView = nativeAdBinding.adBody |
| 206 | + nativeAdView.callToActionView = nativeAdBinding.adCallToAction |
| 207 | + nativeAdView.headlineView = nativeAdBinding.adHeadline |
| 208 | + nativeAdView.iconView = nativeAdBinding.adAppIcon |
| 209 | + nativeAdView.priceView = nativeAdBinding.adPrice |
| 210 | + nativeAdView.starRatingView = nativeAdBinding.adStars |
| 211 | + nativeAdView.storeView = nativeAdBinding.adStore |
| 212 | + nativeAd.mediaContent?.let { nativeAdBinding.adMedia.setMediaContent(it) } |
| 213 | + nativeAdView.mediaView = nativeAdBinding.adMedia |
| 214 | + // [END register_native_ad_assets] |
| 215 | + |
| 216 | + // [START set_native_ad] |
| 217 | + // This method tells the Google Mobile Ads SDK that you have finished populating your |
| 218 | + // native ad view with this native ad. |
| 219 | + nativeAdView.setNativeAd(nativeAd) |
| 220 | + // [END set_native_ad] |
| 221 | + } |
| 222 | + |
| 223 | + // [END display_native_ad] |
| 224 | + |
100 | 225 | private fun destroyAd(nativeAd: NativeAd) { |
101 | 226 | // [START destroy_ad] |
102 | 227 | nativeAd.destroy() |
103 | 228 | // [END destroy_ad] |
104 | 229 | } |
105 | 230 |
|
| 231 | + private fun setEventCallback(adLoader: AdLoader.Builder) { |
| 232 | + // [START set_event_callback] |
| 233 | + adLoader |
| 234 | + .withAdListener( |
| 235 | + object : AdListener() { |
| 236 | + override fun onAdFailedToLoad(adError: LoadAdError) { |
| 237 | + // Handle the failure. |
| 238 | + } |
| 239 | + |
| 240 | + override fun onAdClicked() { |
| 241 | + // Log the click event or other custom behavior. |
| 242 | + } |
| 243 | + } |
| 244 | + ) |
| 245 | + .build() |
| 246 | + // [END set_event_callback] |
| 247 | + } |
| 248 | + |
| 249 | + // [START set_image_scale_type_compose] |
| 250 | + private fun setImageScaleType(mediaView: MediaView) { |
| 251 | + // [START set_image_scale_type] |
| 252 | + mediaView.setImageScaleType(ImageView.ScaleType.CENTER_CROP) |
| 253 | + // [END set_image_scale_type] |
| 254 | + } |
| 255 | + |
| 256 | + // [END set_image_scale_type_compose] |
| 257 | + |
106 | 258 | private companion object { |
107 | 259 | // Test ad unit IDs. |
108 | 260 | // For more information, |
109 | 261 | // see https://developers.google.com/admob/android/test-ads. |
110 | 262 | // and https://developers.google.com/ad-manager/mobile-ads-sdk/android/test-ads. |
111 | 263 | const val AD_UNIT_ID = "ca-app-pub-3940256099942544/2247696110" |
112 | | - const val VIDEO_AD_UNIT_ID = "ca-app-pub-3940256099942544/1044960115" |
113 | | - const val ADMANAGER_AD_UNIT_ID = "/21775744923/example/native" |
114 | | - const val ADMANAGER_VIDEO_AD_UNIT_ID = "/21775744923/example/native-video" |
115 | 264 | } |
116 | 265 | } |
0 commit comments