Adding Native Express Ads Programmatically

1,168 views
Skip to first unread message

Praveen Singh

unread,
Sep 12, 2016, 1:16:20 PM9/12/16
to Google Mobile Ads SDK Developers
I am trying to programmatically add Native ads in my app so that the ad can fill up to the width of the screen.Instead of ads, I am getting an empty space of the same size.When I am passing the hardcode values as the ad size, it's working fine. Please let me know what am I missing here?

Layout.xml 

<com.google.android.gms.ads.NativeExpressAdView
android:id="@+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adUnitId="ca-app-pub-6667404740993831/9531692095" <-- HAVE TO PUT SOMETHING HERE, OTHERWISE IT'S GIVING AN ERROR
ads:adSize="SMART_BANNER"> <-- HAVE TO PUT SOMETHING HERE, OTHERWISE IT'S GIVING AN ERROR
</com.google.android.gms.ads.NativeExpressAdView>

Java Code 

NativeExpressAdView adViewContainer = (NativeExpressAdView)findViewById(R.id.adView);
NativeExpressAdView mAdView = new NativeExpressAdView(this);
mAdView.setAdSize(new AdSize(AdSize.FULL_WIDTH,300));
mAdView.setAdUnitId("ca-app-pub-6667404740993831/9531692095");
adViewContainer.addView(mAdView);
AdRequest request = new AdRequest.Builder().build();
mAdView.loadAd(request);

Vu Chau (Mobile Ads SDK Team)

unread,
Sep 12, 2016, 3:05:34 PM9/12/16
to Google Mobile Ads SDK Developers
Hi Praveen,

The issue with your implementation is that you do not need the XML if you are programmatically adding the NativeExpressAdView to your layout.  So you can clean up the redundant XML's AdView code.

The other issue is that you are adding a NativeExpressAdView (mAdView) into another NativeExpressAdView (adViewContainer).  The parent of your NativeExpressAdView has to be a layout (RelativeLayout, et al.).  So change that and you should be able to see your ad rendered, at full-width.

P.S: The default RelativeLayout from Android has all-around paddings (16dp) so you might need to remove the paddings (I did) to get the ad to show.

Vu Chau
Mobile Ads SDK Team

Praveen Singh

unread,
Sep 13, 2016, 5:04:57 AM9/13/16
to Google Mobile Ads SDK Developers
Thanks for the Reply !!

I have one question here.. if i remove the XML then how could I define the position of ads? Right now, I want it under a TextView so I can put this ads XML
under the TextView  XML. Please suggest

Vu Chau (Mobile Ads SDK Team)

unread,
Sep 13, 2016, 10:40:26 AM9/13/16
to Google Mobile Ads SDK Developers
Hi Praveen,

There are many ways to programmatically position your layouts.  The easiest way would be to use a LinearLayout with a vertical orientation.  Since in LinearLayouts views are added linearly (one above/below another), you can add a TextView first before adding AdView.  This way the AdView will always be below your TextView.

If you are using a RelativeLayout, use the android:layout_below constraint to programmatically position another view below another view.  Here's how to do that. 

Vu Chau
Mobile Ads SDK Team

Reply all
Reply to author
Forward
0 new messages