Native Banner Ad
Using the Native Banner Ad API in Android
The native banner ad API allows you to build a customized experience for showing a native ad without the advertiser's creative assets, such as image, video, or carousel content. Similar to native ads, you will receive a group of Ad Properties such as a title, an icon, and a call-to-action, and you will use them to construct a custom view where the ad is shown. However, unlike banner ads, native banner ad API provides a native layout experience so you have full control of customizing the layout for components inside the ad.
Step 1: Requesting a Native Banner Ad
Add the following code at the top of your Activity to import the Facebook Ads SDK:
import com.facebook.ads.*;
Then, instantiate a
NativeBannerAd
object, set an AdListener
, and call loadAd()
in your Activity:public class NativeBannerAdActivity extends Activity { private final String TAG = NativeBannerAdActivity.class.getSimpleName(); private NativeBannerAd nativeBannerAd; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Instantiate a NativeBannerAd object. // NOTE: the placement ID will eventually identify this as your App, you can ignore it for // now, while you are testing and replace it later when you have signed up. // While you are using this temporary code you will only get test ads and if you release // your code like this to the Google Play your users will not receive ads (you will get a no fill error). nativeBannerAd = new NativeBannerAd(this, "YOUR_PLACEMENT_ID"); nativeBannerAd.setAdListener(new NativeAdListener() { @Override public void onMediaDownloaded(Ad ad) { // Native ad finished downloading all assets Log.e(TAG, "Native ad finished downloading all assets."); } @Override public void onError(Ad ad, AdError adError) { // Native ad failed to load Log.e(TAG, "Native ad failed to load: " + adError.getErrorMessage()); } @Override public void onAdLoaded(Ad ad) { // Native ad is loaded and ready to be displayed Log.d(TAG, "Native ad is loaded and ready to be displayed!"); } @Override public void onAdClicked(Ad ad) { // Native ad clicked Log.d(TAG, "Native ad clicked!"); } @Override public void onLoggingImpression(Ad ad) { // Native ad impression Log.d(TAG, "Native ad impression logged!"); } }); // load the ad nativeBannerAd.loadAd(); } }
We will be coming back later to add code to the
onAdLoaded()
method.Step 2: Creating your Native Banner Ad Layout
The next step is to extract the ad metadata and use its properties to build your customized native UI. You can either create your custom view in a layout .xml, or you can add elements in code.
Please consult our guidelines for native ads when designing native banner ads in your app.
In your Activity's layout
activity_main.xml
, add a container for your native banner ad. The container should be a com.facebook.ads.NativeAdLayout which is a wrapper on top of a FrameLayout with some extra functionality that enabled us to render a native Ad Reporting Flow on top of the ad. Later, in onAdLoaded()
method, you will need populate your native ad view into this container.<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> ... <com.facebook.ads.NativeAdLayout android:id="@+id/native_banner_ad_container" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" /> ...</RelativeLayout>
Create a custom layout
native_banner_ad_unit.xml
for your Native Banner Ad:
Below is an example custom layout for your native banner ad:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <RelativeLayout android:id="@+id/ad_choices_container" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="2dp" /> <TextView android:id="@+id/native_ad_sponsored_label" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:ellipsize="end" android:lines="1" android:padding="2dp" android:textColor="@android:color/darker_gray" android:textSize="12sp" /> </LinearLayout> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@android:color/white"> <com.facebook.ads.AdIconView android:id="@+id/native_icon_view" android:layout_width="50dp" android:layout_height="50dp" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:gravity="center" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerVertical="true" android:orientation="vertical" android:paddingLeft="53dp" android:paddingRight="83dp"> <TextView android:id="@+id/native_ad_title" android:layout_width="match_parent" android:layout_height="wrap_content" android:ellipsize="end" android:lines="1" android:textColor="@android:color/black" android:textSize="15sp" android:textStyle="bold" /> <TextView android:id="@+id/native_ad_social_context" android:layout_width="match_parent" android:layout_height="wrap_content" android:ellipsize="end" android:lines="1" android:textSize="12sp" /> </LinearLayout> <Button android:id="@+id/native_ad_call_to_action" android:layout_width="80dp" android:layout_height="50dp" android:layout_alignParentEnd="true" android:layout_alignParentRight="true" android:background="#4286F4" android:gravity="center" android:paddingLeft="3dp" android:paddingRight="3dp" android:textColor="@android:color/white" android:textSize="12sp" android:visibility="gone" /> </RelativeLayout> </LinearLayout>
Step 3: Populating your Layout Using the Ad's Metadata
Scenario 1: Immediately display the ad once it is loaded successfully. Modify the onAdLoaded()
method above to retrieve the Native Banner Ad's
properties and display it as follows:
public class NativeBannerAdActivity extends Activity { private NativeAdLayout nativeAdLayout; private LinearLayout adView; private NativeBannerAd nativeBannerAd; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Instantiate a NativeBannerAd object. // NOTE: the placement ID will eventually identify this as your App, you can ignore it for // now, while you are testing and replace it later when you have signed up. // While you are using this temporary code you will only get test ads and if you release // your code like this to the Google Play your users will not receive ads (you will get a no fill error). nativeBannerAd = new NativeBannerAd(this, "YOUR_PLACEMENT_ID"); nativeBannerAd.setAdListener(new NativeAdListener() { ... @Override public void onAdLoaded(Ad ad) { // Race condition, load() called again before last ad was displayed if (nativeBannerAd == null || nativeBannerAd != ad) { return; } // Inflate Native Banner Ad into Container inflateAd(nativeBannerAd); } ... }); // load the ad nativeBannerAd.loadAd(); } private void inflateAd(NativeBannerAd nativeBannerAd) { // Unregister last ad nativeBannerAd.unregisterView(); // Add the Ad view into the ad container. nativeAdLayout = findViewById(R.id.native_banner_ad_container); LayoutInflater inflater = LayoutInflater.from(NativeBannerAdActivity.this); // Inflate the Ad view. The layout referenced is the one you created in the last step. adView = (LinearLayout) inflater.inflate(R.layout.native_banner_ad_layout, nativeAdLayout, false); nativeAdLayout.addView(adView); // Add the AdChoices icon RelativeLayout adChoicesContainer = adView.findViewById(R.id.ad_choices_container); AdOptionsView adOptionsView = new AdOptionsView(NativeBannerAdActivity.this, nativeBannerAd, nativeAdLayout); adChoicesContainer.removeAllViews(); adChoicesContainer.addView(adOptionsView, 0); // Create native UI using the ad metadata. TextView nativeAdTitle = adView.findViewById(R.id.native_ad_title); TextView nativeAdSocialContext = adView.findViewById(R.id.native_ad_social_context); TextView sponsoredLabel = adView.findViewById(R.id.native_ad_sponsored_label); AdIconView nativeAdIconView = adView.findViewById(R.id.native_icon_view); Button nativeAdCallToAction = adView.findViewById(R.id.native_ad_call_to_action); // Set the Text. nativeAdCallToAction.setText(nativeBannerAd.getAdCallToAction()); nativeAdCallToAction.setVisibility( nativeBannerAd.hasCallToAction() ? View.VISIBLE : View.INVISIBLE); nativeAdTitle.setText(nativeBannerAd.getAdvertiserName()); nativeAdSocialContext.setText(nativeBannerAd.getAdSocialContext()); sponsoredLabel.setText(nativeBannerAd.getSponsoredTranslation()); // Register the Title and CTA button to listen for clicks. List<View> clickableViews = new ArrayList<>(); clickableViews.add(nativeAdTitle); clickableViews.add(nativeAdCallToAction); nativeBannerAd.registerViewForInteraction(adView, nativeAdIconView, clickableViews); } }
The SDK will log the impression and handle the click automatically. Please note that you must register the ad's view with the
NativeBannerAd
instance to enable that. To make the elements of your ad clickable, register it using:registerViewForInteraction(View view, AdIconView adIconView)
Scenario 2: Display the ad in a few seconds or minutes after it is successfully loaded. You should check whether the ad has been invalidated before displaying it.
In case of not showing the ad immediately after the ad has been loaded, the developer is responsible for checking whether or not the ad has been invalidated. Once the ad is successfully loaded, the ad will be valid for 60 mins. You will not get paid if you are showing an invalidated ad. You should call
isAdInvalidated()
to validate the ad.
You should follow the idea below, but please do not copy the code into your project since it is just an example:
public class NativeBannerAdActivity extends Activity { private NativeBannerAd nativeBannerAd; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Instantiate a NativeBannerAd object. // NOTE: the placement ID will eventually identify this as your App, you can ignore it for // now, while you are testing and replace it later when you have signed up. // While you are using this temporary code you will only get test ads and if you release // your code like this to the Google Play your users will not receive ads (you will get a no fill error). nativeBannerAd = new NativeBannerAd(this, "YOUR_PLACEMENT_ID"); nativeBannerAd.setAdListener(new NativeAdListener() { ... }); // load the ad nativeBannerAd.loadAd(); } private void showAdWithDelay() { /** * Here is an example for displaying the ad with delay; * Please do not copy the Handler into your project */ // Handler handler = new Handler(); handler.postDelayed(new Runnable() { public void run() { // Check if nativeBannerAd has been loaded successfully if(nativeBannerAd == null || !nativeBannerAd.isAdLoaded()) { return; } // Check if ad is already expired or invalidated, and do not show ad if that is the case. You will not get paid to show an invalidated ad. if(nativeBannerAd.isAdInvalidated()) { return; } inflateAd(nativeBannerAd); // Inflate Native Banner Ad into Container same as previous code example } }, 1000 * 60 * 15); // Show the ad after 15 minutes } }
Controlling Clickable Area
For a better user experience and better results, you should always consider controlling the clickable area of your ad to avoid unintentional clicks. Please refer to Audience Network SDK Policy page for more details about white space unclickable enforcement.
For finer control of what is clickable, you can use the
registerViewForInteraction(View view, AdIconView adIconView, List<View> clickableViews)
to register a list of views that can be clicked. For example, if you only want to make the ad title and the call-to-action button clickable in the previous example, you can write it like this:@Override public void onAdLoaded(Ad ad) { ... List<View> clickableViews = new ArrayList<>(); clickableViews.add(nativeAdTitle); clickableViews.add(nativeAdCallToAction); nativeBannerAd.registerViewForInteraction(mAdView, nativeAdIconView, clickableViews); ... }
In cases where you reuse the view to show different ads over time, make sure to call
unregisterView()
before registering the same view with a different instance of NativeBannerAd
.
Run the code and you should see a Native Banner Ad:
When an ad is loaded, the following properties will include some value:
title
, icon
and callToAction
. Other properties might be null or empty. Make sure your code is robust enough to handle these cases.
Native Banner Ad
Reviewed by Code Labe
on
April 21, 2019
Rating: 5
Code Labe
Facebook Audience Network