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]
@@ -110,9 +115,117 @@ private void handleAdLoaded(AdLoader.Builder adLoaderBuilder) {
110115 // [END handle_ad_loaded]
111116 }
112117
118+ private void addNativeAdView (
119+ NativeAd nativeAd ,
120+ Activity activity ,
121+ LayoutInflater layoutInflater ,
122+ FrameLayout frameLayout ) {
123+ // [START add_ad_view]
124+ activity .runOnUiThread (
125+ () -> {
126+ // Inflate the native ad view and add it to the view hierarchy.
127+ NativeAdBinding nativeAdBinding = NativeAdBinding .inflate (layoutInflater );
128+ View adView = nativeAdBinding .getRoot ();
129+
130+ // Display and register the native ad asset views here.
131+ displayNativeAd (nativeAd , nativeAdBinding );
132+
133+ // Remove all old ad views and add the new native.
134+ frameLayout .removeAllViews ();
135+ // Add the new native ad view to the view hierarchy.
136+ frameLayout .addView (adView );
137+ });
138+ // [END add_ad_view]
139+ }
140+
141+ // [START display_native_ad]
142+ private void displayNativeAd (NativeAd nativeAd , NativeAdBinding nativeAdBinding ) {
143+ // [START populate_native_ad_view]
144+ // Populate all native ad view assets with the native ad.
145+ nativeAdBinding .adMedia .setMediaContent (nativeAd .getMediaContent ());
146+ nativeAdBinding .adAdvertiser .setText (nativeAd .getAdvertiser ());
147+ nativeAdBinding .adBody .setText (nativeAd .getBody ());
148+ nativeAdBinding .adCallToAction .setText (nativeAd .getCallToAction ());
149+ nativeAdBinding .adHeadline .setText (nativeAd .getHeadline ());
150+ nativeAdBinding .adAppIcon .setImageDrawable (nativeAd .getIcon ().getDrawable ());
151+ nativeAdBinding .adPrice .setText (nativeAd .getPrice ());
152+ Double starRating = nativeAd .getStarRating ();
153+ if (starRating != null ) {
154+ nativeAdBinding .adStars .setRating (starRating .floatValue ());
155+ }
156+ nativeAdBinding .adStore .setText (nativeAd .getStore ());
157+ // [END populate_native_ad_view]
158+
159+ // [START hide_native_ad_view_assets]
160+ // Hide all native ad view assets that are not returned within the native ad.
161+ nativeAdBinding .adAdvertiser .setVisibility (
162+ (nativeAd .getAdvertiser () == null ) ? View .GONE : View .VISIBLE );
163+ nativeAdBinding .adBody .setVisibility ((nativeAd .getBody () == null ) ? View .GONE : View .VISIBLE );
164+ nativeAdBinding .adCallToAction .setVisibility (
165+ (nativeAd .getCallToAction () == null ) ? View .GONE : View .VISIBLE );
166+ nativeAdBinding .adHeadline .setVisibility (
167+ (nativeAd .getHeadline () == null ) ? View .GONE : View .VISIBLE );
168+ nativeAdBinding .adAppIcon .setVisibility (
169+ (nativeAd .getIcon () == null ) ? View .GONE : View .VISIBLE );
170+ nativeAdBinding .adPrice .setVisibility ((nativeAd .getPrice () == null ) ? View .GONE : View .VISIBLE );
171+ nativeAdBinding .adStars .setVisibility (
172+ (nativeAd .getStarRating () == null ) ? View .GONE : View .VISIBLE );
173+ nativeAdBinding .adMedia .setVisibility (
174+ (nativeAd .getMediaContent () == null ) ? View .GONE : View .VISIBLE );
175+ nativeAdBinding .adStore .setVisibility ((nativeAd .getStore () == null ) ? View .GONE : View .VISIBLE );
176+ // [END hide_native_ad_view_assets]
177+
178+ // [START register_native_ad_assets]
179+ // Register all native ad assets with the native ad view.
180+ NativeAdView nativeAdView = nativeAdBinding .getRoot ();
181+ nativeAdView .setAdvertiserView (nativeAdBinding .adAdvertiser );
182+ nativeAdView .setBodyView (nativeAdBinding .adBody );
183+ nativeAdView .setCallToActionView (nativeAdBinding .adCallToAction );
184+ nativeAdView .setHeadlineView (nativeAdBinding .adHeadline );
185+ nativeAdView .setIconView (nativeAdBinding .adAppIcon );
186+ nativeAdView .setPriceView (nativeAdBinding .adPrice );
187+ nativeAdView .setStarRatingView (nativeAdBinding .adStars );
188+ nativeAdView .setStoreView (nativeAdBinding .adStore );
189+ nativeAdView .setMediaView (nativeAdBinding .adMedia );
190+ // [END register_native_ad_assets]
191+
192+ // [START set_native_ad]
193+ // This method tells the Google Mobile Ads SDK that you have finished populating your
194+ // native ad view with this native ad.
195+ nativeAdView .setNativeAd (nativeAd );
196+ // [END set_native_ad]
197+ }
198+
199+ // [END display_native_ad]
200+
201+ private void setEventCallback (AdLoader .Builder adLoader ) {
202+ // [START set_event_callback]
203+ adLoader
204+ .withAdListener (
205+ new AdListener () {
206+ @ Override
207+ public void onAdFailedToLoad (LoadAdError adError ) {
208+ // Handle the failure.
209+ }
210+
211+ @ Override
212+ public void onAdClicked () {
213+ // Log the click event or other custom behavior.
214+ }
215+ })
216+ .build ();
217+ // [END set_event_callback]
218+ }
219+
113220 private void destroyAd (NativeAd nativeAd ) {
114221 // [START destroy_ad]
115222 nativeAd .destroy ();
116223 // [END destroy_ad]
117224 }
225+
226+ private void setImageScaleType (MediaView mediaView ) {
227+ // [START set_image_scale_type]
228+ mediaView .setImageScaleType (ImageView .ScaleType .CENTER_CROP );
229+ // [END set_image_scale_type]
230+ }
118231}
0 commit comments