Single Native Ad without Adapter



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



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


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

<RelativeLayout xmlns:android=""
    android:layout_width="wrap_content" android:layout_height="wrap_content"


<TextView android:id="@+id/native_ad_title"
<TextView android:id="@+id/native_ad_text"
<TextView android:id="@+id/native_ad_call_to_action"


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() {
        public void onNativeLoad(NativeAd nativeAd) {
            View adViewRender = nativeAd.createAdView(context,getParentView());

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

    MoPubNative mMoPubNative = new MoPubNative(context

    ViewBinder viewBinder = new ViewBinder.Builder(R.layout.native_ad_sample)

    MoPubStaticNativeAdRenderer adRenderer = new MoPubStaticNativeAdRenderer(viewBinder);


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.


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


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?


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.


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.


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.


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.


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


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.


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

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



        mopubHandler.postDelayed(new Runnable() {
            public void run() {
        }, 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

public void prepare(View view) {

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

        public void onClick(View v) {
            Log.d(TAG, "onClick");



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


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.


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 ?

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

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

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



Did you tried nativeAd.handleClick(adView); ?


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 ?


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.


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.


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


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