/** * Populates the asset {@link View}s with data * from the {@link NativeCustomTemplateAd} object. This method is invoked when an * {@link SimpleCustomTemplateAdFetcher} has successfully loaded a * {@link NativeCustomTemplateAd}. * * @param customTemplateAd the ad that is to be displayed */ public void populateView(final NativeCustomTemplateAd customTemplateAd) { mHeadline.setText(customTemplateAd.getText(HEADLINE_TEMPLATE_FIELD_NAME)); mCaption.setText(customTemplateAd.getText(CAPTION_TEMPLATE_FIELD_NAME)); mMainImage.setImageDrawable( customTemplateAd.getImage(MAINIMAGE_TEMPLATE_FIELD_NAME).getDrawable()); mMainImage.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { customTemplateAd.performClick(MAINIMAGE_TEMPLATE_FIELD_NAME); } }); mAdView.setVisibility(View.VISIBLE); }
/** * Populates the asset {@link View}s with data from the {@link NativeCustomTemplateAd} object. * This method is invoked when an {@link SimpleCustomTemplateAdFetcher} has successfully loaded * a {@link NativeCustomTemplateAd}. * * @param customTemplateAd the ad that is to be displayed */ public void populateView(final NativeCustomTemplateAd customTemplateAd) { mHeadline.setText(customTemplateAd.getText(HEADLINE_TEMPLATE_FIELD_NAME)); mCaption.setText(customTemplateAd.getText(CAPTION_TEMPLATE_FIELD_NAME)); mBody.setText(customTemplateAd.getText(BODY_TEMPLATE_FIELD_NAME)); mCallToAction.setText(customTemplateAd.getText(CALLTOACTION_TEMPLATE_FIELD_NAME)); mMainImage.setImageDrawable( customTemplateAd.getImage(MAINIMAGE_TEMPLATE_FIELD_NAME).getDrawable()); mCallToAction.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { customTemplateAd.performClick(CALLTOACTION_TEMPLATE_FIELD_NAME); } }); mAdView.setVisibility(View.VISIBLE); }
/** * Processes the custom click events from the {@link NativeCustomTemplateAd}. In this case, a * {@link Toast} is displayed, but other applications might change UI elements or enable/disable * inputs in response to these click events. * * @param customTemplateAd the ad object that's invoking the method * @param assetName the name of the asset that was clicked by the user */ public void processClick(NativeCustomTemplateAd customTemplateAd, String assetName) { Context context = mAdView.getContext(); String messageFormatString = context.getResources().getString(R.string.custom_click_toast); String message = String.format(messageFormatString, assetName, customTemplateAd.getCustomTemplateId()); Toast.makeText(context, message, Toast.LENGTH_SHORT).show(); }
/** * Populates a {@link View} object with data from a {@link NativeCustomTemplateAd}. This method * handles a particular "simple" custom native ad format. * * @param nativeCustomTemplateAd the object containing the ad's assets * @param adView the view to be populated */ private void populateSimpleTemplateAdView(final NativeCustomTemplateAd nativeCustomTemplateAd, View adView) { TextView headline = adView.findViewById(R.id.simplecustom_headline); TextView caption = adView.findViewById(R.id.simplecustom_caption); headline.setText(nativeCustomTemplateAd.getText("Headline")); caption.setText(nativeCustomTemplateAd.getText("Caption")); FrameLayout mediaPlaceholder = adView.findViewById(R.id.simplecustom_media_placeholder); // Get the video controller for the ad. One will always be provided, even if the ad doesn't // have a video asset. VideoController vc = nativeCustomTemplateAd.getVideoController(); // Apps can check the VideoController's hasVideoContent property to determine if the // NativeCustomTemplateAd has a video asset. if (vc.hasVideoContent()) { mediaPlaceholder.addView(nativeCustomTemplateAd.getVideoMediaView()); 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 { ImageView mainImage = new ImageView(this); mainImage.setAdjustViewBounds(true); mainImage.setImageDrawable(nativeCustomTemplateAd.getImage("MainImage").getDrawable()); mainImage.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { nativeCustomTemplateAd.performClick("MainImage"); } }); mediaPlaceholder.addView(mainImage); mRefresh.setEnabled(true); mVideoStatus.setText("Video status: Ad does not contain a video asset."); } }