Skip to main content

StandNative

Step 1.Prerequisites#

This document mainly introduces how to integrate Native Ads :

  • Use Android Studio 3.2 or later
  • minSdkVersion 16 or later
  • compileSdkVersion 28 or later
  • Recommended: Create your TradPlus Apps and get your AppId
  • Initialize the TradPlus SDK for Android

Step 2.Integration#

2.1 Add AdView to the layout#

Start by including this XML block to your Activity’s or Fragment’s layout.

(1)V6.X New API#

<FrameLayout
android:id="@+id/ad_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"/>

(2)V5.X Old API#

<com.tradplus.ads.mobileads.TradPlusView
android:id="@+id/NativeView"
android:layout_width="320dp"
android:layout_height="340dp"
android:layout_centerHorizontal="true"
android:layout_alignParentTop="true">
</com.tradplus.ads.mobileads.TradPlusView>

2.2 Create an Ad#

(1)V6.X New API#

TPNative tpNative;
ViewGroup adContainer;
adContainer = findViewById(R.id.ad_container);
//create adUnit Id from your App
tpNative = new TPNative(context,"AdUnitId");

(2)V5.X Old API#

TradPlusView mTradPlusNative;
mTradPlusNative = findViewById(R.id.NativeView);
mTradPlusNative.setAdUnitId("AdUnitId");
//Set Ad Size (Optional)
mTradPlusNative.setAdSize(320, 340);
//Set the layout of the ad, the xml file is obtained from the Demo or Download SDK file
mTradPlusNative.setAdLayoutName("native_ad_list_item","video_ad_item");
  • How to create Add Unit , as shown below:

  • After add Add Unit, you can get adUnitId, as shown below:

2.3 Implement Event Listeners#

(1)V6.X New API#

tpNative.setAdListener(new NativeAdListener() {
@Override
public void onAdLoaded(TPAdInfo tpAdInfo, TPBaseAd tpBaseAd) {
// Called when the ad for the given adUnitId has loaded.
// Show an Ad
tpNative.getNativeAd().showAd(adContainer, R.layout.native_ad_list_item, "");
}
@Override
public void onAdClicked(TPAdInfo tpAdInfo) {
// Called when a native ad is clicked.
}
@Override
public void onAdImpression(TPAdInfo tpAdInfo) {
// Called when a native ad starts playing.
}
@Override
public void onAdShowFailed(TPAdError tpAdError, TPAdInfo tpAdInfo) {
// Called when the ad fails to load for the given adUnitId.
}
@Override
public void onAdLoadFailed(TPAdError tpAdError) {
// Called when the ad fails to load for the given adUnitId.
}
@Override
public void onAdClosed(TPAdInfo tpAdInfo) {
// Called when a native ad is closed.
}
});

(2)V5.X Old API#

mTradPlusNative.setAdViewListener(new TradPlusView.FSAdViewListener() {
@Override
public void onAdViewLoaded(TradPlusView tradPlusView) {
// Called when the ad for the given adUnitId has loaded.
}
@Override
public void onAdViewFailed(TradPlusView tradPlusView,TradPlusErrorCode tradPlusErrorCode) {
// Called when the ad fails to load for the given adUnitId.
}
@Override
public void onAdViewClicked(TradPlusView tradPlusView) {
// Called when a native ad is clicked.
}
@Override
public void onAdViewExpanded(TradPlusView tradPlusView) {
// Called when a native ad starts playing.
}
@Override
public void onAdViewCollapsed(TradPlusView tradPlusView) {}
@Override
public void onAdsSourceLoaded(Object o) {}
);

2.4 Load an Ad#

(1)V6.X New API#

tpNative.loadAd();

(2)V5.X Old API#

mTradPlusNative.loadAd();

2.5 Show an Ad#

(1)V6.X New API#

//Called when the ad Loaded
//@param:ViewGroup
//@param:layout,R.layout.native_ad_list_item you can get from Demo or download file
//@param:pass empty string
tpNative.getNativeAd().showAd(adContainer, R.layout.native_ad_list_item, "");
  • V5.X Old API

After the ad is loaded successfully, it will be automatically populated into the layout.

2.6 Release resources#

(1)V6.X New API#

@Override
protected void onDestroy() {
tpNative.onDestroy();
super.onDestroy();
}

(2)V5.X Old API#

@Override
protected void onDestroy() {
mTradPlusNative.destroy();
super.onDestroy();
}