5. Native Ad‎ > ‎

1) Android

Before starting this chapter, you have to got finished SDK settings. If not, check 3) Android Settings first.

[Load NativeAd]

To load a native Ad with a cover image in landscape, just invoke prepareAd() method as the sample code.

NativeAdItem

    NativeAdItem adItem = new NativeAdItem(this, NativeAdItem.STYLE_LANDSCAPE, new NativeAdListener() {


  @Override

  public void onFailure(int errCode) {

  }


  @Override

  public void onLoad(NativeAdItem adItem) {

  // Native Ad rendering

  }


  @Override

  public void onClick() {

  }


  @Override

  public void onShow() {

  }    

    });

    

    adItem.prepareAd();


Or you can use a static methdo in TnkSession class. This code work exactly same as the previous one.

TnkSession.prepareNativeAd

    TnkSession.prepareNativeAd(thisTnkSession.CPC, NativeAdItem.STYLE_LANDSCAPE, new NativeAdListener() {


  @Override

  public void onFailure(int errCode) {

  }


  @Override

  public void onLoad(NativeAdItem adItem) {

  // Native Ad rendering

  }


  @Override

  public void onClick() {

  }


  @Override

  public void onShow() {

  }    

    });


[Display NativeAd]

Once loading a native ad is completed. onLoad() in NativeAdListener is invoked with a parameter of NativeAdItem which contains data for native Ad.
You can render the native Ad with the data in the parameter.

Rendering Native Ad

     ViewGroup adContainer = (ViewGroup)findViewById(R.id.native_ad_container);

    adContainer.removeAllViews();

    

    // ad layout 

    LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    RelativeLayout adItemView = (RelativeLayout)inflater.inflate(R.layout.native_ad_item, null);

    

     // UI rendering

    ImageView adIcon = (ImageView)adItemView.findViewById(R.id.ad_icon);

    adIcon.setImageBitmap(adItem.getIconImage());

    

    TextView titleView = (TextView)adItemView.findViewById(R.id.ad_title);

    titleView.setText(adItem.getTitle());

    

    TextView descView = (TextView)adItemView.findViewById(R.id.ad_desc);

    descView.setText(adItem.getDescription());

    

    ImageView adImage = (ImageView)adItemView.findViewById(R.id.ad_image);

    adImage.setImageBitmap(adItem.getCoverImage());

    

    adContainer.addView(adItemView);

    

     // attach ad layout to ad item

    adItem.attachLayout(adItemView);


When the native Ad is displayed in your app, you should call attachLayout() method of NativeAdItem with the parameter of the viewGroup which the native Ad is displayed in.
SDK will handle click event in automatic with the viewGroup you passed in the parameter. If you want the click event to be handled with other view, you can pass the view in the second parameter of attachLayout() method.

When you remove the view for native ad from the display. you should invoke detachLayout() method. Once detachLayout() method is invoked, the instance of NativeAdItem can be reused to load another Native Ad.

[NativeAdItem API]

1) Create NativeAdItem

Constructor
  - NativeAdItem(Context context, int adStyle, NativeAdListener nativeListener)

Parameters
 Name Description
 context Context instance

 adStyle Style of Native Ad (See Ad Style)
 nativeListener NativeAdListener instance


2) Native Ad Loading

prepareAd()
 Loading a native ad from server. You can specify a name of Display Logic.

 - void prepareAd()
    Load a non-incentive native ad.

 - void prepareAd(String displayLogic)
    Load a native ad with displayLogic. See 5) Display Logic for details of DisplayLogic.

TnkSession.prepareNativeAd()
 Static method provided by TnkSession class. It creates a NativeAdItem instance and invokes prepareAd() method on the instance. The instance is returned.

 - NativeAdItem TnkSession.prepareNativeAd(Context context, String displayLogic, int adStyle, NativeAdListener nativeListener)

Parameters
 Name Description
 context Context instance

 displayLogic Enter TnkSession.PPI for incentive ads or TnkSession.CPC for non-incentive ads or any other string for Display Logic. See 3) Display Logic for details.
 adStyle Style of Native Ad (See Ad Style)
 nativeListener NativeAdListener instance


