Banner, Native, and Native Simple Combined Advertisements
NAM SDK provides a way to receive banners, native and native simple advertisements at the same time through GfpAdLoader
.
Since multiple types are supported on a single page, ad unit settings are required. Please discuss this with a NAM manager.
[Step 1] Add and initialize Dependency
Please refer to common integration details.
The following steps will assume that the NAM SDK application has been completed.
[Step 2] Layout settings
Add ViewGroup
where advertisements will be inserted.
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<RelativeLayout
android:id="@+id/ad_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
[Step 3] Check units when creating parameters
Create an AdParam
object with an AdUnitId set that provides advertising response values of multiple Creative Types on a single page. Assign the object when creating GfpAdLoader
.
[Step 4] Receive ad loading events
Events can be received when an ad is successfully loaded through the Load event callback of withxxx() provided for each Creative type.
Please check the banner and native guides.
[Step 5] Receiving advertising events
When loading an ad through GfpAdLoader, the ad events can be received by setting withAdListener(AdEventListener).
[Step 6] Settings for each Creative to be loaded
Assign the method corresponding to the Creative type to be loaded when creating GfpAdLoader.
The example below demonstrates a situation where all Banner, Native, and NativeSimple advertisements are to be received.
- Kotlin
- Java
class MainActivity : AppCompatActivity() {
private lateinit var adLoader: GfpAdLoader
private lateinit var adContainer: ConstraintLayout
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
adContainer = findViewById(R.id.ad_container)
// Set your AdUnitId
val adParam = AdParam.Builder()
.setAdUnitId("YOUR_AD_UNIT_ID")
.build()
adLoader = GfpAdLoader.Builder(this, adParam)
// Receive event callbacks
.withAdListener(object: AdEventListener {
override fun onAdClicked() {
Log.d("MainActivity", "click event")
}
override fun onAdImpression() {
Log.d("MainActivity", "impression event")
}
override fun onAdMuted() {
Log.d("MainActivity", "mute(block) event")
}
override fun onError(error: GfpError, responseInfo: GfpResponseInfo) {
val errorString = String.format(
"code[%d] subCode[%s] message[%s] responseInfo[%s]",
error.errorCode,
error.errorSubCode,
error.errorMessage,
responseInfo.toString()
)
Log.e("MainActivity", errorString)
}
})
.withBannerAd(
GfpBannerAdOptions.Builder()
.setBannerViewLayoutType(BannerViewLayoutType.FLUID_WIDTH)
.build(),
// receive load event
this::inflateBannerAd
)
.withNativeAd(
GfpNativeAdOptions.Builder()
.build(),
// receive load event
this::inflateNativeAd
)
.withNativeSimpleAd(
GfpNativeSimpleAdOptions.Builder()
.setRenderAdBadge(false)
.build(),
// receive load event
this::inflateNativeSimpleAd
)
.build()
adLoader.loadAd()
// . . .
}
private fun inflateBannerAd(bannerAdView: GfpBannerAdView) {
Log.d("MainActivity", "succeed to load [" + bannerAdView.adProviderName + "]")
adContainer.removeAllViews()
adContainer.addView(bannerAdView)
}
private fun inflateNativeSimpleAd(nativeSimpleAd: GfpNativeSimpleAd) {
Log.d("MainActivity", "succeed to load [" + nativeSimpleAd.adProviderName + "]")
adContainer.removeAllViews()
val nativeSimpleAdView = layoutInflater
.inflate(R.layout.content_native_simple_ad, adContainer, false) as GfpNativeSimpleAdView
adContainer.addView(nativeSimpleAdView)
nativeSimpleAdView.setNativeSimpleAd(nativeSimpleAd)
}
private fun inflateNativeAd(nativeAd: GfpNativeAd) {
Log.d("MainActivity", "succeed to load [" + nativeAd.adProviderName + "]")
adContainer.removeAllViews()
// . . .
}
override fun onDestroy() {
super.onDestroy()
if (::adLoader.isInitialized) {
adLoader.cancel()
}
}
}
public class MainActivity extends AppCompatActivity {
private GfpAdLoader adLoader;
private ConstraintLayout adContainer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
adContainer = findViewById(R.id.ad_container);
// Set your AdUnitId
AdParam adParam = new AdParam.Builder()
.setAdUnitId("YOUR_AD_UNIT_ID")
.build();
adLoader = new GfpAdLoader.Builder(this, adParam)
// Receive event callbacks
.withAdListener(new AdEventListener() {
@Override
public void onAdClicked() {
Log.d("MainActivity", "click event");
}
@Override
public void onAdImpression() {
Log.d("MainActivity", "impression event");
}
@Override
public void onAdMuted() {
Log.d("MainActivity", "mute(block) event");
}
public void onError(GfpError error, GfpResponseInfo responseInfo) {
String errorString = String.format("code[%d] subCode[%s] message[%s] responseInfo[%s]",
error.getErrorCode(),
error.getErrorSubCode(),
error.getErrorMessage(),
responseInfo.toString()
);
Log.e("MainActivity", errorString);
}
})
.withBannerAd(
new GfpBannerAdOptions.Builder()
.setBannerViewLayoutType(BannerViewLayoutType.FLUID_WIDTH)
.build(),
// receive load event
this::inflateBannerAd
)
.withNativeAd(
new GfpNativeAdOptions.Builder()
.build(),
// receive load event
this::inflateNativeAd
)
.withNativeSimpleAd(
new GfpNativeSimpleAdOptions.Builder()
.setRenderAdBadge(false)
.build(),
// receive load event
this::inflateNativeSimpleAd
)
.build();
// . . .
adLoader.loadAd();
}
private void inflateBannerAd(GfpBannerAdView bannerAdView) {
Log.d("MainActivity", "succeed to load [" + bannerAdView.getAdProviderName() + "]");
adContainer.removeAllViews();
adContainer.addView(bannerAdView);
}
private void inflateNativeSimpleAd(GfpNativeSimpleAd nativeSimpleAd) {
Log.d("MainActivity", "succeed to load [" + nativeSimpleAd.getAdProviderName() + "]");
adContainer.removeAllViews();
GfpNativeSimpleAdView nativeSimpleAdView = (GfpNativeSimpleAdView) getLayoutInflater()
.inflate(R.layout.content_native_simple_ad, adContainer, false);
adContainer.addView(nativeSimpleAdView);
nativeSimpleAdView.setNativeSimpleAd(nativeSimpleAd);
}
private void inflateNativeAd(GfpNativeAd nativeAd) {
Log.d("MainActivity", "succeed to load [" + nativeAd.getAdProviderName() + "]");
adContainer.removeAllViews();
// . . .
}
@Override
public void onDestroy() {
super.onDestroy();
if (adLoader != null) {
adLoader.cancel();
}
}
}
Advertisements of the set CreativeType could be loaded only if withxxx() is set for each Creative type.
[Step 7] Advertisement removal
Once the display of the native ad is terminated, the ad must be removed for proper disposal.
To remove an ad, call cancel() provided by GfpAdLoader as shown below.
- Kotlin
- Java
adLoader.cancel()
adLoader.cancel();