Java Code Examples for com.google.android.gms.ads.formats.NativeAd#Image

The following examples show how to use com.google.android.gms.ads.formats.NativeAd#Image . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: StoriesOverViewAdapter.java    From Instagram-Profile-Downloader with MIT License 6 votes vote down vote up
private void populateNativeAdView(UnifiedNativeAd nativeAd,
                                  UnifiedNativeAdView adView) {
    // Some assets are guaranteed to be in every UnifiedNativeAd.
    ((TextView) adView.getHeadlineView()).setText(nativeAd.getHeadline());
    ((TextView) adView.getBodyView()).setText(nativeAd.getBody());
    ((Button) adView.getCallToActionView()).setText(nativeAd.getCallToAction());

    // These assets aren't guaranteed to be in every UnifiedNativeAd, so it's important to
    // check before trying to display them.
    NativeAd.Image icon = nativeAd.getIcon();

    if (icon == null) {
        adView.getIconView().setVisibility(View.INVISIBLE);
    } else {
        ((ImageView) adView.getIconView()).setImageDrawable(icon.getDrawable());
        adView.getIconView().setVisibility(View.VISIBLE);
    }


    // Assign native ad object to the native view.
    adView.setNativeAd(nativeAd);
}
 
Example 2
Source File: StoriesListAdapter.java    From Instagram-Profile-Downloader with MIT License 6 votes vote down vote up
private void populateNativeAdView(UnifiedNativeAd nativeAd,
                                  UnifiedNativeAdView adView) {
    // Some assets are guaranteed to be in every UnifiedNativeAd.
    ((TextView) adView.getHeadlineView()).setText(nativeAd.getHeadline());
    ((Button) adView.getCallToActionView()).setText(nativeAd.getCallToAction());

    // These assets aren't guaranteed to be in every UnifiedNativeAd, so it's important to
    // check before trying to display them.
    NativeAd.Image icon = nativeAd.getIcon();

    if (icon == null) {
        adView.getIconView().setVisibility(View.INVISIBLE);
    } else {
        ((ImageView) adView.getIconView()).setImageDrawable(icon.getDrawable());
        adView.getIconView().setVisibility(View.VISIBLE);
    }


    // Assign native ad object to the native view.
    adView.setNativeAd(nativeAd);
}
 
Example 3
Source File: IMobileUnifiedNativeAdMapper.java    From googleads-mobile-android-mediation with Apache License 2.0 6 votes vote down vote up
public IMobileUnifiedNativeAdMapper(
    @NonNull ImobileSdkAdsNativeAdData adData, @NonNull Drawable adImage) {
  // Initialize fields.
  this.clickEvent = adData.getClickEvent();

  // Set ad image.
  List<NativeAd.Image> images = new ArrayList<>(1);
  images.add(new NativeAdImage(adImage, null, 1));
  setImages(images);
  int height = adImage.getIntrinsicHeight();
  if (height > 0) {
    setMediaContentAspectRatio(adImage.getIntrinsicWidth() / height);
  }

  // Set ad data.
  setAdvertiser(adData.getSponsored());
  setBody(adData.getDescription());
  setCallToAction(Constants.CALL_TO_ACTION);
  setHeadline(adData.getTitle());

  // Created a transparent drawable as i-mobile do not render AdIcon.
  setIcon(new NativeAdImage(new ColorDrawable(Color.TRANSPARENT), null, 1));
}
 
