Native Ads (Custom)
1. Create a custom view
2. NativeCustomAdView
Add the following Java/Kotlin class named NativeCustomAdView into your project.
Download the Java/Kotlin file here.
3. Put the ad view in the layout
The first step toward displaying a native is to place Yodo1MasNativeAdView
in the layout for the Activity or Fragment in which you’d like to display it. The easiest way to do this is to add one to the corresponding XML layout file. Here’s an example that shows an activity’s Yodo1MasNativeAdView
:
You can alternatively create Yodo1MasNativeAdView
programmatically:
Yodo1MasNativeAdView nativeAdView =
new Yodo1MasNativeAdView(this);
nativeAdView.setLayoutParams(new
ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, dp2px(300)));
// TODO: Add nativeAdView to your view hierarchy.
val nativeAdView = Yodo1MasNativeAdView(this)
nativeAdView.setLayoutParams(ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, dp2px(300)))
// TODO: Add nativeAdView to your view hierarchy.
4. Load the ads
Once Yodo1MasNativeAdView
is in place, the next step is to load an ad. That’s done with the loadAd()
method in the Yodo1MasNativeAdView
class.
Here’s an example that shows how to load an ad in the onCreate()
method of an Activity4
Yodo1MasNativeAdViewBuilder builder = new Yodo1MasNativeAdViewBuilder()
.setTitleTextViewId( R.id.title_text_view )
.setBodyTextViewId( R.id.body_text_view )
.setAdvertiserTextViewId( R.id.advertiser_textView )
.setIconImageViewId( R.id.icon_image_view )
.setMediaContentViewGroupId( R.id.media_view_container )
.setOptionsContentViewGroupId( R.id.options_view )
.setCallToActionButtonId( R.id.cta_button );
nativeAdView = findViewById(R.id.yodo1_mas_native);
nativeAdView.setLayoutId(R.layout.native_custom_ad_view, builder);
nativeAdView.setLayoutView(NativeCustomAdView.class, builder);
nativeAdView.loadAd();
val builder = Yodo1MasNativeAdViewBuilder()
.setTitleTextViewId( R.id.title_text_view )
.setBodyTextViewId( R.id.body_text_view )
.setAdvertiserTextViewId( R.id.advertiser_textView )
.setIconImageViewId( R.id.icon_image_view )
.setMediaContentViewGroupId( R.id.media_view_container )
.setOptionsContentViewGroupId( R.id.options_view )
.setCallToActionButtonId( R.id.cta_button );
nativeAdView = findViewById(R.id.yodo1_mas_native)
nativeAdView.setLayoutId(R.layout.native_custom_ad_view, builder)
nativeAdView.setLayoutView(NativeCustomAdView.class, builder)
nativeAdView.loadAd()
5. Ad events
To further customize the behaviour of your ad, you can hook onto a number of events in the ad’s lifecycle: loading, opening, closing, and so on. You can listen for these events through the Yodo1MasNativeAdListener
class.
nativeAdView.setAdListener(new Yodo1MasNativeAdListener() {
@Override public void onNativeAdLoaded(Yodo1MasNativeAdView nativeAdView) {
// Code to be executed when an ad finishes loading.
}
@Override public void onNativeAdFailedToLoad(Yodo1MasNativeAdView nativeAdView, @NonNull Yodo1MasError error) {
// Code to be executed when an ad request fails.
}
});
nativeAdView.setAdListener(object: Yodo1MasNativeAdListener {
override fun onNativeAdLoaded(nativeAdView: Yodo1MasNativeAdView ? ) {
// Code to be executed when an ad finishes loading.
}
override fun onNativeAdFailedToLoad(nativeAdView: Yodo1MasNativeAdView ? , error : Yodo1MasError) {
// Code to be executed when an ad request fails.
}
})