Single Native Ad without Adapter

mopub
android

#1

Hi, I am willing to use the native ad without the adapter. My scenario is this, I am having a layout were i need to place an ad. This parent layout doesn’t contain any adapter views like listiview or recyclerview. I would like to use native ad similar to banner ads without adapter views.

I managed to do this. But i get a logcat message “Unable to set Visibility Tracker due to no available root view”. I can understand this message as i have 2 different layouts one for the parent view and one for the ad view. Also my concern is the size as i dont have enough space for the medium rectangle banner ads. I have switched for native ads so that i can place a custom sized ad. So I would like to know that is there a way to implement only a single instance of native ad (Similar to banner ads)?


#2

Hi!

Could you put some code? Like the layout code and the code you’re using for loading a native ad?


#3

Hello, Thanks for your reply. Below is my layout code for the native ad.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content" android:layout_height="wrap_content"
    >

<ImageView
    android:id="@+id/native_ad_main_image"
    android:layout_width="230dp"
    android:layout_height="120dp"
    android:layout_centerHorizontal="true"
    android:layout_below="@+id/native_ad_text"
    />
<ImageView
    android:id="@+id/native_ad_icon_image"
    android:layout_width="40dp"
    android:layout_height="40dp"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:padding="@dimen/tenDpDimension"
    />

<TextView android:id="@+id/native_ad_title"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:layout_alignRight="@+id/native_ad_icon_image"
    android:layout_alignEnd="@+id/native_ad_icon_image"
    android:layout_marginLeft="@dimen/twentyDpDimension"
    android:layout_marginStart="@dimen/twentyDpDimension"
    android:layout_marginTop="@dimen/oneDpDimension"
    />
<TextView android:id="@+id/native_ad_text"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:layout_below="@id/native_ad_icon_image"
    android:layout_marginTop="@dimen/oneDpDimension"
    android:layout_centerHorizontal="true"
    />
<TextView android:id="@+id/native_ad_call_to_action"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:layout_above="@id/native_ad_main_image"
    android:layout_marginTop="@dimen/oneDpDimension"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:layout_marginEnd="@dimen/tenDpDimension"
    android:layout_marginRight="@dimen/tenDpDimension"
    />

</RelativeLayout>

The above layout is for the native ad. I have another layout which is a main layout in which i have used the include tag to use the above layout inside my main layout. Nothing special there. I have left enough space for the ad. Below is the Java code which i used to initialize the mopub native ad.

 void initAds(){
    MoPubNative.MoPubNativeNetworkListener moPubNativeListener = new MoPubNative.MoPubNativeNetworkListener() {
        @Override
        public void onNativeLoad(NativeAd nativeAd) {
 
            View adViewRender = nativeAd.createAdView(context,getParentView());
            nativeAd.renderAdView(adViewRender);
            nativeAd.prepare(adViewRender);
        }

        @Override
        public void onNativeFail(NativeErrorCode errorCode) {
            Log.d("LockUpMopub",errorCode+ " errorcode");
        }
    };

    MoPubNative mMoPubNative = new MoPubNative(context
            ,getResources().getString(R.string.activity_ad_unit_id),moPubNativeListener);

    ViewBinder viewBinder = new ViewBinder.Builder(R.layout.native_ad_sample)
            .mainImageId(R.id.native_ad_main_image)
            .iconImageId(R.id.native_ad_icon_image)
            .titleId(R.id.native_ad_title)
            .textId(R.id.native_ad_text)
            .callToActionId(R.id.native_ad_call_to_action)
            .build();

    MoPubStaticNativeAdRenderer adRenderer = new MoPubStaticNativeAdRenderer(viewBinder);

    mMoPubNative.registerAdRenderer(adRenderer);
    mMoPubNative.makeRequest();
}

So this completes my ad request. I haven’t used any adapter or view pager. I am willing to have a single native ad which adnetwork like Facebook Audience Network and admob supports. Mopub only allows me to place ad via adapter which i have no use here. I have attached a screenshot of what i expect. Thank you very much for this help. I have been looking for this from past week. I have no other go so i tried to reach you on twitter and facebook. Thanks again.


#4

And what is the layout of the parent in which you want to include your native ad?


#6

I have wrapped the above layout using an include tag inside a RelativeLayout. And that Relative layout is wrapped under a FrameLayout. I tried using this both while creating the adview in onNativeLoad. But it gives me the same error. I even receive the same error when i pass null as parameter corresponding to your sample.

Should i use an include tag to insert the ad layout into my main layout? Or how does the adFramework know my parent. Even if i pass a parent it is unable to detect a parent. In your sample you haven’t pointed how this is handled. FYI i haven’t activated the marketplace yet. I have been using the Demo Line ad unit for testing. Will that be an issue?


#7

You don’t have to wrap the native layout in your parent with an include tag.
If you do it like that you will include your native ad layout but it won’t be populated with ad image, text, call to action…
The native ad layout is created and populated programmatically with the ViewBinder and the MoPubStaticNativeAdRenderer.

You should use the view you’ve created in onNativeLoad() callback and include it programmatically in your parent view.

In your case you ends up with 2 native ad view. One you included with tag in your XML layout (this one is not filled with an ad). And the other native ad view is created by your code but you do nothing with it.
That’s probably why you have the logcat message. Because you’ve created the ad view but you never render it in your view hierarchy.


#8

