This guide shows you how to use the Google Mobile Ads SDK
to display AdMob Native Express ads in Android applications.
It covers adding a
NativeExpressAdView
to a layout, how to request ads, and so on.
Prerequisite
This guide assumes some working knowledge of the Google Mobile Ads SDK. If you haven't already done so, consider running through our Get Started in Android Studio guide.
What's a Native Express ad?
Native Express ads are similar to banners in that they're rectangular ads that
you can drop into a layout and size how you like.
The key difference is that you, the publisher,
can control the ad's presentation details
(things like image sizes, fonts, colors, and so on)
by uploading a CSS template for your ad unit.
AdMob will combine that template with advertiser assets
like icons, images, and text,
and display the result in a NativeExpressAdView
.
This approach minimizes the amount of Java code needed for Native Ads Express,
while helping publishers display ads that look natural in their app.
Create a Native Ads Express ad unit
Native Ads Express ad units are created at apps.admob.com. For an overview of the format and more information on choosing a template size for your ad units, see this guide in our help center. For more information on writing CSS code that gives your Native Express ads a natural, unobtrusive style, see the Guide to custom CSS for native ads express.
NativeExpressAdView
The NativeExpressAdView
class is responsible
for requesting and displaying Native Express ads.
Here's an example NativeExpressAdView
element
that might appear in an XML layout file:
<com.google.android.gms.ads.NativeExpressAdView
android:id="@+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adUnitId="ca-app-123123123123/123123123"
ads:adSize="320x150">
</com.google.android.gms.ads.NativeExpressAdView>
The attributes of the element should be set as follows:
android:layout_height
andandroid:layout_width
should both be set towrap_content
.ads:adUnitId
should be set to a valid Native Ads Express ad unit ID. You can insert an ad unit ID or use a reference to a string resource (@string/my_ad_unit
for example).ads:AdSize
should be the desired size for the Native Express ad. Sizes of the formWIDTHxHEIGHT
are typical, though there are some other options, as you'll see in the next section.
Choose a size
Rather than forcing publishers to choose among fixed sizes, Native Ads Express offers several template sizes (chosen when creating an ad unit), each with a range of height and width values in device-independent pixels (dp):
Template size | Min width | Max width | Min height | Max height |
---|---|---|---|---|
Small | 280 | 1200 | 80 | 612 |
Medium | 280 | 1200 | 132 | 1200 |
Large | 280 | 1200 | 250 | 1200 |
A publisher who wants to display a medium template size can use widths between 280 and 1200 dp and heights from 132 to 1200 dp. That means that 300 by 200, 450 by 150, and 613 by 572 are all valid for the medium template size. Bear in mind, though, that not all sizes are likely to make for a good presentation. While it's technically possible to request a small template with a size of 1200 by 80, it's probably not the best choice! Also, be sure to consider the screen dimensions of the device on which you're displaying the ad. Larger sizes should generally be reserved for presentation on tablets.
Apps aren't required to use the same size for every request. The same ad unit could be requested with one size in portrait orientation and another in landscape, or in different sizes according to the particular device it's running on. In the event that an app makes a request with an ad size that falls outside the range for the ad unit's template, though, an error could be returned.
Publishers can also use the
FULL_WIDTH
constant when programmatically creating an
AdSize
for a NativeExpressAdView
.
In this case, the ad will occupy the entire width of the device screen.
At this time, the AUTO_HEIGHT
constant and FLUID
ad size
should not be used with Native Ads Express.
Load an ad
Loading ads is done by calling the loadAd
method
in NativeExpressAdView
.
This method accepts an AdRequest
object
that publishers can use to add information to the request:
MainActivity.java (excerpt)
NativeExpressAdView adView = (NativeExpressAdView)findViewById(R.id.adView);
AdRequest request = new AdRequest.Builder()
.addTestDevice("YOUR_DEVICE_ID")
.build();
adView.loadAd(request);
Further reading
For more information on the best ways to use Native Ads Express, check out these resources:
- To explore targeting capabilities of native ads, check out the Targeting guide.
- To learn about Ad Events and the AdListener class, check out the Ad Events guide.
You can also see Native Ads Express in action by downloading our sample project from GitHub.