3) Ad Style

We support four styles of Native ads.

- Cover Image
  • NativeAdItem.STYLE_TEXT_ONLY : Native Ad wihtout cover image.
  • NativeAdItem.STYLE_PORTRAIT : Native Ad with a cover image in portrait style.
  • NativeAdItem.STYLE_LANDSCAPE : Native Ad with a cover image in landscape style.
  • NativeAdItem.STYLE_SQUARE : Native Ad with a cover image in square style. (Not supported yet)
- Ad Icon
  • NativeAdItem.STYLE_ICON : Native Ad with icon image.
You can OR the Cover Image and Ad Icon value.

Example

Ad Style Example

// Landscape Cover Image with Icon Image

TnkSession.prepareNativeAd(this, TnkSession.CPC, NativeAdItem.STYLE_LANDSCAPE | NativeAdItem.STYLE_ICON, new NativeAdListener() { ...


// Portrait Cover Image without Icon Image

TnkSession.prepareNativeAd(this, TnkSession.CPC, NativeAdItem.STYLE_PORTRAITnew NativeAdListener() { ...


// without Cover Image but use Icon Image

TnkSession.prepareNativeAd(this, TnkSession.CPC,  NativeAdItem.STYLE_ICONnew NativeAdListener() { ...


// without Cover Image and Icon Image but use text only

TnkSession.prepareNativeAd(this, TnkSession.CPC, NativeAdItem.STYLE_TEXT_ONLYnew NativeAdListener() { ...




4) NativeAdItem APIs

NatvieAdItem instance provides following APIs to access the data of the native Ad.
  • String getTitle() : Title of Ad. 
  • String getDescription() : Subtitle of Ad.
  • Bitmap getIconImage() : Icon Image. null is returned if you didn't specify NativeAdItem.STYLE_ICON.
  • Bitmap getCoverImage() : Conver image. null is returned if you didn't specify NativeAdItem.STYLE_PORTRAIT or  NativeAdItem.STYLE_LANDSCAPE.
Following APIs are for reward Ad.
  • int getRewardPoint() : Amount of reward point to user.
  • String getPointUnit() : Name of point (ex :Coin, Ruby, Gold) 
  • int getRewardType() : Type of user action to get reward.(1 for app execution, 2 for specific action)
  • String getActionText() : Description of how to get reward.

5) Attach & Detach Layout

You should call attachLayout() method on the instance of NativeAdItem when the native Ad is displayed.

- void attachLayout(ViewGroup viewGroup)
   Attach the NativeAdItem with viewGroup. Clicks on viewGroup will be handled by SDK.

- void attachLayout(ViewGroup viewGroup, View clickView)
    Attach the NativeAdItem with viewGroup. Clicks on clickView will be handled by SDK.

- void detachLayout()
   When you remove the view of native ad or reload the native ad, you should invoke detachLayout() on the native Ad. Once detachLayout() method is invoked, the instance of NativeAdItem can be reused to load another Native Ad.

Lifecycle of NativeAdItem is shown below.

 Instantiated -> prepareAd -> attachLayout -> detachLayout
                               ^                                                  |
                               +---------------------------+

[NativeAdListener]

To catch events when an native ad is loaded or users click native ads, use NativeAdListener interface.

NativeAdListener Interface

public interface NativeAdListener {


public static final int FAIL_NO_AD = -1;  // no ad available

public static final int FAIL_NO_IMAGE = -2; // ad image not available


public static final int FAIL_SYSTEM = -9;


/**

* Invoked when user clicked ad.

* @param type 

*/

public void onClick();

/**

 * Invoked when native ad is displayed on screen. If you don't use cover image, this method is not invoked.

 */

public void onShow();

/**

* Invoked when failed to get native ad. Error code is passed to parameter.

*/

public void onFailure(int errCode);

/**

* Invoked when a native ad is loaded. The instance of native ad is passed to parameter.

*/

public void onLoad(NativeAdItem adItem);

}

Subpages (1): ListView Sample
Comments