Skip to content

Commit 74ba1c3

Browse files
nventimiglicopybara-github
authored andcommitted
Updated native ad Java code snippets.
PiperOrigin-RevId: 806369566
1 parent 0563d42 commit 74ba1c3

File tree

2 files changed

+246
-5
lines changed

2 files changed

+246
-5
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<com.google.android.libraries.ads.mobile.sdk.nativead.NativeAdView xmlns:android="http://schemas.android.com/apk/res/android"
2+
android:layout_width="match_parent"
3+
android:layout_height="wrap_content">
4+
5+
<LinearLayout
6+
android:layout_width="match_parent"
7+
android:layout_height="wrap_content"
8+
android:orientation="vertical">
9+
10+
<!-- Ad Attribution -->
11+
<TextView
12+
android:layout_width="wrap_content"
13+
android:layout_height="wrap_content"
14+
android:text="Ad" />
15+
16+
<TextView
17+
android:id="@+id/ad_headline"
18+
android:layout_width="wrap_content"
19+
android:layout_height="wrap_content" />
20+
21+
<LinearLayout
22+
android:layout_width="match_parent"
23+
android:layout_height="wrap_content"
24+
android:orientation="horizontal">
25+
<ImageView
26+
android:id="@+id/ad_icon"
27+
android:layout_width="40dp"
28+
android:layout_height="40dp"
29+
android:adjustViewBounds="true"
30+
android:scaleType="centerCrop" />
31+
<TextView
32+
android:id="@+id/ad_advertiser"
33+
android:layout_width="wrap_content"
34+
android:layout_height="wrap_content"
35+
android:layout_marginStart="8dp" />
36+
</LinearLayout>
37+
38+
<TextView
39+
android:id="@+id/ad_body"
40+
android:layout_width="wrap_content"
41+
android:layout_height="wrap_content" />
42+
43+
<LinearLayout
44+
android:layout_width="match_parent"
45+
android:layout_height="wrap_content"
46+
android:orientation="horizontal">
47+
<TextView
48+
android:id="@+id/ad_price"
49+
android:layout_width="wrap_content"
50+
android:layout_height="wrap_content"
51+
android:layout_marginEnd="8dp" />
52+
<TextView
53+
android:id="@+id/ad_store"
54+
android:layout_width="wrap_content"
55+
android:layout_height="wrap_content"
56+
android:layout_marginEnd="8dp" />
57+
<RatingBar
58+
android:id="@+id/ad_stars"
59+
style="?android:attr/ratingBarStyleSmall"
60+
android:layout_width="wrap_content"
61+
android:layout_height="wrap_content"
62+
android:isIndicator="true"
63+
android:numStars="5"
64+
android:stepSize="0.5" />
65+
</LinearLayout>
66+
67+
<!-- Media Asset must be at least 120x120dp -->
68+
<com.google.android.libraries.ads.mobile.sdk.nativead.MediaView
69+
android:id="@+id/ad_media"
70+
android:layout_width="match_parent"
71+
android:layout_height="175dp" />
72+
73+
<Button
74+
android:id="@+id/ad_call_to_action"
75+
android:layout_width="wrap_content"
76+
android:layout_height="wrap_content" />
77+
78+
</LinearLayout>
79+
</com.google.android.libraries.ads.mobile.sdk.nativead.NativeAdView>

java/advanced/APIDemo/app/src/main/java/com/google/android/gms/snippets/NativeAdSnippets.java

Lines changed: 167 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,23 @@
1414

1515
package com.google.android.gms.snippets;
1616

17+
import android.app.Activity;
1718
import android.content.Context;
19+
import android.view.LayoutInflater;
20+
import android.view.View;
21+
import android.widget.FrameLayout;
22+
import android.widget.ImageView;
1823
import androidx.annotation.NonNull;
1924
import com.google.android.gms.ads.AdListener;
2025
import com.google.android.gms.ads.AdLoader;
2126
import com.google.android.gms.ads.AdRequest;
2227
import com.google.android.gms.ads.LoadAdError;
2328
import com.google.android.gms.ads.admanager.AdManagerAdRequest;
29+
import com.google.android.gms.ads.nativead.MediaView;
2430
import com.google.android.gms.ads.nativead.NativeAd;
2531
import 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. */
2836
final 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

Comments
 (0)