Single Native Ad in Activity

android

#1

Hello,

I’m an Android Developper and I use Mopub/Fabric to integrate ads in my apps.
For Banner/Interstitiel/Native Ad List, it’s ok.
But I have some issues with a specific way to integrate Native Ad.
Let me explain : I have an Activity (with a ViewPager) which implement an Adapter (BaseAdapter) and a ViewHolder to display data. In each child of my ViewPager, there are :

  • First : Some text
  • Second : My Native Ad
  • Third : A button.

Is there a way to accomplish this with a MoPubAdAdapter or something else ?

Thank you in advance.


#2

Hello,

You can use MoPubAdAdapter to wrap your ViewPager to display native ads.

We recommend having each Native Ad fill its own ViewHolder. By defining the XML layout of the ad, you can have the ad’s text image and CTA button display in the format you want in your image.

Please see our documentation on integrating native ads here.

If you want to display an entire native ad within the ViewHolder, this may be more difficult and may require a custom integration.

If you have any specific questions on how to approach this implementation, please let us know.

Thanks!


#3

Here is how you do it. Create a view for one viewpager page and inflate it from XML. The XML should contain your layout. Below is a custom view class for a banner advert.

public class AdvertBannerNative extends LinearLayout {
    private static String LOG_TAG = AdvertBannerNative.class.getName();
    private Context mContext;
    public AdvertBannerNative(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        MoPubNative.MoPubNativeEventListener moPubNativeEventListener = new MoPubNative.MoPubNativeEventListener() {
            @Override
            public void onNativeImpression(View view) { }
            @Override
            public void onNativeClick(View view) { }
        };

        MoPubNative.MoPubNativeNetworkListener moPubNativeNetworkListener = new MoPubNative.MoPubNativeNetworkListener() {
            @Override
            public void onNativeLoad(NativeResponse nativeResponse) {
                ViewBinder viewBinder = new ViewBinder.Builder(R.layout.ad_native_mopub_banner)
                        .iconImageId(R.id.native_icon_image)
                        .titleId(R.id.native_title)
                        .textId(R.id.native_text)
                        .callToActionId(R.id.native_cta)
                        .build();



                AdapterHelper ah = new AdapterHelper(mContext, 0, 3);
                View v = ah.getAdView(null, AdvertBannerNative.this, nativeResponse, viewBinder, null);

                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);

                addView(v, params);
            }
            @Override
            public void onNativeFail(NativeErrorCode errorCode) {}
        };

        String ad_id = "advert id here";
        MoPubNative moPubNative = new MoPubNative(context, ad_id, moPubNativeNetworkListener);
        moPubNative.setNativeEventListener(moPubNativeEventListener);

        final EnumSet<RequestParameters.NativeAdAsset> desiredAssets = EnumSet.of(
                RequestParameters.NativeAdAsset.TITLE,
                RequestParameters.NativeAdAsset.TEXT,
                RequestParameters.NativeAdAsset.ICON_IMAGE,
                RequestParameters.NativeAdAsset.CALL_TO_ACTION_TEXT);

        final Location location = null;
        final String keywords = "";

        RequestParameters mRequestParameters = new RequestParameters.Builder()
                .location(location)
                .keywords(keywords)
                .desiredAssets(desiredAssets)
                .build();

        moPubNative.makeRequest(mRequestParameters);
    }
}