com.google.android.gms.ads.formats.NativeContentAd Java Examples

The following examples show how to use com.google.android.gms.ads.formats.NativeContentAd. 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: AdmobRecyclerAdapterWrapper.java    From admobadapter with Apache License 2.0 6 votes vote down vote up
@Override
public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
    if (viewHolder==null)
        return;
    int itemViewType = viewHolder.getItemViewType();
    if(itemViewType == getViewTypeAdInstall()) {
        NativeAppInstallAdView lvi1 = (NativeAppInstallAdView) viewHolder.itemView;
        NativeAppInstallAd ad1 = (NativeAppInstallAd) getItem(position);
        getInstallAdsLayoutContext().bind(lvi1, ad1);
    }
    else if(itemViewType == getViewTypeAdContent()) {
        NativeContentAdView lvi2 = (NativeContentAdView) viewHolder.itemView;
        NativeContentAd ad2 = (NativeContentAd) getItem(position);
        getContentAdsLayoutContext().bind(lvi2, ad2);
    }
    else{
        int origPos = AdapterCalculator.getOriginalContentPosition(position,
                adFetcher.getFetchedAdsCount(), mAdapter.getItemCount());
        mAdapter.onBindViewHolder(viewHolder, origPos);
    }
}
 
Example #2
Source File: AdmobFetcher.java    From admobadapter with Apache License 2.0 5 votes vote down vote up
/**
 * Subscribing to the native ads events
 */
protected synchronized void setupAds() {
    String unitId = getReleaseUnitId() != null ? getReleaseUnitId() : getDefaultUnitId();
    AdLoader.Builder adloaderBuilder = new AdLoader.Builder(mContext.get(), unitId)
            .withAdListener(new AdListener() {
                @Override
                public void onAdFailedToLoad(int errorCode) {
                    // Handle the failure by logging, altering the UI, etc.
                    Log.i(TAG, "onAdFailedToLoad " + errorCode);
                    lockFetch.set(false);
                    mFetchFailCount++;
                    mFetchingAdsCnt--;
                    ensurePrefetchAmount();
                    onAdFailed( mPrefetchedAdList.size(), errorCode, null);
                }
            })
            .withNativeAdOptions(new NativeAdOptions.Builder()
                    // Methods in the NativeAdOptions.Builder class can be
                    // used here to specify individual options settings.
                    .build());
    if(getAdTypeToFetch().contains(EAdType.ADVANCED_INSTALLAPP))
        adloaderBuilder.forAppInstallAd(new NativeAppInstallAd.OnAppInstallAdLoadedListener() {
                @Override
                public void onAppInstallAdLoaded(NativeAppInstallAd appInstallAd) {
                    onAdFetched(appInstallAd);
                }
            });
    if(getAdTypeToFetch().contains(EAdType.ADVANCED_CONTENT))
        adloaderBuilder.forContentAd(new NativeContentAd.OnContentAdLoadedListener() {
                @Override
                public void onContentAdLoaded(NativeContentAd contentAd) {
                    onAdFetched(contentAd);
                }
            });

    adLoader = adloaderBuilder.build();
}
 
Example #3
Source File: ContentAdLayoutContext.java    From admobadapter with Apache License 2.0 4 votes vote down vote up
@Override
public void bind(NativeAdView nativeAdView, NativeAd nativeAd) throws ClassCastException{
    if (nativeAdView == null || nativeAd == null) return;
    if(!(nativeAd instanceof NativeContentAd) || !(nativeAdView instanceof NativeContentAdView))
        throw new ClassCastException();

    NativeContentAd ad = (NativeContentAd) nativeAd;
    NativeContentAdView adView = (NativeContentAdView) nativeAdView;

    // Locate the view that will hold the headline, set its text, and call the
    // NativeContentAdView's setHeadlineView method to register it.
    TextView tvHeader = (TextView) nativeAdView.findViewById(R.id.tvHeader);
    tvHeader.setText(ad.getHeadline());
    adView.setHeadlineView(tvHeader);

    TextView tvDescription = (TextView) nativeAdView.findViewById(R.id.tvDescription);
    tvDescription.setText(ad.getBody());
    adView.setBodyView(tvDescription);

    ImageView ivLogo = (ImageView) nativeAdView.findViewById(R.id.ivLogo);
    if(ad.getLogo()!=null)
        ivLogo.setImageDrawable(ad.getLogo().getDrawable());
    adView.setLogoView(ivLogo);

    Button btnAction = (Button) nativeAdView.findViewById(R.id.btnAction);
    btnAction.setText(ad.getCallToAction());
    adView.setCallToActionView(btnAction);

    TextView tvAdvertiser = (TextView) nativeAdView.findViewById(R.id.tvAdvertiser);
    tvAdvertiser.setText(ad.getAdvertiser());
    adView.setAdvertiserView(tvAdvertiser);

    ImageView ivImage = (ImageView) nativeAdView.findViewById(R.id.ivImage);
    if (ad.getImages() != null && ad.getImages().size() > 0) {
        ivImage.setImageDrawable(ad.getImages().get(0).getDrawable());
        ivImage.setVisibility(View.VISIBLE);
    } else ivImage.setVisibility(View.GONE);
    adView.setImageView(ivImage);

    // Call the NativeContentAdView's setNativeAd method to register the
    // NativeAdObject.
    nativeAdView.setNativeAd(nativeAd);
}
 
Example #4
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 #5
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);
}