Skip to main content

Banner

Step 1.Prerequisites#

This document mainly introduces how to integrate Banner 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/BannerView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true">
</com.tradplus.ads.mobileads.TradPlusView>

2.2 Create an Ad#

(1)V6.X New API#

TPBanner tpBanner;
ViewGroup adContainer;
...
adContainer = findViewById(R.id.ad_container);
tpBanner = new TPBanner(context);
adContainer.addView(tpBanner);

(2)V5.X Old API#

TradPlusView mTradPlusView;
mTradPlusView = findViewById(R.id.BannerView);
mTradPlusView.setAdUnitId("AdUnitId");

2.3 Implement Event Listeners#

(1)V6.X新接口#

tpBanner.setAdListener(new BannerAdListener() {
@Override
public void onAdClicked(TPAdInfo tpAdInfo) {
// Called when a banner ad is clicked.
}
@Override
public void onAdImpression(TPAdInfo tpAdInfo) {
// Called when a banner ad starts playing.
}
@Override
public void onAdLoaded(TPAdInfo tpAdInfo) {
// Called when the ad for the given adUnitId has loaded.
}
@Override
public void onAdLoadFailed(TPAdError error) {
// Called when the ad fails to load for the given adUnitId.
}
@Override
public void onAdClosed(TPAdInfo tpAdInfo) {
// Called when a banner ad is closed.
}
});

(2)V5.X Old API#

mTradPlusView.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 banner ad is clicked.
}
@Override
public void onAdViewExpanded(TradPlusView tradPlusView) {
// Called when a banner 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#

//Create adUnit Id from your App
tpBanner.loadAd("AdUnitId");

(2)V5.X Old API#

mTradPlusView.loadAd();
  • How to create Add Unit , as shown below:

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

2.5 Hide and show ads#

(1)V6.X New API#

//You can hide or show an ad using setVisibility when an ad is Loaded
tpBanner.setVisibility(View.GONE);
//You can show an ad After hide ad
tpBanner.setVisibility(View.VISIBLE);

(2)V5.X Old API#

//You can hide or show an ad using setVisibility when an ad is Loaded
mTradPlusView.setVisibility(View.GONE);
//You can show an ad After hide ad
mTradPlusView.setVisibility(View.VISIBLE);

2.5 Release resources#

(1)V5.X Old API#

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