# Native Ads

# Step 1: Initialize NativeAd Object

SDK will trigger a series of events to inform the app of the initialization, loading and presentation results of Native Ads.To use Native Ads, you need to create the NativeAd object, implement and set up Listener for listening events, and then call the loadAd method to load and present Native Ads.

The following code example displays how to use the NativeAd object and implement the NativeAdListener  interface to listen the ads event.The event types that can be triggered by SDK can be found in the following code.

import com.zeus.gmc.sdk.mobileads.mintmediation.nativead.NativeAd;
import com.zeus.gmc.sdk.mobileads.mintmediation.nativead.NativeAdLoader;
import com.zeus.gmc.sdk.mobileads.mintmediation.nativead.NativeAdListener;
...
private NativeAdLoader nativeAdLoader;
private NativeAdListener nativeAdListener = new NativeAdListener() {
    /**
     * Invoked when Native Ad are available.
     * You can then show the video by calling nativeAd.showAd().
     */
    @ Override
    public void onAdReady() {
        //native ad load success
        NativeAd adInfo = nativeAdLoader.getNativeAd();
        if (adInfo == null) {
            return;
        }
    }

    /**
     * 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
    }
};

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

	// init native ad loader
    nativeAdLoader = new NativeAdLoader(this, placementID, nativeAdListener);
	...
}

# Step 2: Load Native Ads

You need to call the  loadAd  method to request and cache Native Ads before presentation of Native Ads.We recommend that you call the loadAd method ahead of time before presentation of Native Ads, so as not to affect the Native Ads experience.

nativeAdLoader.loadAd();

WARNING

Warning: The loadAd  method can be called for multiple times at the same time, but we do not propose to do so.For the continuous call in a short time will not increase the fill rate of Native Ads. If there is already a loading in progress, the new request will not be processed.

WARNING

Warning: It is very dangerous to load Native Ads in the onAdFailed callback event. If you have to load Native Ads here, you must set a time interval limit to avoid the continuous failure causes the endless loop of program due to no network.

# Step 3: Native Ads Elements and How to Obtain Them

The Native Ads elements include: icon, title, description, large campaign image and CTA button. Images ①Icon ②Title ③Description ④Large campaign image ⑤CTA button

  • Public String getTitle()
  • Public String getDesc()
  • Public String getCallToActionText()
import com.zeus.gmc.sdk.mobileads.mintmediation.nativead.NativeAd;
...
private NativeAd adInfo;
...
String titlr = adInfo.getTitle();
String desc = adInfo.getDesc();
String callToActionText = adInfo.getCallToActionText();

# Step 4: Present Native Ads

In this section, we will create the custom view to present Native Ads to users.In this example, we use the Android view, but you can customize it according to the app style.

WARNING

Warning: The registerNativeAdView() method is very important.Please refer to the following code as an example, which allows the third-party SDK to track the presentation time and clicks event of Native Ads.Failure of calling this method can cause errors, such as click button failure.

import com.zeus.gmc.sdk.mobileads.mintmediation.nativead.AdIconView;
import com.zeus.gmc.sdk.mobileads.mintmediation.nativead.MediaView;
import com.zeus.gmc.sdk.mobileads.mintmediation.nativead.NativeAd;
import com.zeus.gmc.sdk.mobileads.mintmediation.nativead.NativeAdLoader;
import com.zeus.gmc.sdk.mobileads.mintmediation.nativead.NativeAdListener;
import com.zeus.gmc.sdk.mobileads.mintmediation.nativead.NativeAdView;
...
private NativeAd nativeAd;
private NativeAdView nativeAdView;
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(nativeAd.getTitle());

// set native ad Call To Action Text
Button btn = adView.findViewById(R.id.ad_btn);
btn.setText(nativeAd.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);

// for yandex only, if you use yandex native ad, you need to set this
TextView yandexDomainView = adView.findViewById(R.id.ad_yandex_domain_view);
TextView yandexPriceView = adView.findViewById(R.id.ad_yandex_price_view);
TextView yandexSponsoredView = adView.findViewById(R.id.ad_yandex_sponsored_view);
TextView yandexWarningView = adView.findViewById(R.id.ad_yandex_warning_view);
ImageView yandexFeedbackView = adView.findViewById(R.id.ad_yandex_feedback_view);
nativeAdView.setDomainView(yandexDomainView);
nativeAdView.setPriceView(yandexPriceView);
nativeAdView.setSponsoredView(sponsoredView);
nativeAdView.setWarningView(warningView);
nativeAdView.setFeedbackView(feedbackView);


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

The R.id.native_ad_container code is shown as follows:

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

The R.layout.native_ad_layout code is shown as follows:

<?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.zeus.gmc.sdk.mobileads.mintmediation.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.zeus.gmc.sdk.mobileads.mintmediation.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" />


<!--Only For Yandex-->
<TextView
  android:id="@+id/ad_yandex_domain_view"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  />

<!--Only For Yandex-->
<TextView
  android:id="@+id/ad_yandex_price_view"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  />

<!--Only For Yandex-->
<TextView
  android:id="@+id/ad_yandex_sponsored_view"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  />

<!--Only For Yandex-->
<TextView
  android:id="@+id/ad_yandex_warning_view"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  />

<!--Only For Yandex-->
<ImageView
  android:id="@+id/ad_yandex_feedback_view"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  />


</RelativeLayout>

# Step 5: MediaView

MediaView is a special view designed to display the main media resources.

  • If the loaded Native Ads contain video resources, the video will be cached and started playing in MediaView.
  • If the loaded Native Ads do not contain video resources, download the first image resource and put it in MediaView.
  • MediaView is a view, which can be defined or dynamically constructed in an XML layout.

# Step 6: AdIconView

AdIconView is a special view used to display the icon resources.

  • If the loaded Native Ads contain icon resources, the icon resources will be downloaded and put in AdIconView.
  • AdIconView is a view, which can be defined or dynamically constructed in an XML layout.

# Step 7: Destroy NativeAd Object

It is recommended to release this NativeAd object when the Native Ads activity is destroyed.

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

# Up Next

You can aggregate other ad types based on our integration documents or test your integration: