Native Ads - san-sdk/sample GitHub Wiki

Native ads let you monetize your app in a way that’s consistent with its existing design. You can design the ad layout to be consistent with the look and feel of your app. The SDK automatically handles image caching and metrics tracking so you can focus on how, when, and where to display ads.

Prerequisites

Before integrating native ads into your app:

  1. Already has a PlacementId using the format Native.
  2. Follow our steps to Integrate the SAN SDK into your project.
  3. Integrated the SAN Ad SDK

Step 1. Request the Native Ad

SANNativeAd sanNative = new SANNativeAd(context,  placementId); 
sanNative.setAdLoadListener(new IAdListener.AdLoadListener() {
    @Override
    public void onAdLoaded(SANAd adWrapper) {
        // Called when the ad for the given placementId has loaded.
    }

    @Override
    public void onAdLoadError(AdError adError) {
        // Called when a ad fails to load for the given placementId. 
    }
});
sanNative.setAdActionListener(new IAdListener.AdActionListener() {
    @Override
    public void onAdImpressionError(AdError error) {

    }

    @Override
    public void onAdImpression() {
        // Called when a ad shown
    }

    @Override
    public void onAdClicked() {
        // Called when a ad is clicked
    }

    @Override
    public void onAdCompleted() {

    }

    @Override
    public void onAdClosed(boolean hasRewarded) {

    }
});
sanNative.load();//Request ad

Step 2:Create an XML layout

The Sample:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/native_outer_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/white">

    <ImageView
        android:id="@+id/native_icon_image"
        android:layout_width="64dp"
        android:layout_height="64dp"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginStart="10dp"
        android:layout_marginTop="10dp"
        android:background="@null"
        android:contentDescription="@null" />

    <TextView
        android:id="@+id/native_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toEndOf="@+id/native_icon_image"
        android:layout_alignParentTop="true"
        android:layout_marginTop="32dp"
        android:layout_marginStart="8dp"
        android:textColor="@android:color/darker_gray"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/native_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/native_icon_image"
        android:layout_alignParentStart="true"
        android:layout_marginStart="10dp"
        android:layout_marginTop="10dp"
        android:textColor="@android:color/darker_gray" />

    <com.san.ads.MediaView
        android:id="@+id/native_main_image"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:layout_below="@+id/native_text"
        android:layout_alignParentStart="true"
        android:layout_marginStart="10dp"
        android:layout_marginTop="10dp"
        android:layout_marginEnd="10dp"
        android:background="@null"
        android:scaleType="centerCrop" />

    <Button
        android:id="@+id/native_cta"
        android:layout_width="70dp"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_marginTop="10dp"
        android:layout_marginEnd="10dp"
        android:background="@drawable/ads_button_bg"
        android:clickable="true"
        android:focusable="true"
        android:singleLine="true"
        android:textColor="@color/white"
        android:textStyle="bold" />

    <FrameLayout
        android:id="@+id/ad_choices"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentTop="true"/>
        
    <ImageView
        android:id="@+id/feed_ad_badge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ad_badge" />
</RelativeLayout>

NateAdSample

Step 3:Show Native Ads

  • Get BaseNativeAd to show
  BaseNativeAd nativeAd;//The BaseNativeAd is obtained from void onAdLoaded(SANAd adWrapper)
  TextView titleText = contentView.findViewById(R.id.native_title);
  TextView contentText = contentView.findViewById(R.id.native_text);
  TextView buttonView = contentView.findViewById(R.id.native_button);
  ImageView iconImage = contentView.findViewById(R.id.native_icon_image);
  MediaView mediaLayout = contentView.findViewById(R.id.native_main_image);
  
  //text
  titleText.setText(nativeAd.getTitle());
  contentText.setText(nativeAd.getContent());
  buttonView.setText(nativeAd.getCallToAction());
  //icon
  AdViewRenderHelper.loadImage(iconImage.getContext(), nativeAd.getIconUrl(), iconImage);
  //media view
  mediaLayout.loadMadsMediaView(nativeAd.getNativeAd());
  
  //click list
  List<View> clickViews = new ArrayList<>();
  clickViews.add(titleText);
  clickViews.add(contentText);
  clickViews.add(buttonView);
  clickViews.add(iconImage);
  clickViews.add(mediaLayout);
  //prepare
  nativeAd.prepare(contentView, clickViews, null);
  • Ad attribution

You must display an ad attribution to denote that the view is an advertisement

  • MediaView

MediaView ratio, we recommend using 16:9, a large part of the native advertising image size is this ratio

  • Use nativeAd.prepare()

Make sure to use the prepare method, this method will monitor the contentView and if it is obscured, or not visible, it will be considered invalid for display.

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