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

The following examples show how to use com.google.android.gms.ads.formats.UnifiedNativeAd. 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: AdMobNativeAdResponse.java    From mobile-sdk-android with Apache License 2.0 6 votes vote down vote up
AdMobNativeAdResponse(final UnifiedNativeAd ad) {
    this.nativeAd = ad;
    runnable = new Runnable() {
        @Override
        public void run() {
            if (coverImage != null) {
                coverImage.recycle();
                coverImage = null;
            }
            if (icon != null) {
                icon.recycle();
                icon = null;
            }
            listener = null;
            expired = true;
            if (AdMobNativeAdResponse.this.nativeAd != null) {
                nativeAd.destroy();
            }
        }
    };
    nativeExpireHandler = new Handler(Looper.getMainLooper());
    nativeExpireHandler.postDelayed(runnable, Settings.NATIVE_AD_RESPONSE_EXPIRATION_TIME);
    loadAssets();
}
 
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: 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 #4
Source File: MainActivity.java    From admob-native-advanced-feed with Apache License 2.0 5 votes vote down vote up
private void insertAdsInMenuItems() {
    if (mNativeAds.size() <= 0) {
        return;
    }

    int offset = (mRecyclerViewItems.size() / mNativeAds.size()) + 1;
    int index = 0;
    for (UnifiedNativeAd ad : mNativeAds) {
        mRecyclerViewItems.add(index, ad);
        index = index + offset;
    }
    loadMenu();
}
 
Example #5
Source File: AdMobNativeListener.java    From mobile-sdk-android with Apache License 2.0 5 votes vote down vote up
@Override
public void onUnifiedNativeAdLoaded(UnifiedNativeAd unifiedNativeAd) {
    Clog.e(Clog.mediationLogTag, "AdMob - onUnifiedNativeAdLoaded");
    if (mBC != null) {
        AdMobNativeAdResponse response = new AdMobNativeAdResponse(unifiedNativeAd);
        weakReferenceAdMobNativeAdResponse = new WeakReference<AdMobNativeAdResponse>(response);
        mBC.onAdLoaded(response);
    }
}
 
Example #6
Source File: RecyclerViewAdapter.java    From admob-native-advanced-feed with Apache License 2.0 5 votes vote down vote up
/**
     * Replaces the content in the views that make up the menu item view and the
     * Native ad view. This method is invoked by the layout manager.
     */
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
    int viewType = getItemViewType(position);
    switch (viewType) {
        case UNIFIED_NATIVE_AD_VIEW_TYPE:
            UnifiedNativeAd nativeAd = (UnifiedNativeAd) mRecyclerViewItems.get(position);
            populateNativeAdView(nativeAd, ((UnifiedNativeAdViewHolder) holder).getAdView());
            break;
        case MENU_ITEM_VIEW_TYPE:
            // fall through
        default:
            MenuItemViewHolder menuItemHolder = (MenuItemViewHolder) holder;
            MenuItem menuItem = (MenuItem) mRecyclerViewItems.get(position);

            // Get the menu item image resource ID.
            String imageName = menuItem.getImageName();
            int imageResID = mContext.getResources().getIdentifier(imageName, "drawable",
                mContext.getPackageName());

            // Add the menu item details to the menu item view.
            menuItemHolder.menuItemImage.setImageResource(imageResID);
            menuItemHolder.menuItemName.setText(menuItem.getName());
            menuItemHolder.menuItemPrice.setText(menuItem.getPrice());
            menuItemHolder.menuItemCategory.setText(menuItem.getCategory());
            menuItemHolder.menuItemDescription.setText(menuItem.getDescription());
    }
}
 
Example #7
Source File: RecyclerViewAdapter.java    From admob-native-advanced-feed with Apache License 2.0 5 votes vote down vote up
/**
 * Determines the view type for the given position.
 */
@Override
public int getItemViewType(int position) {

    Object recyclerViewItem = mRecyclerViewItems.get(position);
    if (recyclerViewItem instanceof UnifiedNativeAd) {
        return UNIFIED_NATIVE_AD_VIEW_TYPE;
    }
    return MENU_ITEM_VIEW_TYPE;
}
 
