Single Native Ad in Activity




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.



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.



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() {
            public void onNativeImpression(View view) { }
            public void onNativeClick(View view) { }

        MoPubNative.MoPubNativeNetworkListener moPubNativeNetworkListener = new MoPubNative.MoPubNativeNetworkListener() {
            public void onNativeLoad(NativeResponse nativeResponse) {
                ViewBinder viewBinder = new ViewBinder.Builder(R.layout.ad_native_mopub_banner)

                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);
            public void onNativeFail(NativeErrorCode errorCode) {}

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

        final EnumSet<RequestParameters.NativeAdAsset> desiredAssets = EnumSet.of(

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

        RequestParameters mRequestParameters = new RequestParameters.Builder()