Native

Before You Start

Before integrating ads unit in your app, you must:

Native

Native ad is a form of paid media where the ad experience follows the natural form and function of the user experience in which it is placed.

Step 1. Init Native Ad

The AdTiming SDK fires several events to inform you of Native Ad activity. To display Native Ad, one needs to create a brand new NativeAd object, setup listeners and load ads.

The following snippet demonstrates how to use the NativelAd class to create NativelAd objects and implement the NativelAdListener interface to receive Native Ad events. The SDK will notify the Listener of all possible events listed below:

import com.aiming.mdt.nativead.NativeAd;
import com.aiming.mdt.nativead.NativeAdListener;
...
NativeAd nativeAd = new NativeAd(activity, placementI, new NativeAdListener() {

     /**
      * Invoked when Native Ad are available.
      * You can then show the video by calling nativeAd.showAd().
      */
    @Override
     public void onAdReady(AdInfo info) {
        //native ad load success
     }

     /**
      * Invoked when the end user clicked on the Native Ad
      */
     @Override
     public void onAdClicked() {
        //native ad click
     }

     /**
      * Invoked when the call to load a Native Ad has failed
      * String error contains the reason for the failure.
      */
     @Override
     public void onAdFailed(String error) {
        //native ad load failed
     }
});

 

Step 2. Load NativeAd

Invoke loadAd method to request and cache the Native Ad before it is going to be shown to users. It is strongly recommended to invoke this method a short while before the ad is to be shown.

nativeAd.loadAd()

 Notes

  • The loadAd method could be called multiple times at any time, but we do not recommend continuous requests for a short period of time. Many requests in a short period of time have no added value because the opportunities of available for inventory is unlikely at this time. 
  • !WarningAttempting to load a new ad from the onAdFailed() methods is strongly discouraged. If you must load an ad from onAdFailed() ensure to limit ad load retries to avoid continuous failed ad requests in situations such as limited network connectivity.

 

Step 3. NativeAd Elements and How to Fetch

Elements of NativeAd includes: icon, title, description, large campaign image, CTA button.

Image text

①Icon ②Title ③Description ④Large campaign image ⑤CTA button

■ Public String getTitle()

■ Public String getDesc()

■ Public String getCallToActionText()

import com.aiming.mdt.nativead.AdInfo;
...
private AdInfo adInfo;
...
String titlr = adInfo.getTitle();
String desc = adInfo.getDesc();
String callToActionText = adInfo.getCallToActionText();

 

Step 4. Show NativeAd

In this section, we will create customized views to showcase Native Ads to users. In this sample we used the android view, but it can be customized based on your app’s style.

registerNativeAdView() is very important. See the following code as an example. This allows 3rd-party SDKs to track ad display timing and click events. Failing to call this method may cause errors such as click button failure.

import com.aiming.mdt.nativead.NativeAd;
import com.aiming.mdt.nativead.AdIconView;
import com.aiming.mdt.nativead.NativeAdView;
import com.aiming.mdt.nativead.AdInfo;
import com.aiming.mdt.nativead.MediaView;
...
private NativeAd nativeAd;
private NativeAdView nativeAdView;
private AdInfo mAdInfo;
private View adView;
private RelativeLayout adParent;
...
adParent = this.findViewById(R.id.native_ad_container);
// Get NativeAdView
nativeAdView = new NativeAdView(NativeActivity.this);
nativeAd.loadAd();
...
// Get your native ad view style
adView = View.inflate(this, R.layout.native_ad_layout, null);

// set native ad title
TextView title = adView.findViewById(R.id.ad_title);
title.setText(mAdInfo.getTitle());

// set native ad Call To Action Text
Button btn = adView.findViewById(R.id.ad_btn);
btn.setText(mAdInfo.getCallToActionText());
MediaView mediaView = adView.findViewById(R.id.ad_media);
AdIconView iconMediaView = adView.findViewById(R.id.ad_icon_media);
adParent.removeAllViews();

// Add your native ads view to NativeAdView
nativeAdView.addView(adView);
nativeAdView.setTitleView(title);
nativeAdView.setMediaView(mediaView);
nativeAdView.setAdIconView(iconMediaView);
nativeAdView.setCallToActionView(btn);
nativeAd.registerNativeAdView(nativeAdView)
adView.getLayoutParams().width = ViewGroup.LayoutParams.MATCH_PARENT;
adView.getLayoutParams().height = ViewGroup.LayoutParams.WRAP_CONTENT;
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
layoutParams.addRule(Gravity.CENTER);

// Add nativeAdView to adParent view
adParent.addView(nativeAdView, layoutParams);            

■ R.id.native_ad_container code as following:

<RelativeLayout
    android:id="@+id/native_ad_container"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

</RelativeLayout>

■ R.layout.native_ad_layout code as following:

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

<TextView
   android:id="@+id/ad_title"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_alignParentTop="true"
   android:layout_centerInParent="true"
   android:padding="10dp"
   android:text="title"
   android:textColor="@android:color/white" />

<com.aiming.mdt.nativead.AdIconView
   android:id="@+id/ad_icon_media"
   android:layout_width="250dp"
   android:layout_height="175dp"
   android:layout_below="@id/ad_title"
   android:layout_centerHorizontal="true" />

<com.aiming.mdt.nativead.MediaView
  android:id="@+id/ad_media"
  android:layout_width="250dp"
  android:layout_height="175dp"
  android:layout_below="@id/ad_icon_media"
  android:layout_centerHorizontal="true" />

<Button
  android:id="@+id/ad_btn"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_below="@id/ad_media"
  android:layout_centerInParent="true"
  android:padding="10dp"
  android:text="calltoaction"
  android:textAllCaps="false" />
</RelativeLayout>

 

Step 5. MediaView

The MediaView is a special View designed to display the main media asset.

  • If the loaded ad has a video asset, the video is buffered and starts playing inside the MediaView.
  • If the loaded ad does not contain a video asset, the first image asset is downloaded and placed inside the MediaView instead.
  • MediaView is a View that can be defined in an XML layout or constructed dynamically.

 

Step 6. AdIconView

The AdIconView is a special View designed to display the icon asset.

  • If the loaded ad contain a icon asset, the icon asset is downloaded and placed inside the AdIconView instead.
  • AdIconView is a View that can be defined in an XML layout or constructed dynamically.

 

Step 7. Destory NativeAd Object

It is recommended to destroy the NativeAd object when the ad Activity being destroyed.

/**
 * Invoke nativeAd.destroy() method in Activity's onDestroy() callback to release NativeAd object.
 */
@Override
public void onDestroy()
    if (nativeAd != null) {
        nativeAd.destroy();
    }
    super.onDestroy();
}            

 

Was this article helpful?
0 out of 0 found this helpful

Comments

0 comments

Please sign in to leave a comment.