Search in sources :

Example 1 with MediaView

use of com.google.android.gms.ads.nativead.MediaView in project mopub-android-mediation by mopub.

the class GooglePlayServicesAdRenderer method updateNativeAdview.

/**
 * This method will render the given native ad view using the native ad and set the views to
 * Google's native ad view.
 *
 * @param staticNativeAd         a static native ad object containing the required assets to
 *                               set to the native ad view.
 * @param staticNativeViewHolder a static native view holder object containing the mapped
 *                               views from the view binder.
 * @param nativeAdView           the Google native ad view in the view hierarchy.
 */
private void updateNativeAdview(GooglePlayServicesNativeAd staticNativeAd, GoogleStaticNativeViewHolder staticNativeViewHolder, NativeAdView nativeAdView) {
    NativeRendererHelper.addTextView(staticNativeViewHolder.mTitleView, staticNativeAd.getTitle());
    nativeAdView.setHeadlineView(staticNativeViewHolder.mTitleView);
    NativeRendererHelper.addTextView(staticNativeViewHolder.mTextView, staticNativeAd.getText());
    nativeAdView.setBodyView(staticNativeViewHolder.mTextView);
    if (staticNativeViewHolder.mMediaView != null) {
        MediaView mediaview = new MediaView(nativeAdView.getContext());
        mediaview.setImageScaleType(ScaleType.CENTER_CROP);
        staticNativeViewHolder.mMediaView.removeAllViews();
        staticNativeViewHolder.mMediaView.addView(mediaview);
        nativeAdView.setMediaView(mediaview);
    }
    NativeRendererHelper.addTextView(staticNativeViewHolder.mCallToActionView, staticNativeAd.getCallToAction());
    nativeAdView.setCallToActionView(staticNativeViewHolder.mCallToActionView);
    if (staticNativeAd.getIconImageUrl() != null) {
        NativeImageHelper.loadImageView(staticNativeAd.getIconImageUrl(), staticNativeViewHolder.mIconImageView);
        nativeAdView.setImageView(staticNativeViewHolder.mIconImageView);
    }
    if (staticNativeAd.getAdvertiser() != null) {
        NativeRendererHelper.addTextView(staticNativeViewHolder.mAdvertiserTextView, staticNativeAd.getAdvertiser());
        nativeAdView.setAdvertiserView(staticNativeViewHolder.mAdvertiserTextView);
    }
    // Add the AdChoices icon to the container if one is provided by the publisher.
    if (staticNativeViewHolder.mAdChoicesIconContainer != null) {
        AdChoicesView adChoicesView = new AdChoicesView(nativeAdView.getContext());
        staticNativeViewHolder.mAdChoicesIconContainer.removeAllViews();
        staticNativeViewHolder.mAdChoicesIconContainer.addView(adChoicesView);
        nativeAdView.setAdChoicesView(adChoicesView);
    }
    // Set the privacy information icon to null as the Google Mobile Ads SDK automatically
    // renders the AdChoices icon.
    NativeRendererHelper.addPrivacyInformationIcon(staticNativeViewHolder.mPrivacyInformationIconImageView, null, null);
    nativeAdView.setNativeAd(staticNativeAd.getNativeAd());
}
Also used : AdChoicesView(com.google.android.gms.ads.nativead.AdChoicesView) MediaView(com.google.android.gms.ads.nativead.MediaView)

Example 2 with MediaView

use of com.google.android.gms.ads.nativead.MediaView in project googleads-mobile-android-mediation by googleads.

the class MainActivity method populateNativeAdView.

/**
 * Populates a {@link NativeAdView} object with data from a given {@link NativeAd}.
 *
 * @param nativeAd the object containing the ad's assets
 * @param adView the view to be populated
 */
private void populateNativeAdView(NativeAd nativeAd, NativeAdView adView) {
    // Set the media view. Media content will be automatically populated in the media view once
    // adView.setNativeAd() is called.
    MediaView mediaView = adView.findViewById(R.id.ad_media);
    adView.setMediaView(mediaView);
    // Set other ad assets.
    adView.setHeadlineView(adView.findViewById(R.id.ad_headline));
    adView.setBodyView(adView.findViewById(R.id.ad_body));
    adView.setCallToActionView(adView.findViewById(R.id.ad_call_to_action));
    adView.setIconView(adView.findViewById(R.id.ad_app_icon));
    adView.setPriceView(adView.findViewById(R.id.ad_price));
    adView.setStarRatingView(adView.findViewById(R.id.ad_stars));
    adView.setStoreView(adView.findViewById(R.id.ad_store));
    adView.setAdvertiserView(adView.findViewById(R.id.ad_advertiser));
    // The headline is guaranteed to be in every NativeAd.
    ((TextView) adView.getHeadlineView()).setText(nativeAd.getHeadline());
    // check before trying to display them.
    if (nativeAd.getBody() == null) {
        adView.getBodyView().setVisibility(View.INVISIBLE);
    } else {
        adView.getBodyView().setVisibility(View.VISIBLE);
        ((TextView) adView.getBodyView()).setText(nativeAd.getBody());
    }
    if (nativeAd.getCallToAction() == null) {
        adView.getCallToActionView().setVisibility(View.INVISIBLE);
    } else {
        adView.getCallToActionView().setVisibility(View.VISIBLE);
        ((Button) adView.getCallToActionView()).setText(nativeAd.getCallToAction());
    }
    if (nativeAd.getIcon() == null) {
        adView.getIconView().setVisibility(View.GONE);
    } else {
        ((ImageView) adView.getIconView()).setImageDrawable(nativeAd.getIcon().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 || nativeAd.getStarRating() < 3) {
        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);
    }
    // This method tells the Google Mobile Ads SDK that you have finished populating your
    // native ad view with this native ad. The SDK will populate the adView's MediaView
    // with the media content from this native ad.
    adView.setNativeAd(nativeAd);
}
Also used : RadioButton(android.widget.RadioButton) Button(android.widget.Button) TextView(android.widget.TextView) ImageView(android.widget.ImageView) RatingBar(android.widget.RatingBar) MediaView(com.google.android.gms.ads.nativead.MediaView)

Aggregations

MediaView (com.google.android.gms.ads.nativead.MediaView)2 Button (android.widget.Button)1 ImageView (android.widget.ImageView)1 RadioButton (android.widget.RadioButton)1 RatingBar (android.widget.RatingBar)1 TextView (android.widget.TextView)1 AdChoicesView (com.google.android.gms.ads.nativead.AdChoicesView)1