|
14 | 14 |
|
15 | 15 | package com.google.android.gms.snippets |
16 | 16 |
|
17 | | -import android.content.Context |
| 17 | +import android.app.Activity |
| 18 | +import android.view.LayoutInflater |
| 19 | +import android.view.View |
| 20 | +import android.widget.FrameLayout |
| 21 | +import android.widget.ImageView |
18 | 22 | import com.google.android.gms.ads.AdListener |
19 | 23 | import com.google.android.gms.ads.AdLoader |
20 | 24 | import com.google.android.gms.ads.AdRequest |
21 | 25 | import com.google.android.gms.ads.LoadAdError |
22 | 26 | import com.google.android.gms.ads.admanager.AdManagerAdRequest |
| 27 | +import com.google.android.gms.ads.nativead.MediaView |
23 | 28 | import com.google.android.gms.ads.nativead.NativeAd |
24 | | -import com.google.android.gms.ads.nativead.NativeAdOptions |
25 | | -import kotlinx.coroutines.CoroutineScope |
26 | | -import kotlinx.coroutines.Dispatchers |
27 | | -import kotlinx.coroutines.launch |
| 29 | +import com.google.android.gms.example.apidemo.databinding.NativeAdBinding |
28 | 30 |
|
29 | 31 | /** Kotlin code snippets for the developer guide. */ |
30 | 32 | internal class NativeAdSnippets { |
31 | 33 |
|
32 | | - private fun createAdLoader(context: Context) { |
33 | | - // [START create_ad_loader] |
34 | | - // It is recommended to call AdLoader.Builder on a background thread. |
35 | | - CoroutineScope(Dispatchers.IO).launch { |
36 | | - val adLoader = |
37 | | - AdLoader.Builder(context, AD_UNIT_ID) |
38 | | - .forNativeAd { nativeAd -> |
39 | | - // The native ad loaded successfully. You can show the ad. |
40 | | - } |
41 | | - .withAdListener( |
42 | | - object : AdListener() { |
43 | | - override fun onAdFailedToLoad(adError: LoadAdError) { |
44 | | - // The native ad load failed. Check the adError message for failure reasons. |
45 | | - } |
46 | | - } |
47 | | - ) |
48 | | - // Use the NativeAdOptions.Builder class to specify individual options settings. |
49 | | - .withNativeAdOptions(NativeAdOptions.Builder().build()) |
50 | | - .build() |
51 | | - } |
52 | | - // [END create_ad_loader] |
53 | | - } |
54 | | - |
55 | | - private fun setAdLoaderListener(adLoaderBuilder: AdLoader.Builder) { |
56 | | - // [START set_ad_listener] |
57 | | - adLoaderBuilder.withAdListener( |
58 | | - // Override AdListener callbacks here. |
59 | | - object : AdListener() {} |
60 | | - ) |
61 | | - // [END set_ad_listener] |
62 | | - } |
63 | | - |
64 | 34 | private fun loadAd(adLoader: AdLoader) { |
65 | 35 | // [START load_ad] |
66 | 36 | adLoader.loadAd(AdRequest.Builder().build()) |
@@ -97,20 +67,144 @@ internal class NativeAdSnippets { |
97 | 67 | // [END handle_ad_loaded] |
98 | 68 | } |
99 | 69 |
|
| 70 | + private fun addNativeAdView( |
| 71 | + activity: Activity, |
| 72 | + nativeAd: NativeAd, |
| 73 | + layoutInflater: LayoutInflater, |
| 74 | + frameLayout: FrameLayout, |
| 75 | + ) { |
| 76 | + // [START add_ad_view] |
| 77 | + activity.runOnUiThread { |
| 78 | + // Inflate the native ad view and add it to the view hierarchy. |
| 79 | + val nativeAdBinding = NativeAdBinding.inflate(layoutInflater) |
| 80 | + val adView = nativeAdBinding.root |
| 81 | + |
| 82 | + // Display and register the native ad asset views here. |
| 83 | + displayAndRegisterNativeAd(nativeAd, nativeAdBinding) |
| 84 | + |
| 85 | + // Remove all old ad views and add the new native. |
| 86 | + frameLayout.removeAllViews() |
| 87 | + // Add the new native ad view to the view hierarchy. |
| 88 | + frameLayout.addView(adView) |
| 89 | + } |
| 90 | + // [END add_ad_view] |
| 91 | + } |
| 92 | + |
| 93 | + // [START display_native_ad] |
| 94 | + private fun displayAndRegisterNativeAd(nativeAd: NativeAd, nativeAdBinding: NativeAdBinding) { |
| 95 | + // [START populate_native_ad_view] |
| 96 | + // Populate all native ad view assets with the native ad. |
| 97 | + nativeAdBinding.adMedia.mediaContent = nativeAd.mediaContent |
| 98 | + nativeAdBinding.adHeadline.text = nativeAd.headline |
| 99 | + |
| 100 | + // Hide all native ad view assets that are not returned within the native ad. |
| 101 | + nativeAd.body?.let { body -> |
| 102 | + nativeAdBinding.adBody.text = body |
| 103 | + nativeAdBinding.adBody.visibility = View.VISIBLE |
| 104 | + } ?: run { nativeAdBinding.adBody.visibility = View.INVISIBLE } |
| 105 | + |
| 106 | + nativeAd.callToAction?.let { callToAction -> |
| 107 | + nativeAdBinding.adCallToAction.text = callToAction |
| 108 | + nativeAdBinding.adCallToAction.visibility = View.VISIBLE |
| 109 | + } ?: run { nativeAdBinding.adCallToAction.visibility = View.INVISIBLE } |
| 110 | + |
| 111 | + nativeAd.icon?.let { icon -> |
| 112 | + nativeAdBinding.adAppIcon.setImageDrawable(icon.drawable) |
| 113 | + nativeAdBinding.adAppIcon.visibility = View.VISIBLE |
| 114 | + } ?: run { nativeAdBinding.adAppIcon.visibility = View.GONE } |
| 115 | + |
| 116 | + nativeAd.price?.let { price -> |
| 117 | + nativeAdBinding.adPrice.text = price |
| 118 | + nativeAdBinding.adPrice.visibility = View.VISIBLE |
| 119 | + } ?: run { nativeAdBinding.adPrice.visibility = View.INVISIBLE } |
| 120 | + |
| 121 | + nativeAd.store?.let { store -> |
| 122 | + nativeAdBinding.adStore.text = store |
| 123 | + nativeAdBinding.adStore.visibility = View.VISIBLE |
| 124 | + } ?: run { nativeAdBinding.adStore.visibility = View.INVISIBLE } |
| 125 | + |
| 126 | + nativeAd.starRating?.let { rating -> |
| 127 | + nativeAdBinding.adStars.rating = rating.toFloat() |
| 128 | + nativeAdBinding.adStars.visibility = View.VISIBLE |
| 129 | + } ?: run { nativeAdBinding.adStars.visibility = View.INVISIBLE } |
| 130 | + |
| 131 | + nativeAd.advertiser?.let { advertiser -> |
| 132 | + nativeAdBinding.adAdvertiser.text = advertiser |
| 133 | + nativeAdBinding.adAdvertiser.visibility = View.VISIBLE |
| 134 | + } ?: run { nativeAdBinding.adAdvertiser.visibility = View.INVISIBLE } |
| 135 | + // [END populate_native_ad_view] |
| 136 | + |
| 137 | + // [START register_native_ad_assets] |
| 138 | + // Register all native ad assets with the native ad view. |
| 139 | + val nativeAdView = nativeAdBinding.root |
| 140 | + nativeAdView.advertiserView = nativeAdBinding.adAdvertiser |
| 141 | + nativeAdView.bodyView = nativeAdBinding.adBody |
| 142 | + nativeAdView.callToActionView = nativeAdBinding.adCallToAction |
| 143 | + nativeAdView.headlineView = nativeAdBinding.adHeadline |
| 144 | + nativeAdView.iconView = nativeAdBinding.adAppIcon |
| 145 | + nativeAdView.priceView = nativeAdBinding.adPrice |
| 146 | + nativeAdView.starRatingView = nativeAdBinding.adStars |
| 147 | + nativeAdView.storeView = nativeAdBinding.adStore |
| 148 | + nativeAd.mediaContent?.let { nativeAdBinding.adMedia.setMediaContent(it) } |
| 149 | + nativeAdView.mediaView = nativeAdBinding.adMedia |
| 150 | + // [END register_native_ad_assets] |
| 151 | + |
| 152 | + // [START set_native_ad] |
| 153 | + // This method tells the Google Mobile Ads SDK that you have finished populating your |
| 154 | + // native ad view with this native ad. |
| 155 | + nativeAdView.setNativeAd(nativeAd) |
| 156 | + // [END set_native_ad] |
| 157 | + } |
| 158 | + |
| 159 | + // [END display_native_ad] |
| 160 | + |
100 | 161 | private fun destroyAd(nativeAd: NativeAd) { |
101 | 162 | // [START destroy_ad] |
102 | 163 | nativeAd.destroy() |
103 | 164 | // [END destroy_ad] |
104 | 165 | } |
105 | 166 |
|
106 | | - private companion object { |
107 | | - // Test ad unit IDs. |
108 | | - // For more information, |
109 | | - // see https://developers.google.com/admob/android/test-ads. |
110 | | - // and https://developers.google.com/ad-manager/mobile-ads-sdk/android/test-ads. |
111 | | - 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" |
| 167 | + private fun setEventCallback(adLoader: AdLoader.Builder) { |
| 168 | + // [START set_event_callback] |
| 169 | + adLoader |
| 170 | + .withAdListener( |
| 171 | + object : AdListener() { |
| 172 | + override fun onAdClosed() { |
| 173 | + // Called when the ad is closed. |
| 174 | + } |
| 175 | + |
| 176 | + override fun onAdFailedToLoad(adError: LoadAdError) { |
| 177 | + // Called when an ad fails to load. |
| 178 | + } |
| 179 | + |
| 180 | + override fun onAdOpened() { |
| 181 | + // Called when an ad opens full screen. |
| 182 | + } |
| 183 | + |
| 184 | + override fun onAdLoaded() { |
| 185 | + // Called when an ad has loaded. |
| 186 | + } |
| 187 | + |
| 188 | + override fun onAdClicked() { |
| 189 | + // Called when a click is recorded for an ad. |
| 190 | + } |
| 191 | + |
| 192 | + override fun onAdImpression() { |
| 193 | + // Called when an impression is recorded for an ad. |
| 194 | + } |
| 195 | + |
| 196 | + override fun onAdSwipeGestureClicked() { |
| 197 | + // Called when a swipe gesture is recorded for an ad. |
| 198 | + } |
| 199 | + } |
| 200 | + ) |
| 201 | + .build() |
| 202 | + // [END set_event_callback] |
| 203 | + } |
| 204 | + |
| 205 | + private fun setImageScaleType(mediaView: MediaView) { |
| 206 | + // [START set_image_scale_type] |
| 207 | + mediaView.setImageScaleType(ImageView.ScaleType.CENTER_CROP) |
| 208 | + // [END set_image_scale_type] |
115 | 209 | } |
116 | 210 | } |
0 commit comments