NativeAppInstallAdViewHolder(View view) { super(view); NativeAppInstallAdView adView = (NativeAppInstallAdView) view; // The MediaView will display a video asset if one is present in the ad, and the // first image asset otherwise. MediaView mediaView = (MediaView) adView.findViewById(R.id.appinstall_media); adView.setMediaView(mediaView); // Register the view used for each individual asset. adView.setHeadlineView(adView.findViewById(R.id.appinstall_headline)); adView.setBodyView(adView.findViewById(R.id.appinstall_body)); adView.setCallToActionView(adView.findViewById(R.id.appinstall_call_to_action)); adView.setIconView(adView.findViewById(R.id.appinstall_app_icon)); adView.setPriceView(adView.findViewById(R.id.appinstall_price)); adView.setStarRatingView(adView.findViewById(R.id.appinstall_stars)); adView.setStoreView(adView.findViewById(R.id.appinstall_store)); }
/** * Populates a {@link NativeAppInstallAdView} object with data from a given * {@link NativeAppInstallAd}. * * @param nativeAppInstallAd the object containing the ad's assets * @param adView the view to be populated */ private void populateAppInstallAdView(NativeAppInstallAd nativeAppInstallAd, NativeAppInstallAdView adView) { VideoController videoController = nativeAppInstallAd.getVideoController(); // Assign native ad object to the native view. adView.setNativeAd(nativeAppInstallAd); adView.setHeadlineView(adView.findViewById(R.id.appinstall_headline)); adView.setBodyView(adView.findViewById(R.id.appinstall_body)); adView.setCallToActionView(adView.findViewById(R.id.appinstall_call_to_action)); adView.setIconView(adView.findViewById(R.id.appinstall_app_icon)); adView.setPriceView(adView.findViewById(R.id.appinstall_price)); adView.setStarRatingView(adView.findViewById(R.id.appinstall_stars)); adView.setStoreView(adView.findViewById(R.id.appinstall_store)); // Some assets are guaranteed to be in every NativeAppInstallAd. ((TextView) adView.getHeadlineView()).setText(nativeAppInstallAd.getHeadline()); ((TextView) adView.getBodyView()).setText(nativeAppInstallAd.getBody()); ((Button) adView.getCallToActionView()).setText(nativeAppInstallAd.getCallToAction()); ((ImageView) adView.getIconView()).setImageDrawable(nativeAppInstallAd.getIcon() .getDrawable()); MediaView sampleMediaView = adView.findViewById(R.id.appinstall_media); ImageView imageView = adView.findViewById(R.id.appinstall_image); if (videoController.hasVideoContent()) { imageView.setVisibility(View.GONE); adView.setMediaView(sampleMediaView); } else { sampleMediaView.setVisibility(View.GONE); adView.setImageView(imageView); List<NativeAd.Image> images = nativeAppInstallAd.getImages(); if (images.size() > 0) { ((ImageView) adView.getImageView()).setImageDrawable(images.get(0).getDrawable()); } } // Some aren't guaranteed, however, and should be checked. if (nativeAppInstallAd.getPrice() == null) { adView.getPriceView().setVisibility(View.INVISIBLE); } else { adView.getPriceView().setVisibility(View.VISIBLE); ((TextView) adView.getPriceView()).setText(nativeAppInstallAd.getPrice()); } if (nativeAppInstallAd.getStore() == null) { adView.getStoreView().setVisibility(View.INVISIBLE); } else { adView.getStoreView().setVisibility(View.VISIBLE); ((TextView) adView.getStoreView()).setText(nativeAppInstallAd.getStore()); } if (nativeAppInstallAd.getStarRating() == null) { adView.getStarRatingView().setVisibility(View.INVISIBLE); } else { ((RatingBar) adView.getStarRatingView()) .setRating(nativeAppInstallAd.getStarRating().floatValue()); adView.getStarRatingView().setVisibility(View.VISIBLE); } // Handle the fact that this could be a Sample SDK native ad, which includes a // "degree of awesomeness" field. Bundle extras = nativeAppInstallAd.getExtras(); if (extras.containsKey(SampleCustomEvent.DEGREE_OF_AWESOMENESS)) { TextView degree = (TextView) adView.findViewById(R.id.appinstall_degreeofawesomeness); degree.setVisibility(View.VISIBLE); degree.setText(extras.getString(SampleCustomEvent.DEGREE_OF_AWESOMENESS)); } }
/** * Populates a {@link NativeContentAdView} object with data from a given * {@link NativeContentAd}. * * @param nativeContentAd the object containing the ad's assets * @param adView the view to be populated */ private void populateContentAdView(NativeContentAd nativeContentAd, NativeContentAdView adView) { adView.setHeadlineView(adView.findViewById(R.id.contentad_headline)); adView.setBodyView(adView.findViewById(R.id.contentad_body)); adView.setCallToActionView(adView.findViewById(R.id.contentad_call_to_action)); adView.setLogoView(adView.findViewById(R.id.contentad_logo)); adView.setAdvertiserView(adView.findViewById(R.id.contentad_advertiser)); // Some assets are guaranteed to be in every NativeContentAd. ((TextView) adView.getHeadlineView()).setText(nativeContentAd.getHeadline()); ((TextView) adView.getBodyView()).setText(nativeContentAd.getBody()); ((TextView) adView.getCallToActionView()).setText(nativeContentAd.getCallToAction()); ((TextView) adView.getAdvertiserView()).setText(nativeContentAd.getAdvertiser()); // Get the video controller for the ad. One will always be provided, even if the ad doesn't // have a video asset. VideoController vc = nativeContentAd.getVideoController(); MediaView mediaView = adView.findViewById(R.id.contentad_media); ImageView mainImageView = adView.findViewById(R.id.contentad_image); // Apps can check the VideoController's hasVideoContent property to determine if the // NativeContentAd has a video asset. if (vc.hasVideoContent()) { mainImageView.setVisibility(View.GONE); adView.setMediaView(mediaView); mVideoStatus.setText(String.format(Locale.getDefault(), "Video status: Ad contains a %.2f:1 video asset.", vc.getAspectRatio())); // Create a new VideoLifecycleCallbacks object and pass it to the VideoController. The // VideoController will call methods on this object when events occur in the video // lifecycle. vc.setVideoLifecycleCallbacks(new VideoController.VideoLifecycleCallbacks() { public void onVideoEnd() { // Publishers should allow native ads to complete video playback before // refreshing or replacing them with another ad in the same UI location. mRefresh.setEnabled(true); mVideoStatus.setText("Video status: Video playback has ended."); super.onVideoEnd(); } }); } else { mediaView.setVisibility(View.GONE); adView.setImageView(mainImageView); // At least one image is guaranteed. List<NativeAd.Image> images = nativeContentAd.getImages(); mainImageView.setImageDrawable(images.get(0).getDrawable()); mRefresh.setEnabled(true); mVideoStatus.setText("Video status: Ad does not contain a video asset."); } // These assets aren't guaranteed to be in every NativeContentAd, so it's important to // check before trying to display them. NativeAd.Image logoImage = nativeContentAd.getLogo(); if (logoImage == null) { adView.getLogoView().setVisibility(View.INVISIBLE); } else { ((ImageView) adView.getLogoView()).setImageDrawable(logoImage.getDrawable()); adView.getLogoView().setVisibility(View.VISIBLE); } // Assign native ad object to the native view. adView.setNativeAd(nativeContentAd); }