Example #8
Source File: MainActivity.java    From admob-native-advanced-feed with Apache License 2.0 5 votes vote down vote up
private void loadNativeAds() {

    AdLoader.Builder builder = new AdLoader.Builder(this, getString(R.string.ad_unit_id));
    adLoader = builder.forUnifiedNativeAd(
        new UnifiedNativeAd.OnUnifiedNativeAdLoadedListener() {
            @Override
            public void onUnifiedNativeAdLoaded(UnifiedNativeAd unifiedNativeAd) {
                // A native ad loaded successfully, check if the ad loader has finished loading
                // and if so, insert the ads into the list.
                mNativeAds.add(unifiedNativeAd);
                if (!adLoader.isLoading()) {
                    insertAdsInMenuItems();
                }
            }
        }).withAdListener(
        new AdListener() {
            @Override
            public void onAdFailedToLoad(int errorCode) {
                // A native ad failed to load, check if the ad loader has finished loading
                // and if so, insert the ads into the list.
                Log.e("MainActivity", "The previous native ad failed to load. Attempting to"
                    + " load another.");
                if (!adLoader.isLoading()) {
                    insertAdsInMenuItems();
                }
            }
        }).build();

    // Load the Native ads.
    adLoader.loadAds(new AdRequest.Builder().build(), NUMBER_OF_ADS);
}
 
Example #9
Source File: StoriesFragment.java    From Instagram-Profile-Downloader with MIT License 5 votes vote down vote up
private void loadNativeAds() {

        AdLoader.Builder builder = new AdLoader.Builder(context, getString(R.string.native_ad_id));
        adLoader = builder.forUnifiedNativeAd(
                new UnifiedNativeAd.OnUnifiedNativeAdLoadedListener() {
                    @Override
                    public void onUnifiedNativeAdLoaded(UnifiedNativeAd unifiedNativeAd) {
                        // A native ad loaded successfully, check if the ad loader has finished loading
                        // and if so, insert the ads into the list.
                        mNativeAds.add(unifiedNativeAd);
                        if (!adLoader.isLoading()) {
                            insertAdsInMenuItems();
                        }
                    }
                }).withAdListener(
                new AdListener() {
                    @Override
                    public void onAdFailedToLoad(int errorCode) {
                        // A native ad failed to load, check if the ad loader has finished loading
                        // and if so, insert the ads into the list.
                        Log.e("MainActivity", "The previous native ad failed to load. Attempting to"
                                + " load another.");
                        if (!adLoader.isLoading()) {
                            insertAdsInMenuItems();
                        }
                    }
                }).build();

        // Load the Native Express ad.
    }
 
Example #10
Source File: DownloadHistoryVideoFragment.java    From Instagram-Profile-Downloader with MIT License 5 votes vote down vote up
private void insertAdsInMenuItems() {
    if (mNativeAds.size() <= 0) {
        return;
    }
    List<UnifiedNativeAd> nativeAdList = new ArrayList<>();
    nativeAdList.addAll(mNativeAds);
    nativeAdList.remove(mNativeAds.size()-1);

    for (UnifiedNativeAd ad : nativeAdList) {
        modelList.add(spaceBetweenAds, ad);
    }
    modelList.add(mNativeAds.get(mNativeAds.size()-1));

    adapter.addAll(modelList,stories, modelListCopy);
}
 
Example #11
Source File: StoriesFragment.java    From Instagram-Profile-Downloader with MIT License 5 votes vote down vote up
private void insertAdsInMenuItems() {
    if (mNativeAds.size() <= 0) {
        return;
    }

    for (UnifiedNativeAd ad: mNativeAds) {
        userObjectList.add(spaceBetweenAds, ad);
    }

    adapter.setUserObjects(userObjectListCopy,userObjectList);
}
 
Example #12
Source File: StoriesOverview.java    From Instagram-Profile-Downloader with MIT License 5 votes vote down vote up
private void loadNativeAds() {

        AdLoader.Builder builder = new AdLoader.Builder(context, getString(R.string.native_ad_id));
        adLoader = builder.forUnifiedNativeAd(
                new UnifiedNativeAd.OnUnifiedNativeAdLoadedListener() {
                    @Override
                    public void onUnifiedNativeAdLoaded(UnifiedNativeAd unifiedNativeAd) {
                        // A native ad loaded successfully, check if the ad loader has finished loading
                        // and if so, insert the ads into the list.
                        mNativeAds.add(unifiedNativeAd);
                        if (!adLoader.isLoading()) {
                            insertAdsInMenuItems();
                        }
                    }
                }).withAdListener(
                new AdListener() {
                    @Override
                    public void onAdFailedToLoad(int errorCode) {
                        // A native ad failed to load, check if the ad loader has finished loading
                        // and if so, insert the ads into the list.
                        Log.e("MainActivity", "The previous native ad failed to load. Attempting to"
                                + " load another.");
                        if (!adLoader.isLoading()) {
                            insertAdsInMenuItems();
                        }
                    }
                }).build();

        // Load the Native Express ad.
    }
 
