1414
1515package com .google .android .gms .snippets ;
1616
17+ import android .app .Activity ;
1718import android .content .Context ;
19+ import android .view .LayoutInflater ;
20+ import android .view .View ;
21+ import android .widget .FrameLayout ;
22+ import android .widget .ImageView ;
1823import androidx .annotation .NonNull ;
1924import com .google .android .gms .ads .AdListener ;
2025import com .google .android .gms .ads .AdLoader ;
2126import com .google .android .gms .ads .AdRequest ;
2227import com .google .android .gms .ads .LoadAdError ;
2328import com .google .android .gms .ads .admanager .AdManagerAdRequest ;
29+ import com .google .android .gms .ads .nativead .MediaView ;
2430import com .google .android .gms .ads .nativead .NativeAd ;
2531import com .google .android .gms .ads .nativead .NativeAdOptions ;
32+ import com .google .android .gms .ads .nativead .NativeAdView ;
33+ import com .google .android .gms .example .apidemo .databinding .NativeAdBinding ;
2634
2735/** Java code snippets for the developer guide. */
2836final class NativeAdSnippets {
@@ -32,9 +40,6 @@ final class NativeAdSnippets {
3240 // see https://developers.google.com/admob/android/test-ads.
3341 // and https://developers.google.com/ad-manager/mobile-ads-sdk/android/test-ads.
3442 private static final String AD_UNIT_ID = "ca-app-pub-3940256099942544/2247696110" ;
35- private static final String VIDEO_AD_UNIT_ID = "ca-app-pub-3940256099942544/1044960115" ;
36- private static final String ADMANAGER_AD_UNIT_ID = "/21775744923/example/native" ;
37- private static final String ADMANAGER_VIDEO_AD_UNIT_ID = "/21775744923/example/native-video" ;
3843
3944 private void createAdLoader (Context context ) {
4045 // [START create_ad_loader]
@@ -52,9 +57,33 @@ public void onNativeAdLoaded(@NonNull NativeAd nativeAd) {}
5257 .withAdListener (
5358 new AdListener () {
5459 @ Override
55- // The native ad load failed. Check the adError message for failure
56- // reasons.
60+ // Called when the user is about to return to the application after
61+ // clicking on an ad.
62+ public void onAdClosed () {}
63+
64+ @ Override
65+ // Called when an ad request failed.
5766 public void onAdFailedToLoad (@ NonNull LoadAdError adError ) {}
67+
68+ @ Override
69+ // Called when an ad opens an overlay that covers the screen.
70+ public void onAdOpened () {}
71+
72+ @ Override
73+ // Called when an ad is received.
74+ public void onAdLoaded () {}
75+
76+ @ Override
77+ // Called when a click is recorded for an ad.
78+ public void onAdClicked () {}
79+
80+ @ Override
81+ // Called when an impression is recorded for an ad.
82+ public void onAdImpression () {}
83+
84+ @ Override
85+ // Called when a swipe gesture on an ad is recorded as a click.
86+ public void onAdSwipeGestureClicked () {}
5887 })
5988 // Use the NativeAdOptions.Builder class to specify individual options
6089 // settings.
@@ -110,9 +139,142 @@ private void handleAdLoaded(AdLoader.Builder adLoaderBuilder) {
110139 // [END handle_ad_loaded]
111140 }
112141
142+ private void addNativeAdView (
143+ NativeAd nativeAd ,
144+ Activity activity ,
145+ LayoutInflater layoutInflater ,
146+ FrameLayout frameLayout ) {
147+ // [START add_ad_view]
148+ activity .runOnUiThread (
149+ () -> {
150+ // Inflate the native ad view and add it to the view hierarchy.
151+ NativeAdBinding nativeAdBinding = NativeAdBinding .inflate (layoutInflater );
152+ View adView = nativeAdBinding .getRoot ();
153+
154+ // Display and register the native ad asset views here.
155+ displayAndRegisterNativeAd (nativeAd , nativeAdBinding );
156+
157+ // Remove all old ad views and add the new native.
158+ frameLayout .removeAllViews ();
159+ // Add the new native ad view to the view hierarchy.
160+ frameLayout .addView (adView );
161+ });
162+ // [END add_ad_view]
163+ }
164+
165+ // [START display_native_ad]
166+ private void displayAndRegisterNativeAd (NativeAd nativeAd , NativeAdBinding nativeAdBinding ) {
167+ // [START populate_native_ad_view]
168+ // Populate all native ad view assets with the native ad.
169+ nativeAdBinding .adMedia .setMediaContent (nativeAd .getMediaContent ());
170+ nativeAdBinding .adAdvertiser .setText (nativeAd .getAdvertiser ());
171+ nativeAdBinding .adBody .setText (nativeAd .getBody ());
172+ nativeAdBinding .adCallToAction .setText (nativeAd .getCallToAction ());
173+ nativeAdBinding .adHeadline .setText (nativeAd .getHeadline ());
174+ nativeAdBinding .adAppIcon .setImageDrawable (nativeAd .getIcon ().getDrawable ());
175+ nativeAdBinding .adPrice .setText (nativeAd .getPrice ());
176+ Double starRating = nativeAd .getStarRating ();
177+ if (starRating != null ) {
178+ nativeAdBinding .adStars .setRating (starRating .floatValue ());
179+ }
180+ nativeAdBinding .adStore .setText (nativeAd .getStore ());
181+ // [END populate_native_ad_view]
182+
183+ // [START hide_native_ad_view_assets]
184+ // Hide all native ad view assets that are not returned within the native ad.
185+ nativeAdBinding .adAdvertiser .setVisibility (
186+ (nativeAd .getAdvertiser () == null ) ? View .GONE : View .VISIBLE );
187+ nativeAdBinding .adBody .setVisibility ((nativeAd .getBody () == null ) ? View .GONE : View .VISIBLE );
188+ nativeAdBinding .adCallToAction .setVisibility (
189+ (nativeAd .getCallToAction () == null ) ? View .GONE : View .VISIBLE );
190+ nativeAdBinding .adHeadline .setVisibility (
191+ (nativeAd .getHeadline () == null ) ? View .GONE : View .VISIBLE );
192+ nativeAdBinding .adAppIcon .setVisibility (
193+ (nativeAd .getIcon () == null ) ? View .GONE : View .VISIBLE );
194+ nativeAdBinding .adPrice .setVisibility ((nativeAd .getPrice () == null ) ? View .GONE : View .VISIBLE );
195+ nativeAdBinding .adStars .setVisibility (
196+ (nativeAd .getStarRating () == null ) ? View .GONE : View .VISIBLE );
197+ nativeAdBinding .adMedia .setVisibility (
198+ (nativeAd .getMediaContent () == null ) ? View .GONE : View .VISIBLE );
199+ nativeAdBinding .adStore .setVisibility ((nativeAd .getStore () == null ) ? View .GONE : View .VISIBLE );
200+ // [END hide_native_ad_view_assets]
201+
202+ // [START register_native_ad_assets]
203+ // Register all native ad assets with the native ad view.
204+ NativeAdView nativeAdView = nativeAdBinding .getRoot ();
205+ nativeAdView .setAdvertiserView (nativeAdBinding .adAdvertiser );
206+ nativeAdView .setBodyView (nativeAdBinding .adBody );
207+ nativeAdView .setCallToActionView (nativeAdBinding .adCallToAction );
208+ nativeAdView .setHeadlineView (nativeAdBinding .adHeadline );
209+ nativeAdView .setIconView (nativeAdBinding .adAppIcon );
210+ nativeAdView .setPriceView (nativeAdBinding .adPrice );
211+ nativeAdView .setStarRatingView (nativeAdBinding .adStars );
212+ nativeAdView .setStoreView (nativeAdBinding .adStore );
213+ nativeAdView .setMediaView (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+
225+ private void setEventCallback (AdLoader .Builder adLoader ) {
226+ // [START set_event_callback]
227+ adLoader
228+ .withAdListener (
229+ new AdListener () {
230+ @ Override
231+ public void onAdClosed () {
232+ // Called when the user is about to return to the application after clicking an ad.
233+ }
234+
235+ @ Override
236+ public void onAdFailedToLoad (LoadAdError adError ) {
237+ // Called when an ad request failed.
238+ }
239+
240+ @ Override
241+ public void onAdOpened () {
242+ // Called when an ad opens an overlay that covers the screen.
243+ }
244+
245+ @ Override
246+ public void onAdLoaded () {
247+ // Called when an ad is received.
248+ }
249+
250+ @ Override
251+ public void onAdClicked () {
252+ // Called when a click is recorded for an ad.
253+ }
254+
255+ @ Override
256+ public void onAdImpression () {
257+ // Called when an impression is recorded for an ad.
258+ }
259+
260+ @ Override
261+ public void onAdSwipeGestureClicked () {
262+ // Called when a swipe gesture on an ad is recorded as a click.
263+ }
264+ })
265+ .build ();
266+ // [END set_event_callback]
267+ }
268+
113269 private void destroyAd (NativeAd nativeAd ) {
114270 // [START destroy_ad]
115271 nativeAd .destroy ();
116272 // [END destroy_ad]
117273 }
274+
275+ private void setImageScaleType (MediaView mediaView ) {
276+ // [START set_image_scale_type]
277+ mediaView .setImageScaleType (ImageView .ScaleType .CENTER_CROP );
278+ // [END set_image_scale_type]
279+ }
118280}
0 commit comments