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 Get Started in Android Studio.
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 combines that template with advertiser assets
like icons, images, and text,
and displays 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 a description of the format and more information on choosing a template size for your ad units, see Overview of native ads express. 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_heightandandroid:layout_widthshould both be set towrap_content.ads:adUnitIdshould 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_unitfor example).ads:AdSizeshould be the desired size for the Native Express ad. Sizes of the formWIDTHxHEIGHTare 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 occupies 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);
Native video
In addition to images, text, and numbers, some native ads contain video assets. At the current time, this is limited to the app install format, but may be extended to content ads in the future.
To simplify the configuration and display of video, the Mobile Ads SDK provides the following video-related classes for Native Ads Express:
VideoOptions
The VideoOptions
class allows apps to configure how native video assets should behave.
VideoOptions objects can be assigned to a NativeExpressAdView
via the setVideoOptions method.
mNativeExpressAdView.setVideoOptions(new VideoOptions.Builder()
.setStartMuted(true)
.build());
The VideoOptions.Builder
class currently offers one method,
setStartMuted,
which tells the SDK whether video assets should start in a muted state.
The default value is true.
VideoController
The VideoController
class is used to retrieve information about video assets.
Apps can get a reference to the controller from a
NativeExpressAdView by calling
getVideoController
VideoController vc = myNativeExpressAdView.getVideoController();
This method always returns a VideoController object,
even when no video asset is present in the ad.
VideoController offers these methods for querying video state:
hasVideoContent()- Returnstrueif the ad has a video asset, andfalseif it doesn't.getAspectRatio()- Returns the aspect ratio of the video (width/height), or zero if no video aspect is present.
Apps can also use the
VideoController.VideoLifecycleCallbacks
class to get notifications when events occur in the lifecycle
of a video asset.
VideoController vc = mNativeExpressAdView.getVideoController();
vc.setVideoLifecycleCallbacks(new VideoController.VideoLifecycleCallbacks() {
public void onVideoEnd() {
// Here apps can take action knowing video playback is finished
// It's always a good idea to wait for playback to complete before
// replacing or refreshing a native ad, for example.
super.onVideoEnd();
}
});
Next steps
For more information on the best ways to use Native Ads Express, check out these resources:
- To explore targeting capabilities of native ads, see Targeting.
- To learn about Ad Events and the
AdListenerclass, see Ad Events. - To see Native Ads Express in action, download our sample project from GitHub.