Example #13
Source File: StoriesOverview.java    From Instagram-Profile-Downloader with MIT License 5 votes vote down vote up
private void insertAdsInMenuItems() {
    if (mNativeAds.size() <= 0) {
        return;
    }
    List<UnifiedNativeAd> nativeAdList = new ArrayList<>();
    nativeAdList.addAll(mNativeAds);
    nativeAdList.remove(mNativeAds.size()-1);

    for (UnifiedNativeAd ad : nativeAdList) {
        modelList.add(spaceBetweenAds, ad);
    }
    modelList.add(mNativeAds.get(mNativeAds.size()-1));

    adapter.addAll(modelList, modelListCopy);
}
 
Example #14
Source File: DownloadHistoryPhotoFragment.java    From Instagram-Profile-Downloader with MIT License 5 votes vote down vote up
private void loadNativeAds() {

        AdLoader.Builder builder = new AdLoader.Builder(context, getString(R.string.native_ad_id));
        adLoader = builder.forUnifiedNativeAd(
                new UnifiedNativeAd.OnUnifiedNativeAdLoadedListener() {
                    @Override
                    public void onUnifiedNativeAdLoaded(UnifiedNativeAd unifiedNativeAd) {
                        // A native ad loaded successfully, check if the ad loader has finished loading
                        // and if so, insert the ads into the list.
                        mNativeAds.add(unifiedNativeAd);
                        if (!adLoader.isLoading()) {
                            insertAdsInMenuItems();
                        }
                    }
                }).withAdListener(
                new AdListener() {
                    @Override
                    public void onAdFailedToLoad(int errorCode) {
                        // A native ad failed to load, check if the ad loader has finished loading
                        // and if so, insert the ads into the list.
                        Log.e("MainActivity", "The previous native ad failed to load. Attempting to"
                                + " load another.");
                        if (!adLoader.isLoading()) {
                            insertAdsInMenuItems();
                        }
                    }
                }).build();

        // Load the Native Express ad.
    }
 
Example #15
Source File: DownloadHistoryPhotoFragment.java    From Instagram-Profile-Downloader with MIT License 5 votes vote down vote up
private void insertAdsInMenuItems() {
    if (mNativeAds.size() <= 0) {
        return;
    }
    List<UnifiedNativeAd> nativeAdList = new ArrayList<>();
    nativeAdList.addAll(mNativeAds);
    nativeAdList.remove(mNativeAds.size() - 1);

    for (UnifiedNativeAd ad : nativeAdList) {
        modelList.add(spaceBetweenAds, ad);
    }
    modelList.add(mNativeAds.get(mNativeAds.size() - 1));

    adapter.addAll(modelList, stories, modelListCopy);
}
 
Example #16
Source File: DownloadHistoryVideoFragment.java    From Instagram-Profile-Downloader with MIT License 5 votes vote down vote up
private void loadNativeAds() {

        AdLoader.Builder builder = new AdLoader.Builder(context, getString(R.string.native_ad_id));
        adLoader = builder.forUnifiedNativeAd(
                new UnifiedNativeAd.OnUnifiedNativeAdLoadedListener() {
                    @Override
                    public void onUnifiedNativeAdLoaded(UnifiedNativeAd unifiedNativeAd) {
                        // A native ad loaded successfully, check if the ad loader has finished loading
                        // and if so, insert the ads into the list.
                        mNativeAds.add(unifiedNativeAd);
                        if (!adLoader.isLoading()) {
                            insertAdsInMenuItems();
                        }
                    }
                }).withAdListener(
                new AdListener() {
                    @Override
                    public void onAdFailedToLoad(int errorCode) {
                        // A native ad failed to load, check if the ad loader has finished loading
                        // and if so, insert the ads into the list.
                        Log.e("MainActivity", "The previous native ad failed to load. Attempting to"
                                + " load another.");
                        if (!adLoader.isLoading()) {
                            insertAdsInMenuItems();
                        }
                    }
                }).build();

        // Load the Native Express ad.
    }
 