Thank you for this info. I think this is the issue. I will try modifying my code and will let you know. As i have seen from your code. You haven’t added the rendered view into your view hierarchy once you get the ad view in onNativeLoad. So I am bit confused.


#9

Indeed it was only a part of my code because I wanted to remove extra code I needed in my use case. But the next step is just to insert the native ad created in the view hierarchy.


#10

Actually I thought the view will add itself to the root view if i pass the parent view in createAdView(). But this was a failure so I am confused already.I must have tried to add the view explicitly. But now I found the issue. Thank you for taking time to reply me. This helped a lot. Thank you very much.


#11

You’re welcome :slight_smile: Hope everything is OK for you now :wink:


#12

Everything is fine. Just a little confusion with createAdView. Now you have cleared it. Thanks for the Swift help. Hope our posts may help others.


#13

I try to make the same, use native ad as banner without adapters.
I have nearly the same code but impression tracking was stuck to 0.

Thanks for showing that i nee to call prepare on the native ad.

I have another problem now, some native from Applovin and Mopub marketplace don’t react on click.
Facebook test ads and flurry open a new screen.

public void onNativeLoad(NativeAd nativeAd) {

        LinearLayout ll = (LinearLayout) findViewById(R.id.bannerNative);

        View mView = nativeAd.createAdView(getApplicationContext(), ll);
        nativeAd.clear(mView);

        nativeAd.renderAdView(mView);
        nativeAd.prepare(mView);
        


        ll.removeAllViews();
        ll.addView(mView);

        mopubHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                mopubMakeRequest();
            }
        }, 30 * 1000);

    }

I have try to call prepare after or before renderAdView, still nothing on click.

Should i call clear if i remove the view later like in my code ?

Edit: For ApplovinAdapter i have found this commented code but when tested it get the click but don’t open a new screen

@Override
public void prepare(View view) {

    mView = view;
    mView.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Log.d(TAG, "onClick");
            mNativeAd.launchClickTarget(mContext);
            notifyAdClicked();
        }
    });

    trackImpression(mNativeAd);

}

Still no idea why the ads from Mopub market place don’t react on click.


#14

I really don’t understand your scenario. But I have seen the code for the nativead and if the view is destroyed the impression tracker or click tracker does not work. But I don’t know the case for clear. As both the destroy and clear which we call delegates to a superclass reference of an abstract BaseNaiveAd. But I really don’t know from where the actual native ad is from. Link provided below. As far as I know click listeners are automatically injected by mopub API. Hope this helps.


#15

I have remove the clear, it was for testing because nothing move on user click (with or without clear).

After looking at Flurry and Facebook adapter, i think they handle the click event directly. That’s why it works for them.

But for Applovin or Mopub market place, something is missing, probably in my code, that link user click to Mopub default action.

Do you have something that move on user click with your code ? With which network ?

Edit:
The logic is to display a banner like with native but if we don’t refresh manually, we get just one ad.
I begin to make a request, render / prepare and add it to the view.
At this point i need another ad 30s later.
When the ad refresh, i need to remove the previous native ad otherwise the new one doesn’t display.

Here is a code more clean.
Still no click event, even on first ad load.

public void onNativeLoad(NativeAd nativeAd) {
    LinearLayout ll = (LinearLayout) findViewById(R.id.bannerNative);
    ll.removeAllViews();

    View mView = nativeAd.createAdView(getApplicationContext(), ll);
    nativeAd.renderAdView(mView);
    nativeAd.prepare(mView);
   
    ll.addView(mView);

    mopubHandler.postDelayed(new Runnable() {
        @Override
        public void run() {
           mopubMakeRequest();
        }
    }, 30 * 1000);

}

#16

Did you tried nativeAd.handleClick(adView); ?


#17

This method is called when a user click on a Ad, we can’t call it directly like prepare.
The @VisibleForTesting just above means we should not call it directly.
Even if i try to use it, i get a red error on this line.

Have you make some tests with Mopub marketplace Ads if they react to click with your code ?


#18

Hello,
I had followed this method for that I want to show native ad in my ViewPager.
For now, I want to “cache” the ad.
What I did is to save the nativeAd in an Array as a static variable.
But there comes some problem.
When the cache ad is loaded, I still do createAdView, renderAdView and prepare.
But I found that sometimes when two ad is the same between the viewPager.
One of the ad can’t click.
The most weird thing is that it won’t happen when mediate the marketplace ad(as direct sales) but happen on facebook and flurry mediation.

Hope someone can help thanks.


#19

Hello, Is you problem fixed with View Pager? I think the third party ads is handling click events by itself. Mopub is not handling it. That is the reason the 3rd part ads cannot be clicked. There must be a way to dispatch the click event to the 3rd party ad. But I don’t know how to do it. I found the following from mopub resources section.

Mediated SDK Ads:
If you are mediating SDK networks through MoPub, the partner SDK will be responsible for the click-through behavior. Mopub will not be able to alter the behavior.


#20

Hi, I had the same issue you had. I doubted that the 3rd party ads are not receiving click events. But I solved the issue. The solution is to not call NativeAd.clear(), until and unless you don’ need the ad. I was clearing the ad in onImpressed and onClicked as well. The mistake is, the impression is tracked as soon as the view is visible and onImpression is called which clears the adView, in my case. That is the reason i cannot click on ads. I hope it will be useful for someone else


#21

@raaja090 If it comes error (Server returned empty response) in onNativeFail , what is it ? do you know ?