Example 4
Source File: SampleUnifiedNativeAdMapper.java    From googleads-mobile-android-mediation with Apache License 2.0 5 votes vote down vote up
public SampleUnifiedNativeAdMapper(SampleNativeAd ad) {
  sampleAd = ad;
  setHeadline(sampleAd.getHeadline());
  setBody(sampleAd.getBody());
  setCallToAction(sampleAd.getCallToAction());
  setStarRating(sampleAd.getStarRating());
  setStore(sampleAd.getStoreName());
  setIcon(new SampleNativeMappedImage(ad.getIcon(), ad.getIconUri(),
      SampleCustomEvent.SAMPLE_SDK_IMAGE_SCALE));
  setAdvertiser(ad.getAdvertiser());

  List<NativeAd.Image> imagesList = new ArrayList<NativeAd.Image>();
  imagesList.add(new SampleNativeMappedImage(ad.getImage(), ad.getImageUri(),
      SampleCustomEvent.SAMPLE_SDK_IMAGE_SCALE));
  setImages(imagesList);

  if (sampleAd.getPrice() != null) {
    NumberFormat formatter = NumberFormat.getCurrencyInstance();
    String priceString = formatter.format(sampleAd.getPrice());
    setPrice(priceString);
  }

  Bundle extras = new Bundle();
  extras.putString(SampleCustomEvent.DEGREE_OF_AWESOMENESS, ad.getDegreeOfAwesomeness());
  this.setExtras(extras);

  setOverrideClickHandling(false);
  setOverrideImpressionRecording(false);

  setAdChoicesContent(sampleAd.getInformationIcon());
}
 
Example 5
Source File: NendUnifiedNativeNormalAdMapper.java    From googleads-mobile-android-mediation with Apache License 2.0 5 votes vote down vote up
NendUnifiedNativeNormalAdMapper(
    Context context,
    NendNativeAdForwarder forwarder,
    NendAdNative ad,
    NendNativeMappedImage adImage,
    NendNativeMappedImage logoImage) {
  super(logoImage);
  this.forwarder = forwarder;
  nendAd = ad;
  connector = NendNativeAdConnectorFactory.createNativeAdConnector(ad);

  setAdvertiser(ad.getPromotionName());
  setHeadline(ad.getTitleText());
  setBody(ad.getContentText());
  setCallToAction(ad.getActionText());

  ImageView imageView = new ImageView(context);
  if (adImage == null) {
    Log.w(TAG, "Missing Image of nend's native ad, so MediaView will be unavailable...");
  } else {
    List<NativeAd.Image> imagesList = new ArrayList<>();
    imagesList.add(adImage);
    setImages(imagesList);

    Drawable drawable = adImage.getDrawable();
    if (drawable != null) {
      imageView.setAdjustViewBounds(true);
      imageView.setImageDrawable(drawable);
    }
  }
  setMediaView(imageView);
  adChoicesMappingView = new TextView(context);
  adChoicesMappingView.setText(NendAdNative.AdvertisingExplicitly.PR.getText());
  setAdChoicesContent(adChoicesMappingView);

  nendAd.setNendAdNativeListener(this);
}
 
Example 6
Source File: MoPubNativeAppInstallAdMapper.java    From googleads-mobile-android-mediation with Apache License 2.0 5 votes vote down vote up
public MoPubNativeAppInstallAdMapper(@NonNull Context context,
    @NonNull StaticNativeAd ad,
    @Nullable Drawable icon,
    @Nullable Drawable nativeAdMainImage,
    int privacyIconPlacementParam,
    int privacyIconSize) {
  mMoPubNativeAdData = ad;
  setHeadline(mMoPubNativeAdData.getTitle());
  setBody(mMoPubNativeAdData.getText());
  setCallToAction(mMoPubNativeAdData.getCallToAction());
  privacyIconPlacement = privacyIconPlacementParam;
  mPrivacyIconSize = privacyIconSize;

  MoPubNativeMappedImage iconImage = new MoPubNativeMappedImage(icon,
      mMoPubNativeAdData.getIconImageUrl(), MoPubAdapter.DEFAULT_MOPUB_IMAGE_SCALE);
  setIcon(iconImage);
  MoPubNativeMappedImage mainImage = new MoPubNativeMappedImage(
      nativeAdMainImage, mMoPubNativeAdData.getMainImageUrl(),
      MoPubAdapter.DEFAULT_MOPUB_IMAGE_SCALE);
  List<NativeAd.Image> imagesList = new ArrayList<NativeAd.Image>();
  imagesList.add(mainImage);
  setImages(imagesList);
  ImageView mediaView = new ImageView(context);
  mediaView.setImageDrawable(nativeAdMainImage);
  setMediaView(mediaView);
  setOverrideClickHandling(true);
  setOverrideImpressionRecording(true);
}
 