Example #17
Source File: TemplateView.java    From googleads-mobile-android-native-templates with Apache License 2.0 4 votes vote down vote up
public void setNativeAd(UnifiedNativeAd nativeAd) {
  this.nativeAd = nativeAd;

  String store = nativeAd.getStore();
  String advertiser = nativeAd.getAdvertiser();
  String headline = nativeAd.getHeadline();
  String body = nativeAd.getBody();
  String cta = nativeAd.getCallToAction();
  Double starRating = nativeAd.getStarRating();
  Image icon = nativeAd.getIcon();

  String secondaryText;

  nativeAdView.setCallToActionView(callToActionView);
  nativeAdView.setHeadlineView(primaryView);
  nativeAdView.setMediaView(mediaView);
  secondaryView.setVisibility(VISIBLE);
  if (adHasOnlyStore(nativeAd)) {
    nativeAdView.setStoreView(secondaryView);
    secondaryText = store;
  } else if (!TextUtils.isEmpty(advertiser)) {
    nativeAdView.setAdvertiserView(secondaryView);
    secondaryText = advertiser;
  } else {
    secondaryText = "";
  }

  primaryView.setText(headline);
  callToActionView.setText(cta);

  //  Set the secondary view to be the star rating if available.
  if (starRating != null && starRating > 0) {
    secondaryView.setVisibility(GONE);
    ratingBar.setVisibility(VISIBLE);
    ratingBar.setMax(5);
    nativeAdView.setStarRatingView(ratingBar);
  } else {
    secondaryView.setText(secondaryText);
    secondaryView.setVisibility(VISIBLE);
    ratingBar.setVisibility(GONE);
  }

  if (icon != null) {
    iconView.setVisibility(VISIBLE);
    iconView.setImageDrawable(icon.getDrawable());
  } else {
    iconView.setVisibility(GONE);
  }

  if (tertiaryView != null) {
    tertiaryView.setText(body);
    nativeAdView.setBodyView(tertiaryView);
  }

  nativeAdView.setNativeAd(nativeAd);
}
 
Example #18
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 #19
Source File: MainActivity.java    From googleads-mobile-android-mediation with Apache License 2.0 4 votes vote down vote up
/**
 * Populates a {@link UnifiedNativeAdView} object with data from a given {@link UnifiedNativeAd}.
 *
 * @param nativeAd the object containing the ad's assets
 * @param adView the view to be populated
 */
private void populateUnifiedNativeAdView(UnifiedNativeAd nativeAd, UnifiedNativeAdView 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 UnifiedNativeAd.
  ((TextView) adView.getHeadlineView()).setText(nativeAd.getHeadline());

  // These assets aren't guaranteed to be in every UnifiedNativeAd, so it's important to
  // 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);
}
 
Example #20
Source File: MainActivity.java    From android-ads with Apache License 2.0 4 votes vote down vote up
/**
 * Creates a request for a new native ad based on the boolean parameters and calls the
 * corresponding "populate" method when one is successfully returned.
 *
 */
private void refreshAd() {
    refresh.setEnabled(false);

    AdLoader.Builder builder = new AdLoader.Builder(this, ADMOB_AD_UNIT_ID);

    builder.forUnifiedNativeAd(new UnifiedNativeAd.OnUnifiedNativeAdLoadedListener() {
        // OnUnifiedNativeAdLoadedListener implementation.
        @Override
        public void onUnifiedNativeAdLoaded(UnifiedNativeAd unifiedNativeAd) {
            // You must call destroy on old ads when you are done with them,
            // otherwise you will have a memory leak.
            if (nativeAd != null) {
                nativeAd.destroy();
            }
            nativeAd = unifiedNativeAd;
            FrameLayout frameLayout =
                    findViewById(R.id.fl_adplaceholder);
            UnifiedNativeAdView adView = (UnifiedNativeAdView) getLayoutInflater()
                    .inflate(R.layout.ad_unified, null);
            populateUnifiedNativeAdView(unifiedNativeAd, adView);
            frameLayout.removeAllViews();
            frameLayout.addView(adView);
        }

    });

    VideoOptions videoOptions = new VideoOptions.Builder()
            .setStartMuted(startVideoAdsMuted.isChecked())
            .build();

    NativeAdOptions adOptions = new NativeAdOptions.Builder()
            .setVideoOptions(videoOptions)
            .build();

    builder.withNativeAdOptions(adOptions);

    AdLoader adLoader = builder.withAdListener(new AdListener() {
        @Override
        public void onAdFailedToLoad(int errorCode) {
            refresh.setEnabled(true);
            Toast.makeText(MainActivity.this, "Failed to load native ad: "
                    + errorCode, Toast.LENGTH_SHORT).show();
        }
    }).build();

    adLoader.loadAd(new AdRequest.Builder().build());

    videoStatus.setText("");
}
 
