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 NativeAdListener

The OpenMediation 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 implement the NativeAdListener interface to receive Native Ad events. The SDK will notify the Listener of all possible events listed below:

import com.openmediation.sdk.nativead.AdInfo;
import com.openmediation.sdk.nativead.NativeAd;
import com.openmediation.sdk.nativead.NativeAdListener;
...
NativeAdListener listener = new NativeAdListener() {

     /** 
      * Invoked when Native Ad are available. 
      * You can then show the Ad by calling nativeAd.showAd(). 
      */
    @Override
     public void onNativeAdLoaded(String placementId, AdInfo adInfo) {
        //native ad load success
     }

     /** 
      * Invoked when the call to load a Native Ad has failed 
      * String error contains the reason for the failure. 
      */
     @Override
     public void onNativeAdLoadFailed(String placementId, Error error) {
        //native ad load failed
     }
     
     /** 
      * Invoked when an impression is recorded for NativeAd.
      */
     @Override
     public void onNativeAdImpression(String placementId, AdInfo info) {
         //native ad impression
     }

     /** 
      * Invoked when the end user clicked on the Native Ad 
      */
     @Override
     public void onNativeAdClicked(String placementId) {
        //native ad click
     }
});
// add NativeAdListener
NativeAd.addAdListener(placementId, listener);

 

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(placementId);

 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.openmediation.sdk.nativead.AdInfo;
...
private AdInfo adInfo;
...
String title = 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.openmediation.sdk.nativead.AdIconView;
import com.openmediation.sdk.nativead.AdInfo;
import com.openmediation.sdk.nativead.MediaView;
import com.openmediation.sdk.nativead.NativeAd;
import com.openmediation.sdk.nativead.NativeAdListener;
import com.openmediation.sdk.nativead.NativeAdView;
...
private NativeAdView nativeAdView;
private AdInfo adInfo;
private View adView;
private RelativeLayout adContainer;
    ...
    adContainer = this.findViewById(R.id.native_ad_container);
    // Get NativeAdView
    nativeAdView = new NativeAdView(NativeActivity.this);
    NativeAd.loadAd(placementId);
    ...
    
    @Override
    public void onNativeAdLoaded(String placementId, AdInfo info){
        adContainer.removeAllViews();
        adInfo = info;
        // Native Template rendering, for Admost, TikTok_cn, Tencent Network
        if (info.isTemplateRender()) {
            RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
                    RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
            layoutParams.addRule(Gravity.CENTER);
            adContainer.addView(info.getView(), layoutParams);
        } else {
            adView = LayoutInflater.from(MainActivity.this).inflate(R.layout.native_ad_layout, null);
            TextView title = adView.findViewById(R.id.ad_title);
            title.setText(info.getTitle());
            TextView desc = adView.findViewById(R.id.ad_desc);
            desc.setText(info.getDesc());
            Button btn = adView.findViewById(R.id.ad_btn);
            btn.setText(info.getCallToActionText());
            MediaView mediaView = adView.findViewById(R.id.ad_media);
            nativeAdView = new NativeAdView(MainActivity.this);
            AdIconView adIconView = adView.findViewById(R.id.ad_icon_media);
            nativeAdView.addView(adView);
            nativeAdView.setTitleView(title);
            nativeAdView.setDescView(desc);
            nativeAdView.setAdIconView(adIconView);
            nativeAdView.setCallToActionView(btn);
            nativeAdView.setMediaView(mediaView);
            NativeAd.registerNativeAdView(placementId, nativeAdView, info);
            RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
                    RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
            adContainer.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.openmediation.sdk.nativead.AdIconView
   android:id="@+id/ad_icon_media"
   android:layout_width="50dp"
   android:layout_height="50dp"
   android:layout_below="@id/ad_title"
   android:layout_centerHorizontal="true" />

<com.openmediation.sdk.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. Destroy NativeAd Object

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

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

 

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

Comments

0 comments

Article is closed for comments.