Example 7
Source File: AppLovinNativeAdMapper.java    From googleads-mobile-android-mediation with Apache License 2.0 5 votes vote down vote up
AppLovinNativeAdMapper(AppLovinNativeAd nativeAd, Context context) {
  mNativeAd = nativeAd;
  setHeadline(nativeAd.getTitle());
  setBody(nativeAd.getDescriptionText());
  setCallToAction(nativeAd.getCtaText());

  ImageView mediaView = new ImageView(context);
  ViewGroup.LayoutParams layoutParams =
      new ViewGroup.LayoutParams(
          ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
  mediaView.setLayoutParams(layoutParams);

  ArrayList<NativeAd.Image> images = new ArrayList<>(1);
  Uri imageUri = Uri.parse(nativeAd.getImageUrl());
  Drawable imageDrawable = Drawable.createFromPath(imageUri.getPath());

  Uri iconUri = Uri.parse(nativeAd.getIconUrl());
  Drawable iconDrawable = Drawable.createFromPath(iconUri.getPath());

  AppLovinNativeAdImage image = new AppLovinNativeAdImage(imageUri, imageDrawable);
  AppLovinNativeAdImage icon = new AppLovinNativeAdImage(iconUri, iconDrawable);

  images.add(image);
  setImages(images);
  setIcon(icon);

  mediaView.setImageDrawable(imageDrawable);
  setMediaView(mediaView);
  setStarRating(nativeAd.getStarRating());

  Bundle extraAssets = new Bundle();
  extraAssets.putLong(AppLovinNativeAdapter.KEY_EXTRA_AD_ID, nativeAd.getAdId());
  extraAssets.putString(AppLovinNativeAdapter.KEY_EXTRA_CAPTION_TEXT, nativeAd.getCaptionText());
  setExtras(extraAssets);

  setOverrideClickHandling(false);
  setOverrideImpressionRecording(false);
}
 
Example 8
Source File: RecyclerViewAdapter.java    From admob-native-advanced-feed with Apache License 2.0 4 votes vote down vote up
private void populateNativeAdView(UnifiedNativeAd nativeAd,
                                  UnifiedNativeAdView adView) {
    // Some assets are guaranteed to be in every UnifiedNativeAd.
    ((TextView) adView.getHeadlineView()).setText(nativeAd.getHeadline());
    ((TextView) adView.getBodyView()).setText(nativeAd.getBody());
    ((Button) adView.getCallToActionView()).setText(nativeAd.getCallToAction());

    // These assets aren't guaranteed to be in every UnifiedNativeAd, so it's important to
    // check before trying to display them.
    NativeAd.Image icon = nativeAd.getIcon();

    if (icon == null) {
        adView.getIconView().setVisibility(View.INVISIBLE);
    } else {
        ((ImageView) adView.getIconView()).setImageDrawable(icon.getDrawable());
        adView.getIconView().setVisibility(View.VISIBLE);
    }

    if (nativeAd.getPrice() == null) {
        adView.getPriceView().setVisibility(View.INVISIBLE);
    } else {
        adView.getPriceView().setVisibility(View.VISIBLE);
        ((TextView) adView.getPriceView()).setText(nativeAd.getPrice());
    }

    if (nativeAd.getStore() == null) {
        adView.getStoreView().setVisibility(View.INVISIBLE);
    } else {
        adView.getStoreView().setVisibility(View.VISIBLE);
        ((TextView) adView.getStoreView()).setText(nativeAd.getStore());
    }

    if (nativeAd.getStarRating() == null) {
        adView.getStarRatingView().setVisibility(View.INVISIBLE);
    } else {
        ((RatingBar) adView.getStarRatingView())
            .setRating(nativeAd.getStarRating().floatValue());
        adView.getStarRatingView().setVisibility(View.VISIBLE);
    }

    if (nativeAd.getAdvertiser() == null) {
        adView.getAdvertiserView().setVisibility(View.INVISIBLE);
    } else {
        ((TextView) adView.getAdvertiserView()).setText(nativeAd.getAdvertiser());
        adView.getAdvertiserView().setVisibility(View.VISIBLE);
    }

    // Assign native ad object to the native view.
    adView.setNativeAd(nativeAd);
}
 
Example 9
Source File: SampleUnifiedNativeAdMapper.java    From googleads-mobile-android-mediation with Apache License 2.0 4 votes vote down vote up
public SampleUnifiedNativeAdMapper(SampleNativeAd ad) {
  sampleAd = ad;
  setHeadline(sampleAd.getHeadline());
  setBody(sampleAd.getBody());
  setCallToAction(sampleAd.getCallToAction());
  setStarRating(sampleAd.getStarRating());
  setStore(sampleAd.getStoreName());
  setIcon(new SampleNativeMappedImage(ad.getIcon(), ad.getIconUri(),
      SampleAdapter.SAMPLE_SDK_IMAGE_SCALE));
  setAdvertiser(ad.getAdvertiser());

  List<NativeAd.Image> imagesList = new ArrayList<NativeAd.Image>();
  imagesList.add(new SampleNativeMappedImage(ad.getImage(), ad.getImageUri(),
      SampleAdapter.SAMPLE_SDK_IMAGE_SCALE));
  setImages(imagesList);

  if (sampleAd.getPrice() != null) {
    NumberFormat formatter = NumberFormat.getCurrencyInstance();
    String priceString = formatter.format(sampleAd.getPrice());
    setPrice(priceString);
  }

  Bundle extras = new Bundle();
  extras.putString(SampleAdapter.DEGREE_OF_AWESOMENESS, ad.getDegreeOfAwesomeness());
  this.setExtras(extras);

  SampleMediaView mediaView = sampleAd.getMediaView();

  // Some ads from Sample SDK have video assets and some do not.
  if (mediaView != null) {
    setMediaView(mediaView);
    setHasVideoContent(true);
  } else {
    setHasVideoContent(false);
  }

  setOverrideClickHandling(false);
  setOverrideImpressionRecording(false);

  setAdChoicesContent(sampleAd.getInformationIcon());
}
 
Example 10
Source File: MoPubUnifiedNativeAdMapper.java    From googleads-mobile-android-mediation with Apache License 2.0 4 votes vote down vote up
public MoPubUnifiedNativeAdMapper(@NonNull Context context,
    @NonNull StaticNativeAd ad,
    @Nullable Drawable icon,
    @Nullable Drawable nativeAdMainImage,
    int privacyIconPlacementParam,
    int privacyIconSize) {
  mMoPubNativeAdData = ad;
  setHeadline(mMoPubNativeAdData.getTitle());

  setBody(mMoPubNativeAdData.getText());

  setCallToAction(mMoPubNativeAdData.getCallToAction());
  privacyIconPlacement = privacyIconPlacementParam;
  mPrivacyIconSize = privacyIconSize;

  MoPubNativeMappedImage iconImage = new MoPubNativeMappedImage(icon,
      mMoPubNativeAdData.getIconImageUrl(), MoPubAdapter.DEFAULT_MOPUB_IMAGE_SCALE);

  setIcon(iconImage);

  MoPubNativeMappedImage mainImage = new MoPubNativeMappedImage(
      nativeAdMainImage, mMoPubNativeAdData.getMainImageUrl(),
      MoPubAdapter.DEFAULT_MOPUB_IMAGE_SCALE);

  List<NativeAd.Image> imagesList = new ArrayList<NativeAd.Image>();
  imagesList.add(mainImage);
  setImages(imagesList);

  int height = mainImage.getHeight();
  int width = mainImage.getWidth();
  float aspectRatio = 0.0f;
  if (height > 0) {
    aspectRatio = (float) (width / height);
  }

  setMediaContentAspectRatio(aspectRatio);

  ImageView mediaView = new ImageView(context);
  mediaView.setImageDrawable(nativeAdMainImage);
  setMediaView(mediaView);

  setOverrideClickHandling(true);

  setOverrideImpressionRecording(true);
}
 
Example 11
Source File: AppLovinUnifiedNativeAdMapper.java    From googleads-mobile-android-mediation with Apache License 2.0 4 votes vote down vote up
public AppLovinUnifiedNativeAdMapper(Context context, AppLovinNativeAd nativeAd) {
  mNativeAd = nativeAd;
  setHeadline(mNativeAd.getTitle());
  setBody(mNativeAd.getDescriptionText());
  setCallToAction(mNativeAd.getCtaText());

  final ImageView mediaView = new ImageView(context);
  ViewGroup.LayoutParams layoutParams =
      new ViewGroup.LayoutParams(
          ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
  mediaView.setLayoutParams(layoutParams);

  ArrayList<NativeAd.Image> images = new ArrayList<>(1);
  Uri imageUri = Uri.parse(mNativeAd.getImageUrl());
  Drawable imageDrawable = Drawable.createFromPath(imageUri.getPath());

  Uri iconUri = Uri.parse(mNativeAd.getIconUrl());
  Drawable iconDrawable = Drawable.createFromPath(iconUri.getPath());

  AppLovinNativeAdImage image = new AppLovinNativeAdImage(imageUri, imageDrawable);
  AppLovinNativeAdImage icon = new AppLovinNativeAdImage(iconUri, iconDrawable);

  images.add(image);
  setImages(images);
  setIcon(icon);

  mediaView.setImageDrawable(imageDrawable);
  setMediaView(mediaView);
  int imageHeight = imageDrawable.getIntrinsicHeight();
  if (imageHeight > 0) {
    setMediaContentAspectRatio(imageDrawable.getIntrinsicWidth() / imageHeight);
  }
  setStarRating((double) mNativeAd.getStarRating());

  Bundle extraAssets = new Bundle();
  extraAssets.putLong(AppLovinNativeAdapter.KEY_EXTRA_AD_ID, mNativeAd.getAdId());
  extraAssets.putString(AppLovinNativeAdapter.KEY_EXTRA_CAPTION_TEXT, mNativeAd.getCaptionText());
  setExtras(extraAssets);

  setOverrideClickHandling(false);
  setOverrideImpressionRecording(false);
}
 
Example 12
Source File: DFPCustomControlsFragment.java    From android-ads with Apache License 2.0 4 votes vote down vote up
/**
 * 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) {
    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());

    // Get the video controller for the ad. One will always be provided, even if the ad doesn't
    // have a video asset.
    VideoController videoController = nativeAppInstallAd.getVideoController();

    MediaView mediaView = adView.findViewById(R.id.appinstall_media);
    ImageView mainImageView = adView.findViewById(R.id.appinstall_image);

    // Apps can check the VideoController's hasVideoContent property to determine if the
    // NativeAppInstallAd has a video asset.
    if (videoController.hasVideoContent()) {
        mainImageView.setVisibility(View.GONE);
        adView.setMediaView(mediaView);
    } else {
        mediaView.setVisibility(View.GONE);
        adView.setImageView(mainImageView);

        // At least one image is guaranteed.
        List<NativeAd.Image> images = nativeAppInstallAd.getImages();
        mainImageView.setImageDrawable(images.get(0).getDrawable());
    }

    // These assets aren't guaranteed to be in every NativeAppInstallAd, so it's important to
    // check before trying to display them.
    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);
    }


    // Assign native ad object to the native view.
    adView.setNativeAd(nativeAppInstallAd);

    customControlsView.setVideoController(videoController);

    refresh.setEnabled(true);
}
 
Example 13
Source File: DFPCustomControlsFragment.java    From android-ads with Apache License 2.0 4 votes vote down vote up
/**
 * 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 videoController = 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 (videoController.hasVideoContent()) {
        mainImageView.setVisibility(View.GONE);
        adView.setMediaView(mediaView);
    } 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());
    }

    // 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);
    customControlsView.setVideoController(videoController);

    refresh.setEnabled(true);
}
 
Example 14
Source File: DFPCustomControlsFragment.java    From googleads-mobile-android-examples with Apache License 2.0 4 votes vote down vote up
/**
 * 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) {
    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());

    // Get the video controller for the ad. One will always be provided, even if the ad doesn't
    // have a video asset.
    VideoController videoController = nativeAppInstallAd.getVideoController();

    MediaView mediaView = adView.findViewById(R.id.appinstall_media);
    ImageView mainImageView = adView.findViewById(R.id.appinstall_image);

    // Apps can check the VideoController's hasVideoContent property to determine if the
    // NativeAppInstallAd has a video asset.
    if (videoController.hasVideoContent()) {
        mainImageView.setVisibility(View.GONE);
        adView.setMediaView(mediaView);
    } else {
        mediaView.setVisibility(View.GONE);
        adView.setImageView(mainImageView);

        // At least one image is guaranteed.
        List<NativeAd.Image> images = nativeAppInstallAd.getImages();
        mainImageView.setImageDrawable(images.get(0).getDrawable());
    }

    // These assets aren't guaranteed to be in every NativeAppInstallAd, so it's important to
    // check before trying to display them.
    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);
    }


    // Assign native ad object to the native view.
    adView.setNativeAd(nativeAppInstallAd);

    customControlsView.setVideoController(videoController);

    refresh.setEnabled(true);
}
 
Example 15
Source File: DFPCustomControlsFragment.java    From googleads-mobile-android-examples with Apache License 2.0 4 votes vote down vote up
/**
 * 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 videoController = 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 (videoController.hasVideoContent()) {
        mainImageView.setVisibility(View.GONE);
        adView.setMediaView(mediaView);
    } 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());
    }

    // 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);
    customControlsView.setVideoController(videoController);

    refresh.setEnabled(true);
}
 
Example 16
Source File: AdMobNativeAdResponse.java    From mobile-sdk-android with Apache License 2.0 4 votes vote down vote up
private void loadAssets() {
    nativeElements.put(NATIVE_ELEMENT_OBJECT, nativeAd);
    if (nativeAd.getHeadline() != null) {
        title = nativeAd.getHeadline().toString();
    }
    if (nativeAd.getBody() != null) {
        description = nativeAd.getBody().toString();
    }
    if (nativeAd.getCallToAction() != null) {
        callToAction = nativeAd.getCallToAction().toString();
    }
    if (nativeAd.getIcon() != null) {
        NativeAd.Image iconImage = nativeAd.getIcon();
        if (iconImage.getUri() != null) {
            iconUrl = iconImage.getUri().toString();
        }
    }
    if(nativeAd.getImages() !=null) {
        List<NativeAd.Image> images = nativeAd.getImages();
        if (images != null && images.size() > 0) {
            NativeAd.Image image = images.get(0);
            if (image.getUri() != null) {
                imageUrl = image.getUri().toString();
            }
        }
    }
    if (nativeAd.getStarRating() != null && nativeAd.getStarRating() > 0) {
        rating = new Rating(nativeAd.getStarRating(), 5.0);
    }
    if (nativeAd.getStore() != null) {
        nativeElements.put(AdMobNativeSettings.NATIVE_ELEMENT_STORE_KEY, nativeAd.getStore().toString());
    }
    if (nativeAd.getPrice() != null) {
        nativeElements.put(AdMobNativeSettings.NATIVE_ELEMENT_PRICE_KEY, nativeAd.getPrice());
    }
    if (nativeAd.getAdvertiser() != null) {
        nativeElements.put(AdMobNativeSettings.NATIVE_ELEMENT_ADVERTISER_KEY, nativeAd.getAdvertiser().toString());
    }
    Bundle bundle = nativeAd.getExtras();
    if (bundle != null && bundle.size() > 0) {
        for (String key : bundle.keySet()) {
            nativeElements.put(key, bundle.get(key));
        }
    }
}