Get Started with Avocarrot

Overview

Examples

Android

Overview

Getting Started

- Integrating Ads

Stream

Native Assets

Banner

Interstitial

Video

- Others

Troubleshooting

- Adapters

Admob Android Adapter

Mopub Android Adapter

iOS

Overview

Getting Started

- Integrating Ads

Stream

Native Assets

Banner

Interstitial

Video

- Others

Troubleshooting

- Adapters

Admob iOS Adapter

Mopub iOS Adapter

API Documentation

Reporting API

Help

Supported Networks & Ad Types

General

Earnings

Payments and Accounting

Integration

Reporting

Mediation Network Optimization

Legal and Privacy

Updates

SDK Change Log

Android Change Log

iOS Change Log

Native Assets

Updated 2 months 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.

Currently the Native Assets do not support client mediation networks such as AdMob, Facebook, MoPub, etc.

Prerequisites

You need an Avocarrot account to complete the tutorial. Create an account
You need to create a Native Assets ad unit for your app on your Avocarrot dashboard.
You need to setup the Avocarrot SDK. Setup SDK

Setup Native Assets Ads

  1. Create and add a layout file, eg:

    <Linearlayout android:id="@+id/native_assets_ad_container" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:orientation="vertical"/>
    <Textview android:id="@+id/native_assets_ad_title" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content"/>
    <Textview android:id="@+id/native_assets_ad_body" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content">
    <Textview android:id="@+id/native_assets_ad_choices_caption" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content"/>
    <Textview android:id="@+id/native_assets_ad_call_to_action" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content"/>
    <Imageview android:id="@+id/native_assets_ad_icon" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content"/>
    <Imageview android:id="@+id/native_assets_ad_image" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content"/>
    <Imageview android:id="@+id/native_assets_ad_choices_icon" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content"/>
    <Ratingbar android:id="@+id/native_assets_ad_star_rating" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content"/>
    
  2. Setup your layout view by adding the following code to onCreate:
    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);
    
  3. 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, nativeAssetsAdListener);
    Get your adUnitId from the dashboard.
  4. Implement onAdLoaded method of NativeAssetAdCallback, eg:

    NativeAssetsAdCallback nativeAssetsAdListener = new NativeAssetsAdCallback() {
        @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);
        }
        @Override
        public void onAdFailed(@NonNull NativeAssetsAd ad, @NonNull ResponseStatus responseStatus) {
        }
        @Override
        public void onAdOpened(@NonNull NativeAssetsAd ad) {
        }
        @Override
        public void onAdClicked(@NonNull NativeAssetsAd ad) {
        }
        @Override
        public void onAdClosed(@NonNull NativeAssetsAd ad) {
        }
    }
    
  5. Implement Render methods for Native assets:

    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);
        }
    }
                
  6. onDestroy() method should be called depending on the activity lifecycle events.
     @Override
        public void onDestroy() {
            super.onDestroy();
            if (nativeAssetsAd != null) {
                nativeAssetsAd.destroy();
            }
        }
  7. 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.
  8. 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:
    Avocarrot.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. Test your implementation on real devices with real ads before rolling it out to production. Please make sure that your test device has a sim-card and 'Ads Personalization' setting is turned on.
  3. Check whether Google Play Services are installed within your application.
  4. 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.
  5. Update payment details. Login to your account and update your payment settings.

Was this article useful?