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

The following examples show how to use com.google.android.gms.ads.formats.NativeAppInstallAd. 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: AdmobRecyclerAdapterWrapper.java    From admobadapter with Apache License 2.0 5 votes vote down vote up
@Override
public int getItemViewType(int position) {
    if (AdapterCalculator.canShowAdAtPosition(position, adFetcher.getFetchedAdsCount())) {
        int adPos = AdapterCalculator.getAdIndex(position);
        NativeAd ad = adFetcher.getAdForIndex(adPos);
        return ad instanceof NativeAppInstallAd ? getViewTypeAdInstall() : getViewTypeAdContent();
    } else {
        int origPos = AdapterCalculator.getOriginalContentPosition(position,
                adFetcher.getFetchedAdsCount(), mAdapter.getItemCount());
        return mAdapter.getItemViewType(origPos);
    }
}
 
Example #4
Source File: InstallAppAdLayoutContext.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 NativeAppInstallAd) || !(nativeAdView instanceof NativeAppInstallAdView))
        throw new ClassCastException();

    NativeAppInstallAd ad = (NativeAppInstallAd) nativeAd;
    NativeAppInstallAdView adView = (NativeAppInstallAdView) nativeAdView;

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

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

    ImageView ivLogo = (ImageView) adView.findViewById(R.id.ivLogo);
    if(ad.getIcon()!=null)
        ivLogo.setImageDrawable(ad.getIcon().getDrawable());
    adView.setIconView(ivLogo);

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

    TextView tvStore = (TextView) adView.findViewById(R.id.tvStore);
    tvStore.setText(ad.getStore());
    adView.setStoreView(tvStore);

    TextView tvPrice = (TextView) adView.findViewById(R.id.tvPrice);
    tvPrice.setText(ad.getPrice());
    adView.setPriceView(tvPrice);

    ImageView ivImage = (ImageView) adView.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 NativeAppInstallAdView's setNativeAd method to register the
    // NativeAd.
    adView.setNativeAd(ad);
}
 
Example #5
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 #6
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);
}