Example #21
Source File: AdMobCustomMuteThisAdFragment.java    From android-ads with Apache License 2.0 4 votes vote down vote up
/**
 * Populates a {@link UnifiedNativeAdView} object with data from a given
 * {@link UnifiedNativeAd}.
 *
 * @param nativeAd the object containing the ad's assets
 * @param adView the view to be populated
 */
private void populateUnifiedNativeAdView(UnifiedNativeAd nativeAd, UnifiedNativeAdView adView) {
    MediaView mediaView = adView.findViewById(R.id.ad_media);
    adView.setMediaView(mediaView);

    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));

    ((TextView) adView.getHeadlineView()).setText(nativeAd.getHeadline());

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

    if (nativeAd.getCallToAction() == null) {
        adView.getCallToActionView().setVisibility(View.INVISIBLE);
    } else {
        ((Button) adView.getCallToActionView()).setText(nativeAd.getCallToAction());
        adView.getCallToActionView().setVisibility(View.VISIBLE);
    }

    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 {
        ((TextView) adView.getPriceView()).setText(nativeAd.getPrice());
        adView.getPriceView().setVisibility(View.VISIBLE);
    }

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

    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);
    }

    adView.setNativeAd(nativeAd);

    VideoController vc = nativeAd.getVideoController();

    if (vc.hasVideoContent()) {
        vc.setVideoLifecycleCallbacks(new VideoController.VideoLifecycleCallbacks() {
            @Override
            public void onVideoEnd() {
                refresh.setEnabled(true);
                super.onVideoEnd();
            }
        });
    } else {
        refresh.setEnabled(true);
    }
}
 
Example #22
Source File: AdMobCustomMuteThisAdFragment.java    From android-ads with Apache License 2.0 4 votes vote down vote up
/**
 * Creates a request for a new unified native ad based on the boolean parameters and calls the
 * "populateUnifiedNativeAdView" method when one is successfully returned.
 */
private void refreshAd() {
    refresh.setEnabled(false);
    muteButton.setEnabled(false);

    Resources resources = getActivity().getResources();
    AdLoader.Builder builder = new AdLoader.Builder(getActivity(),
            resources.getString(R.string.custommute_fragment_ad_unit_id));

    builder.forUnifiedNativeAd(new UnifiedNativeAd.OnUnifiedNativeAdLoadedListener() {
        // OnUnifiedNativeAdLoadedListener implementation.
        @Override
        public void onUnifiedNativeAdLoaded(UnifiedNativeAd unifiedNativeAd) {
            AdMobCustomMuteThisAdFragment.this.nativeAd = unifiedNativeAd;
            muteButton.setEnabled(unifiedNativeAd.isCustomMuteThisAdEnabled());
            nativeAd.setMuteThisAdListener(new MuteThisAdListener() {
                @Override
                public void onAdMuted() {
                    muteAd();
                    Toast.makeText(getActivity(), "Ad muted", Toast.LENGTH_SHORT).show();
                }
            });

            UnifiedNativeAdView adView = (UnifiedNativeAdView) getLayoutInflater()
                .inflate(R.layout.ad_unified, null);
            populateUnifiedNativeAdView(unifiedNativeAd, adView);
            adContainer.removeAllViews();
            adContainer.addView(adView);
        }

    });

    NativeAdOptions adOptions = new NativeAdOptions.Builder()
        .setRequestCustomMuteThisAd(true)
        .build();

    builder.withNativeAdOptions(adOptions);

    AdLoader adLoader = builder.withAdListener(new AdListener() {
        @Override
        public void onAdFailedToLoad(int errorCode) {
            refresh.setEnabled(true);
            Toast.makeText(getActivity(), "Failed to load native ad: "
                    + errorCode, Toast.LENGTH_SHORT).show();
        }
    }).build();

    adLoader.loadAd(new AdRequest.Builder().build());
}
 
