Show Native Ads on Android

Native ads give you the ability to create ads that are customized to match the look and feel of the rest of your application. This page describes our Native Ads API at a high level, and includes a usage example. For a complete reference, see the Android SDK Reference.

Native networks supported through mediation:

In order to serve native ads, you will send a native ad request, and receive a native ad response. For Android 9 and above and API v. 28 and above, the request must be HTTPS by default in order to track viewability accurately.  You can enable HTTPS with useHttps(true).

In the example code below, we:

Maintain references to native views and native response objects.

It is your responsibility to keep a reference to the native ad view and NativeAdResponse object if necessary.

public class MyActivity extends Activity {
 
    Context activityContext;
    NativeAdResponse nativeAdResponse;
    LinearLayout container;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
 
        activityContext = this;
 
        // Create a NativeAdRequest object
        NativeAdRequest adRequest = new NativeAdRequest(activityContext, "123456"); // Placement ID
         
        // Optionally set the renderer_id
        //adRequest.setRendererId(123);
 
        // Create a listener for ad events
        NativeAdEventListener adEventListener = new
                NativeAdEventListener() {
                    @Override
                    public void onAdWasClicked() {
                        // Do something when the view is clicked
                    }
 
                    @Override
                    public void onAdWillLeaveApplication() {
                        // Do something when the ad is taking user away from current app
                    }
 
                    @Override
                    public void onAdWasClicked(String clickUrl, String fallbackURL) {
                        // Handle Click URL
                    }
                };
 
        // Whether to pre-load the native ad's icon and main image
        adRequest.shouldLoadIcon(true);
        adRequest.shouldLoadImage(true);
 
        adRequest.setListener(new NativeAdRequestListener() {
            @Override
            public void onAdLoaded(NativeAdResponse response) {
                nativeAdResponse = response;
                // Cover image
                ImageView imageView = new ImageView(activityContext);
                imageView.setImageBitmap(response.getImage());
 
                // Icon image
                ImageView iconView = new ImageView(activityContext);
                iconView.setImageBitmap(response.getIcon());
 
                // Title
                TextView title = new TextView(activityContext);
                title.setText(response.getTitle());
 
                // Main text
                TextView description = new TextView(activityContext);
                description.setText(response.getDescription());
 
                // Text that indicates a call to action -- for example, to install an app
                TextView callToAction = new TextView(activityContext);
                callToAction.setText(response.getCallToAction());
 
                // Create a container (a parent view that holds all the
                // views for native ads)
                LinearLayout container = new LinearLayout(activityContext);
                container.addView(iconView);
                container.addView(title);
 
                // Add the native ad container to the view hierarchy
                LinearLayout ad_frame = findViewById(R.id.native_ad_frame);
                ad_frame.addView(container);
            }
 
            @Override
            public void onAdFailed(ResultCode errorcode) {
 
            }
        });
 
        // Call loadAd() to request a response once
        adRequest.loadAd();
 
        // Register native views for click and impression tracking.  The
        // adEventListener is the listener created above; it can be null if
        // you don't want to receive notifications about click events.
        // Impressions and clicks won't be counted if the view is not registered.
        NativeAdSDK.registerTracking(nativeAdResponse, container, adEventListener);
 
        // It's your responsibility to keep a reference to the view
        // and NativeAdResponse object if necessary.
        // Once done with the native ad view, call the following method to
        // unregister that view.
        NativeAdSDK.unRegisterTracking(container);
    }
}
    

Fields Supported in Native

As of version 5.0 of the Mobile SDK, support for native assets is aligned with how native creatives are set up in Xandr's UI.

If you are still using Legacy Native in Xandr's UI, you will need to move to "New" Native for your creatives.

The following is a comprehensive list of native assets supported in the SDKs.

AssetSupported Pre 5.0?Supported Post 5.0?v5.0+ API-Usage Example
Image, Width, HeightYes, Yes, YesYes, Yes, Yes
          nativeAdResponse.getImage();
nativeAdResponse.getImageSize();
          nativeAdResponse.getImageUrl();
Icon+Width+HeightYes, No, NoYes, Yes, Yes
          nativeAdResponse.getIcon();
nativeAdResponse.getIconSize();
          nativeAdResponse.getIconUrl();
TitleYesYes
          nativeAdResponse.getTitle();
Sponsored byYesYes
          nativeAdResponse.getSponsoredBy();
Body textYesYes
          nativeAdResponse.getDescription();
Desc2YesYes
          nativeAdResponse.getAdditionalDescription();
Call-to-actionYesYes
          nativeAdResponse.getCallToAction();
Rating, ScaleYes, YesYes, No
          nativeAdResponse.getAdStarRating();
LikesNoYes (json only)
if((nativeAdResponse.getNetworkIdentifier() == NativeAdResponse.Network.APPNEXUS) &&. (nativeAdResponse.getNativeElements().get(NativeAdResponse.NATIVE_ELEMENT_OBJECT)) instanceof JSONObject){
JSONObject nativeResponseJSON = (JSONObject) (nativeAdResponse.getNativeElements().get(NativeAdResponse.NATIVE_ELEMENT_OBJECT));
    String likes = JsonUtil.getJSONString(nativeResponseJSON,"likes");
String downloads = JsonUtil.getJSONString(nativeResponseJSON,"downloads");
String price = JsonUtil.getJSONString(nativeResponseJSON,"price");
String saleprice = JsonUtil.getJSONString(nativeResponseJSON,"saleprice");
String phone = JsonUtil.getJSONString(nativeResponseJSON,"phone");
String address = JsonUtil.getJSONString(nativeResponseJSON,"address");
String displayurl = JsonUtil.getJSONString(nativeResponseJSON,"displayurl");
}
DownloadsNoYes (json only)
PriceNoYes (json only)
Sale PriceNoYes (json only)
PhoneNoYes (json only)
AddressNoYes (json only)
Display URLNoYes (json only)
Privacy URLNoYes
          nativeAdResponse.getPrivacyLink();
VideoNoYes
          nativeAdResponse.getVastXml();
CustomYesNo 
ContextYesNo 
Full textYesNo 


Related Topics