@@ -56,16 +56,14 @@ public class AdManagerCustomControlsFragment extends Fragment {
5656 private CheckBox customControlsCheckbox ;
5757 private CheckBox nativeAdsCheckbox ;
5858 private CheckBox customFormatAdsCheckbox ;
59- private CustomControlsView customControlsView ;
6059 private NativeAd nativeAd ;
6160 private NativeCustomFormatAd nativeCustomFormatAd ;
6261
63- public AdManagerCustomControlsFragment () {
64- }
62+ public AdManagerCustomControlsFragment () {}
6563
6664 @ Override
67- public View onCreateView (LayoutInflater inflater , ViewGroup container ,
68- Bundle savedInstanceState ) {
65+ public View onCreateView (
66+ LayoutInflater inflater , ViewGroup container , Bundle savedInstanceState ) {
6967 return inflater .inflate (R .layout .fragment_gam_customcontrols , container , false );
7068 }
7169
@@ -77,7 +75,6 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat
7775 customControlsCheckbox = view .findViewById (R .id .cb_custom_controls );
7876 nativeAdsCheckbox = view .findViewById (R .id .cb_native );
7977 customFormatAdsCheckbox = view .findViewById (R .id .cb_custom_format );
80- customControlsView = view .findViewById (R .id .custom_controls );
8178
8279 refresh .setOnClickListener (unusedView -> refreshAd ());
8380
@@ -111,6 +108,7 @@ private void populateNativeAdView(NativeAd nativeAd, NativeAdView adView) {
111108 adView .setPriceView (adView .findViewById (R .id .ad_price ));
112109 adView .setStarRatingView (adView .findViewById (R .id .ad_stars ));
113110 adView .setStoreView (adView .findViewById (R .id .ad_store ));
111+ adView .setMediaView (adView .findViewById (R .id .ad_media ));
114112
115113 // Some assets are guaranteed to be in every NativeAd.
116114 ((TextView ) adView .getHeadlineView ()).setText (nativeAd .getHeadline ());
@@ -137,20 +135,20 @@ private void populateNativeAdView(NativeAd nativeAd, NativeAdView adView) {
137135 if (nativeAd .getStarRating () == null ) {
138136 adView .getStarRatingView ().setVisibility (View .INVISIBLE );
139137 } else {
140- ((RatingBar ) adView .getStarRatingView ())
141- .setRating (nativeAd .getStarRating ().floatValue ());
138+ ((RatingBar ) adView .getStarRatingView ()).setRating (nativeAd .getStarRating ().floatValue ());
142139 adView .getStarRatingView ().setVisibility (View .VISIBLE );
143140 }
144141
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.
149143 MediaContent mediaContent = nativeAd .getMediaContent ();
150144 if (mediaContent != null ) {
151- customControlsView .setMediaContent (mediaContent );
145+ CustomControlsView customControls = adView .findViewById (R .id .custom_video_controls );
146+ customControls .initialize (mediaContent , startVideoAdsMuted .isChecked ());
152147 }
153148
149+ // Assign native ad object to the native view.
150+ adView .setNativeAd (nativeAd );
151+
154152 refresh .setEnabled (true );
155153 }
156154
@@ -161,8 +159,8 @@ private void populateNativeAdView(NativeAd nativeAd, NativeAdView adView) {
161159 * @param nativeCustomFormatAd the object containing the ad's assets
162160 * @param adView the view to be populated
163161 */
164- private void populateSimpleTemplateAdView (final NativeCustomFormatAd nativeCustomFormatAd ,
165- View adView ) {
162+ private void populateSimpleTemplateAdView (
163+ final NativeCustomFormatAd nativeCustomFormatAd , View adView ) {
166164 TextView headline = adView .findViewById (R .id .simplecustom_headline );
167165 TextView caption = adView .findViewById (R .id .simplecustom_caption );
168166
@@ -175,11 +173,15 @@ private void populateSimpleTemplateAdView(final NativeCustomFormatAd nativeCusto
175173
176174 // Apps can check the MediaContent's hasVideoContent property to determine if the
177175 // 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 ()) {
180178 MediaView mediaView = new MediaView (requireActivity ());
181- mediaView .setMediaContent (nativeCustomFormatAd . getMediaContent () );
179+ mediaView .setMediaContent (mediaContent );
182180 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 );
183185 } else {
184186 ImageView mainImage = new ImageView (getActivity ());
185187 mainImage .setAdjustViewBounds (true );
@@ -188,12 +190,10 @@ private void populateSimpleTemplateAdView(final NativeCustomFormatAd nativeCusto
188190 mainImage .setOnClickListener (unusedView -> nativeCustomFormatAd .performClick ("MainImage" ));
189191 mediaPlaceholder .addView (mainImage );
190192 }
191- customControlsView .setMediaContent (nativeCustomFormatAd .getMediaContent ());
192193
193194 refresh .setEnabled (true );
194195 }
195196
196-
197197 /**
198198 * Creates a request for a new native ad based on the boolean parameters and calls the
199199 * corresponding "populate" method when one is successfully returned.
@@ -202,8 +202,9 @@ private void refreshAd() {
202202 refresh .setEnabled (false );
203203
204204 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 ));
207208
208209 if (customFormatAdsCheckbox .isChecked ()) {
209210 builder .forCustomFormatAd (
@@ -264,14 +265,13 @@ public void onNativeAdLoaded(NativeAd ad) {
264265 });
265266 }
266267
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 ();
271273
272- NativeAdOptions adOptions = new NativeAdOptions .Builder ()
273- .setVideoOptions (videoOptions )
274- .build ();
274+ NativeAdOptions adOptions = new NativeAdOptions .Builder ().setVideoOptions (videoOptions ).build ();
275275
276276 builder .withNativeAdOptions (adOptions );
277277
@@ -296,7 +296,5 @@ public void onAdFailedToLoad(LoadAdError loadAdError) {
296296 })
297297 .build ();
298298 adLoader .loadAd (new AdManagerAdRequest .Builder ().build ());
299-
300- customControlsView .reset ();
301299 }
302300}
0 commit comments