Example #23
Source File: MainActivity.java    From googleads-mobile-android-examples with Apache License 2.0 4 votes vote down vote up
/**
 * Creates a request for a new native ad based on the boolean parameters and calls the
 * corresponding "populate" method when one is successfully returned.
 *
 */
private void refreshAd() {
    refresh.setEnabled(false);

    AdLoader.Builder builder = new AdLoader.Builder(this, ADMOB_AD_UNIT_ID);

    builder.forUnifiedNativeAd(new UnifiedNativeAd.OnUnifiedNativeAdLoadedListener() {
        // OnUnifiedNativeAdLoadedListener implementation.
        @Override
        public void onUnifiedNativeAdLoaded(UnifiedNativeAd unifiedNativeAd) {
            // If this callback occurs after the activity is destroyed, you must call
            // destroy and return or you may get a memory leak.
            if (isDestroyed()) {
                unifiedNativeAd.destroy();
                return;
            }
            // You must call destroy on old ads when you are done with them,
            // otherwise you will have a memory leak.
            if (nativeAd != null) {
                nativeAd.destroy();
            }
            nativeAd = unifiedNativeAd;
            FrameLayout frameLayout =
                    findViewById(R.id.fl_adplaceholder);
            UnifiedNativeAdView adView = (UnifiedNativeAdView) getLayoutInflater()
                    .inflate(R.layout.ad_unified, null);
            populateUnifiedNativeAdView(unifiedNativeAd, adView);
            frameLayout.removeAllViews();
            frameLayout.addView(adView);
        }

    });

    VideoOptions videoOptions = new VideoOptions.Builder()
            .setStartMuted(startVideoAdsMuted.isChecked())
            .build();

    NativeAdOptions adOptions = new NativeAdOptions.Builder()
            .setVideoOptions(videoOptions)
            .build();

    builder.withNativeAdOptions(adOptions);

    AdLoader adLoader = builder.withAdListener(new AdListener() {
        @Override
        public void onAdFailedToLoad(int errorCode) {
            refresh.setEnabled(true);
            Toast.makeText(MainActivity.this, "Failed to load native ad: "
                    + errorCode, Toast.LENGTH_SHORT).show();
        }
    }).build();

    adLoader.loadAd(new AdRequest.Builder().build());

    videoStatus.setText("");
}
 
Example #24
Source File: AdMobCustomMuteThisAdFragment.java    From googleads-mobile-android-examples with Apache License 2.0 4 votes vote down vote up
/**
 * Populates a {@link UnifiedNativeAdView} object with data from a given
 * {@link UnifiedNativeAd}.
 *
 * @param nativeAd the object containing the ad's assets
 * @param adView the view to be populated
 */
private void populateUnifiedNativeAdView(UnifiedNativeAd nativeAd, UnifiedNativeAdView adView) {
    adView.setMediaView((MediaView) adView.findViewById(R.id.ad_media));

    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));

    ((TextView) adView.getHeadlineView()).setText(nativeAd.getHeadline());
    adView.getMediaView().setMediaContent(nativeAd.getMediaContent());

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

    if (nativeAd.getCallToAction() == null) {
        adView.getCallToActionView().setVisibility(View.INVISIBLE);
    } else {
        ((Button) adView.getCallToActionView()).setText(nativeAd.getCallToAction());
        adView.getCallToActionView().setVisibility(View.VISIBLE);
    }

    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 {
        ((TextView) adView.getPriceView()).setText(nativeAd.getPrice());
        adView.getPriceView().setVisibility(View.VISIBLE);
    }

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

    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);
    }

    adView.setNativeAd(nativeAd);

    VideoController vc = nativeAd.getVideoController();

    if (vc.hasVideoContent()) {
        vc.setVideoLifecycleCallbacks(new VideoController.VideoLifecycleCallbacks() {
            @Override
            public void onVideoEnd() {
                refresh.setEnabled(true);
                super.onVideoEnd();
            }
        });
    } else {
        refresh.setEnabled(true);
    }
}
 
Example #25
Source File: AdMobCustomMuteThisAdFragment.java    From googleads-mobile-android-examples with Apache License 2.0 4 votes vote down vote up
/**
 * Creates a request for a new unified native ad based on the boolean parameters and calls the
 * "populateUnifiedNativeAdView" method when one is successfully returned.
 */
