Native Ads - uc-union/union-ads-sdk-demo GitHub Wiki

Integration Guide

Note: Be sure that SDK is initialzed correctly, see Initialize SDK.

Native ads are the type of ads Ad SDK can offer, where developers enjoy the greatest flexibility. Ad SDK only returns the relevant sources of the ads to the developer, and developer himself would decide the placement, size and layout of the ads.

Example codes are listed below, for a complete example please refer to NativeFragment.java

final NativeAd nativeAd = new NativeAd(
        MainActivity.this);
nativeAd.setAdListener(new AdListener() {
    @Override
    public void onAdLoaded(Ad ad) {
        NativeAdAssets assets = nativeAd.getNativeAdAssets();
        /* ... */
    }
    @Override
    public void onAdClosed(Ad ad) {
    }
    @Override
    public void onAdShowed(Ad ad) {
    }
    @Override
    public void onAdClicked(Ad ad) {
    }
    @Override
    public void onAdError(Ad ad, AdError adError) {
    }
});
AdRequest adRequest = AdRequest.newBuilder()
        .pub(<Pub>)
        .build();
nativeAd.loadAd(adRequest);

NOTE:

  1. Getting NativeAdAssets via getNativeAdAssets method of NativeAd after onAdLoaded is callback or you will get null
  1. <Pub> is a unique id for the ad placement, please get it from uc union manager console or your BD

Register View

The SDK will log the impression and handle the click automatically. Please note that developers must register the ad's view with NativeAd instance to enable that.

To make the whole view clickable register the view using:

registerViewForInteraction(ViewGroup parent, View... clickable)

parent should be the container of all clickable.

Image Filter

Developer can get all the cover images in NativeAdAssets.Image by getCovers() method in NativeAdAssets instance by default. And developer should pick up a appropriate one according to demands.

The SDK provide a helper class ImageFilter to help developer to simplify filtering images by passing the expected width & height (dp).

ImageFilter.filter(List<NativeAdAssets.Image> images, int width, int height)

ImageFiler may return null if can not find a matched image approached to expected width & height.

Image Downloader

Only image width & height, url info are contained in NativeAdAssets.Image, so developer should download and display image by themselves.

The SDK provide a helper class ImageDownloader to help developer to simplify downloading and displaying images.

ImageDownloader has the following two usages:

  1. Download image and return Drawable asynchronously, example codes are listed below:

    ImageDownloader.downloadImage(image, new ImageDownloader.Delegate() {
        @Override
        public void onSuccess(Drawable drawable) {
        }
    
        @Override
        public void onFail() {
        }
      })
  2. Download image and set it to ImageView by following two API in different signature:

    ImageDownloader.downloadAndDisplayImage(NativeAdAssets.Image image, ImageView imageView)
    ImageDownloader.downloadAndDisplayImage(NativeAdAssets.Image image, ImageView imageView, Drawable fallback)

    fallback is used if download image failed and display the Drawable that user specified.

DEMO

Once you run the demo project and show the native ad, you should see something like this:

native-ads-example

Next Steps

  • See FAQ to know about the frequently asked questions.

  • See API reference to explore all APIs in Union Ad SDK.

⚠️ **GitHub.com Fallback** ⚠️