@@ -56,16 +56,14 @@ public class AdManagerCustomControlsFragment extends Fragment {
56
56
private CheckBox customControlsCheckbox ;
57
57
private CheckBox nativeAdsCheckbox ;
58
58
private CheckBox customFormatAdsCheckbox ;
59
- private CustomControlsView customControlsView ;
60
59
private NativeAd nativeAd ;
61
60
private NativeCustomFormatAd nativeCustomFormatAd ;
62
61
63
- public AdManagerCustomControlsFragment () {
64
- }
62
+ public AdManagerCustomControlsFragment () {}
65
63
66
64
@ Override
67
- public View onCreateView (LayoutInflater inflater , ViewGroup container ,
68
- Bundle savedInstanceState ) {
65
+ public View onCreateView (
66
+ LayoutInflater inflater , ViewGroup container , Bundle savedInstanceState ) {
69
67
return inflater .inflate (R .layout .fragment_gam_customcontrols , container , false );
70
68
}
71
69
@@ -77,7 +75,6 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat
77
75
customControlsCheckbox = view .findViewById (R .id .cb_custom_controls );
78
76
nativeAdsCheckbox = view .findViewById (R .id .cb_native );
79
77
customFormatAdsCheckbox = view .findViewById (R .id .cb_custom_format );
80
- customControlsView = view .findViewById (R .id .custom_controls );
81
78
82
79
refresh .setOnClickListener (unusedView -> refreshAd ());
83
80
@@ -111,6 +108,7 @@ private void populateNativeAdView(NativeAd nativeAd, NativeAdView adView) {
111
108
adView .setPriceView (adView .findViewById (R .id .ad_price ));
112
109
adView .setStarRatingView (adView .findViewById (R .id .ad_stars ));
113
110
adView .setStoreView (adView .findViewById (R .id .ad_store ));
111
+ adView .setMediaView (adView .findViewById (R .id .ad_media ));
114
112
115
113
// Some assets are guaranteed to be in every NativeAd.
116
114
((TextView ) adView .getHeadlineView ()).setText (nativeAd .getHeadline ());
@@ -137,20 +135,20 @@ private void populateNativeAdView(NativeAd nativeAd, NativeAdView adView) {
137
135
if (nativeAd .getStarRating () == null ) {
138
136
adView .getStarRatingView ().setVisibility (View .INVISIBLE );
139
137
} else {
140
- ((RatingBar ) adView .getStarRatingView ())
141
- .setRating (nativeAd .getStarRating ().floatValue ());
138
+ ((RatingBar ) adView .getStarRatingView ()).setRating (nativeAd .getStarRating ().floatValue ());
142
139
adView .getStarRatingView ().setVisibility (View .VISIBLE );
143
140
}
144
141
145
- // Assign native ad object to the native view.
146
- adView .setNativeAd (nativeAd );
147
-
148
- // Set up the custom video controls functionality.
142
+ // Set up the custom video controls.
149
143
MediaContent mediaContent = nativeAd .getMediaContent ();
150
144
if (mediaContent != null ) {
151
- customControlsView .setMediaContent (mediaContent );
145
+ CustomControlsView customControls = adView .findViewById (R .id .custom_video_controls );
146
+ customControls .initialize (mediaContent , startVideoAdsMuted .isChecked ());
152
147
}
153
148
149
+ // Assign native ad object to the native view.
150
+ adView .setNativeAd (nativeAd );
151
+
154
152
refresh .setEnabled (true );
155
153
}
156
154
@@ -161,8 +159,8 @@ private void populateNativeAdView(NativeAd nativeAd, NativeAdView adView) {
161
159
* @param nativeCustomFormatAd the object containing the ad's assets
162
160
* @param adView the view to be populated
163
161
*/
164
- private void populateSimpleTemplateAdView (final NativeCustomFormatAd nativeCustomFormatAd ,
165
- View adView ) {
162
+ private void populateSimpleTemplateAdView (
163
+ final NativeCustomFormatAd nativeCustomFormatAd , View adView ) {
166
164
TextView headline = adView .findViewById (R .id .simplecustom_headline );
167
165
TextView caption = adView .findViewById (R .id .simplecustom_caption );
168
166
@@ -175,11 +173,15 @@ private void populateSimpleTemplateAdView(final NativeCustomFormatAd nativeCusto
175
173
176
174
// Apps can check the MediaContent's hasVideoContent property to determine if the
177
175
// NativeCustomTemplateAd has a video asset.
178
- if ( nativeCustomFormatAd .getMediaContent () != null
179
- && nativeCustomFormatAd . getMediaContent () .hasVideoContent ()) {
176
+ MediaContent mediaContent = nativeCustomFormatAd .getMediaContent ();
177
+ if ( mediaContent != null && mediaContent .hasVideoContent ()) {
180
178
MediaView mediaView = new MediaView (requireActivity ());
181
- mediaView .setMediaContent (nativeCustomFormatAd . getMediaContent () );
179
+ mediaView .setMediaContent (mediaContent );
182
180
mediaPlaceholder .addView (mediaView );
181
+ CustomControlsView customControls = new CustomControlsView (getContext ());
182
+ customControls .initialize (mediaContent , startVideoAdsMuted .isChecked ());
183
+ ViewGroup videoControlHolder = adView .findViewById (R .id .simplecustom_video_control_holder );
184
+ videoControlHolder .addView (customControls );
183
185
} else {
184
186
ImageView mainImage = new ImageView (getActivity ());
185
187
mainImage .setAdjustViewBounds (true );
@@ -188,12 +190,10 @@ private void populateSimpleTemplateAdView(final NativeCustomFormatAd nativeCusto
188
190
mainImage .setOnClickListener (unusedView -> nativeCustomFormatAd .performClick ("MainImage" ));
189
191
mediaPlaceholder .addView (mainImage );
190
192
}
191
- customControlsView .setMediaContent (nativeCustomFormatAd .getMediaContent ());
192
193
193
194
refresh .setEnabled (true );
194
195
}
195
196
196
-
197
197
/**
198
198
* Creates a request for a new native ad based on the boolean parameters and calls the
199
199
* corresponding "populate" method when one is successfully returned.
@@ -202,8 +202,9 @@ private void refreshAd() {
202
202
refresh .setEnabled (false );
203
203
204
204
Resources resources = getActivity ().getResources ();
205
- AdLoader .Builder builder = new AdLoader .Builder (getActivity (),
206
- resources .getString (R .string .customcontrols_fragment_ad_unit_id ));
205
+ AdLoader .Builder builder =
206
+ new AdLoader .Builder (
207
+ getActivity (), resources .getString (R .string .customcontrols_fragment_ad_unit_id ));
207
208
208
209
if (customFormatAdsCheckbox .isChecked ()) {
209
210
builder .forCustomFormatAd (
@@ -264,14 +265,13 @@ public void onNativeAdLoaded(NativeAd ad) {
264
265
});
265
266
}
266
267
267
- VideoOptions videoOptions = new VideoOptions .Builder ()
268
- .setStartMuted (startVideoAdsMuted .isChecked ())
269
- .setCustomControlsRequested (customControlsCheckbox .isChecked ())
270
- .build ();
268
+ VideoOptions videoOptions =
269
+ new VideoOptions .Builder ()
270
+ .setStartMuted (startVideoAdsMuted .isChecked ())
271
+ .setCustomControlsRequested (customControlsCheckbox .isChecked ())
272
+ .build ();
271
273
272
- NativeAdOptions adOptions = new NativeAdOptions .Builder ()
273
- .setVideoOptions (videoOptions )
274
- .build ();
274
+ NativeAdOptions adOptions = new NativeAdOptions .Builder ().setVideoOptions (videoOptions ).build ();
275
275
276
276
builder .withNativeAdOptions (adOptions );
277
277
@@ -296,7 +296,5 @@ public void onAdFailedToLoad(LoadAdError loadAdError) {
296
296
})
297
297
.build ();
298
298
adLoader .loadAd (new AdManagerAdRequest .Builder ().build ());
299
-
300
- customControlsView .reset ();
301
299
}
302
300
}
0 commit comments