Docy

Native Ads (Custom)

1. Create a custom view

				
					<!-- native_custom_ad_view.xml -->
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
		xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    
</androidx.constraintlayout.widget.ConstraintLayout>
				
			

2. 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:

				
					<com.yodo1.mas.nativeads.Yodo1MasNativeAdView 
	xmlns:masads="http://schemas.android.com/apk/res-auto"
	android:id="@+id/yodo1_mas_native"
	android:layout_width="match_parent"
	android:layout_height="300dp"
	android:layout_gravity="center_horizontal|top"/>
				
			

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.
				
			

3. 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 Activity:

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

4. Ad events

To further customize the behavior 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.
    }
})
				
			
CONTENTS