Get Started with Avocarrot

Overview

Examples

Android

Overview

- Avocarrot Exchange SDK

Setup Exchange SDK

Stream

Native Assets

Exchange Troubleshooting

- Avocarrot Mediation SDK

Setup Mediation

Stream Ads Mediation

Native ads Mediation

Native Assets Mediation

Banner Mediation

Interstitial Mediation

Video Mediation

Supported Networks & Ad Types

Mediation Troubleshooting

iOS

Overview

- Avocarrot Exchange SDK

Setup SDK

Stream

Native Assets

Exchange Troubleshooting

- Avocarrot Mediation

Getting Started With Mediation

Stream Ads Mediation

Native Ads Mediation

Banner Ads Mediation

Interstitial Ads Mediation

Video Ads Mediation

Supported Networks & Ad Types

Mediation Troubleshooting

API Documentation

Reporting API

Help

FAQ

Updates

SDK Change Log

Android Change Log

iOS Change Log

Native Assets

Updated 7 days ago ​by Derek Lim

Overview

This tutorial helps you setup Avocarrot Native Assets ads in your Android app and receive your first ad.

Native Assets is an ad unit that provides you access to the raw assets (eg. Icon, Image, Title & etc) of a native ad.

Prerequisites

You need an Avocarrot account to complete the tutorial. Create an account
You need to setup the Exchange SDK. Setup Exchange SDK

Setup Native Assets Ads

  1. Add the following code to your activity:

    NativeAssetsAd nativeAssetsAd = NativeAssetsAdPool.load(context, "YOUR_NATIVE_AD_UNIT_ID", nativeAssetsAdListener);
    
    Get your adUnitId from the dashboard.
  2. You can configure Builder for NativeAssetsAd to load image resources by adding this lines to it: 
    final NativeAssetsConfig.Builder config = new NativeAssetsConfig.Builder()
           .prefetchIcon(true)
           .prefetchImage(true)
           .prefetchAdChoiceIcon(true);
    nativeAssetsAd = NativeAssetsAdPool.load(this, getAdUnitId(), config, this);
    

    Or you can manage resources downloading by yourself, when you will receive a NativeAssets object in onAdLoaded callback:

    public void onAdLoaded(@NonNull NativeAssetsAd nativeAssetsAd, @NonNull NativeAssets nativeAssets) {
        Image icon = nativeAssets.getIcon();
        if (icon != null) {
            String iconUrl = icon.getUrl();
            /* your image downloading code here */
        }
    }
    

    There is a common algorithm to use when implementing a Native Assets Ad:

    • Create your own subclass of View which will be used as ad view container.
    • Initialize NativeAssetsAd by NativeAssetsAdPool.load(context, "YOUR_NATIVE_AD_UNIT_ID", nativeAssetsAdListener); call to start loading of assets
    • Get NativeAssets in onAdLoaded(@NonNull NativeAssetsAd nativeAssetsAd, @NonNull NativeAssets nativeAssets) callback of nativeAssetsAdListener
    • Render the Ad view container
    • Register the Ad views for impressions and clicks:
      • call nativeAssetsAd.registerViewForImpression(adContainer) to register your Ad container view for impression
      • to register ad views for click events call .registerViewsForClick(clickableViewList);
      • call .registerAdChoiceViewForClick(adChoiceView) to register AdChoice view for clicks.
    • Unregister the Ad views by calling .unregisterViews() when you are no longer displaying them.
    • Destroy the NativeAssetsAd by calling .destroy() when you're not going to use it anymore.
    • For example:

      @Override
          protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              ...
              adContainer = (LinearLayout) findViewById(R.id.native_assets_ad_container);
              title = (TextView) findViewById(R.id.native_assets_ad_title);
              body = (TextView) findViewById(R.id.native_assets_ad_body);
              adChoiceText = (TextView) findViewById(R.id.native_assets_ad_choices_caption);
              cta = (TextView) findViewById(R.id.native_assets_ad_call_to_action);
              icon = (ImageView) findViewById(R.id.native_assets_ad_icon);
              image = (ImageView) findViewById(R.id.native_assets_ad_image);
              adChoiceIcon = (ImageView) findViewById(R.id.native_assets_ad_choices_icon);
              rating = (RatingBar) findViewById(R.id.native_assets_ad_star_rating);
              final NativeAssetsConfig.Builder config = new NativeAssetsConfig.Builder()
                              .prefetchIcon(true)
                              .prefetchImage(true)
                              .prefetchAdChoiceIcon(true);
              NativeAssetsAd nativeAssetsAd = NativeAssetsAdPool.load(context, "YOUR_NATIVE_AD_UNIT_ID", config, nativeAssetsAdListener);
              ...
          }
          @Override
          protected void onDestroy() {
              super.onDestroy();
              if (nativeAssetsAd != null) {
                  nativeAssetsAd.destroy();
              }
          }
          @Override
          public void onAdLoaded(@NonNull NativeAssetsAd nativeAssetsAd, @NonNull NativeAssets nativeAssets) {            
              title.setText(nativeAssets.getTitle());
              body.setText(nativeAssets.getText());
              renderImageView(icon, nativeAssets.getIcon());
              renderImageView(image, nativeAssets.getImage());
              cta.setText(nativeAssets.getCallToAction());
              AdChoice adChoice = nativeAssets.getAdChoice();
              if (adChoice != null) {                       
                  adChoiceIcon.setImageDrawable(adChoice.getIcon().getDrawable());
                  nativeAssetsAd.registerAdChoiceViewForClick(adChoiceIcon);
              }
              renderRatingBarView(rating, nativeAssets.getRating());
              nativeAssetsAd.registerViewsForClick(Arrays.asList(title, icon, image, cta));
              nativeAssetsAd.registerViewForImpression(adContainer);
          }
          private static void renderImageView(@NonNull ImageView view, Image image) {
              if (image != null) {
                  view.setVisibility(View.VISIBLE);
                  view.setAdjustViewBounds(true);
                  int width = image.getWidth();
                  int height = image.getHeight();
                  if (width > 0 && height > 0) {
                  view.setMaxWidth(width);
                      view.setMaxHeight(height);
                  }
                  view.setImageDrawable(image.getDrawable());
              } else {
                  view.setImageDrawable(null);
                  view.setVisibility(GONE);
              }
          }
          private static void renderRatingBarView(@NonNull RatingBar view, @Nullable Rating starRating) {
              view.setStepSize(0.1F);
              view.setIsIndicator(true);
              if (starRating != null) {
                  view.setNumStars((int) starRating.getScale());
                  view.setRating((float) starRating.getValue());
                  view.setVisibility(View.VISIBLE);
              } else {
                  view.setVisibility(GONE);
              }
          }
  3. Compile and Run your project to get your first sample native ad.

    Make sure to test this in an emulator that supports Google Play Services or in a mobile device.
  4. Congratulations! You can now customize your native ads to match the look and feel of your app.

Advanced Customization

Go Live!

  1. Remove sandbox If you have received a sample ad then simply disable the Sandbox mode by including the following instruction in your code:
    AvocarrotSDK.setTestMode(false);
    You should disable Sandbox mode ONLY when you are done with the integration and you are ready to publish your app. It is against Avocarrot's policies to use live ads during development, and doing so could get your Avocarrot account banned.
  2. Publish app You are now ready to upload your app and start making money! If you have submitted your app in the Google Play/ Apple Store and you can see live ads, then you have nothing to worry about. Just wait until the impressions threshold has been reached and the app will be shown as live in your dashboard as well.
  3. Update payment details. Login to your account and update your payment settings.

Was this article useful?