private void refreshAd() {
    refresh.setEnabled(false);
    muteButton.setEnabled(false);

    Resources resources = getActivity().getResources();
    AdLoader.Builder builder = new AdLoader.Builder(getActivity(),
            resources.getString(R.string.custommute_fragment_ad_unit_id));

    builder.forUnifiedNativeAd(new UnifiedNativeAd.OnUnifiedNativeAdLoadedListener() {
        // OnUnifiedNativeAdLoadedListener implementation.
        @Override
        public void onUnifiedNativeAdLoaded(UnifiedNativeAd unifiedNativeAd) {
            AdMobCustomMuteThisAdFragment.this.nativeAd = unifiedNativeAd;
            muteButton.setEnabled(unifiedNativeAd.isCustomMuteThisAdEnabled());
            nativeAd.setMuteThisAdListener(new MuteThisAdListener() {
                @Override
                public void onAdMuted() {
                    muteAd();
                    Toast.makeText(getActivity(), "Ad muted", Toast.LENGTH_SHORT).show();
                }
            });

            UnifiedNativeAdView adView = (UnifiedNativeAdView) getLayoutInflater()
                .inflate(R.layout.ad_unified, null);
            populateUnifiedNativeAdView(unifiedNativeAd, adView);
            adContainer.removeAllViews();
            adContainer.addView(adView);
        }

    });

    NativeAdOptions adOptions = new NativeAdOptions.Builder()
        .setRequestCustomMuteThisAd(true)
        .build();

    builder.withNativeAdOptions(adOptions);

    AdLoader adLoader = builder.withAdListener(new AdListener() {
        @Override
        public void onAdFailedToLoad(int errorCode) {
            refresh.setEnabled(true);
            Toast.makeText(getActivity(), "Failed to load native ad: "
                    + errorCode, Toast.LENGTH_SHORT).show();
        }
    }).build();

    adLoader.loadAd(new AdRequest.Builder().build());
}
 
Example #26
Source File: TemplateView.java    From googleads-mobile-android-native-templates with Apache License 2.0 4 votes vote down vote up
private boolean adHasOnlyStore(UnifiedNativeAd nativeAd) {
  String store = nativeAd.getStore();
  String advertiser = nativeAd.getAdvertiser();
  return !TextUtils.isEmpty(store) && TextUtils.isEmpty(advertiser);
}
 
Example #27
Source File: StoriesOverViewAdapter.java    From Instagram-Profile-Downloader with MIT License 4 votes vote down vote up
@Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder1, final int position) {


        int viewType = getItemViewType(position);

        // Binding data based on View Type
        switch (viewType) {
            case GlobalConstant.CONTENT_TYPE:
                StoriesOverViewHolder holder = (StoriesOverViewHolder) holder1;

                holder.setIsRecyclable(false);
                final String model = (String) modelList.get(position);

                if (!model.endsWith(".jpg")) {
                    holder.isVideo.setVisibility(View.VISIBLE);
                    Glide.with(context).load(model).thumbnail(0.2f).into(holder.imageView);
                } else {
                    holder.isVideo.setVisibility(View.GONE);
                    String url = model.substring(0, model.length() - 4);

                    Glide.with(context).load(url).thumbnail(0.2f).into(holder.imageView);
                }

                holder.layout.setVisibility(View.VISIBLE);

                holder.layout.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
//                Intent intent = new Intent(context, ViewStoryActivity.class);
//                DataHolder.setData(storyModels);
//                intent.putExtra("isLarge", true);
//                intent.putExtra("pos", position);
//                context.startActivity(intent);



                        Intent intent = new Intent(context, ViewStoryActivity.class);
                        intent.putExtra("isFromNet", true);
                        intent.putStringArrayListExtra("urls", (ArrayList<String>) storyModels);
                        intent.putExtra("pos", position);
                        context.startActivity(intent);
                        ((Activity) context).overridePendingTransition(R.anim.enter_main, R.anim.exit_splash);
                    }
                });

                break;
            case GlobalConstant.AD_TYPE:
                // fall through
            default:
                UnifiedNativeAd nativeAd = (UnifiedNativeAd) modelList.get(position);
                populateNativeAdView(nativeAd, ((StoriesNativeAdView) holder1).getAdView());

        }


    }