Native Ads for Android

The myTarget SDK provides the ability to display ads in your app using native visual components. The SDK loads the ad and gives the app an ad model with specific properties and methods for counting impressions and handing clicks. The API also provides build-in visual component that you can use in your app, instead of creating your own.

Initialization

To display native ads in your app, you must create an instance of the NativeAd class and specify your slotId.
private NativeAd ad;
 
private void initAd()
{
    // Enabling debug mode
    // NativeAd.setDebugMode(true);
 
 
    // Create an instance of NativeAd
    ad = new NativeAd(YOUR_SLOT_ID, this);
}

Loading ads

To receive notifications (such as ad load succeeded, ad load failed, or ad clicked), you must create an instance of NativeAdListener and set it as an event listener. Then you can load the ad.
private NativeAd ad;
 
private void initAd()
{
    // Create an instance of NativeAd
    ad = new NativeAd(YOUR_SLOT_ID, this);
     
    // Set the event listener
    ad.setListener(new NativeAd.NativeAdListener()
    {
        @Override
        public void onLoad(NativePromoBanner banner, NativeAd ad)
        {
        }
 
        @Override
        public void onNoAd(String reason, NativeAd ad)
        {
        }
 
        @Override
        public void onClick(NativeAd ad)
        {
        }
  
        @Override
        public void onShow(NativeAd ad)
        {
        }
 
 
        @Override
        public void onVideoPlay(NativeAd ad)
        {
        }
 
 
        @Override
        public void onVideoPause(NativeAd ad)
        {
        }
 
 
        @Override
        public void onVideoComplete(NativeAd ad)
        {
        }
    });
         
    // Start loading ad
    ad.load();
}

Autoloading images and video

By default, images and videos are preloaded. You can turn off the automatic loading of images and videos, but keep in mind that downloading them will take extra time, which will create an additional delay in displaying ads in your application.

ad.setCachePolicy(CachePolicy.NONE);
ad.load();   
Valid values are CachePolicy.ALL (default), CachePolicy.IMAGE, CachePolicy.VIDEO, CachePolicy.NONE.

If preloading is enabled, then the corresponding images will be loaded and cached in parallel with the loading of the main data of the advertising banner.

If the preloading of images is turned off, they will be asynchronously and automatically loaded when the registerView method is called. No additional action required.

Displaying ads

After successfully loading the ad, you can use the properties of the banner object to initialize your visual component. Which properties are available depends on what is being advertised, i.e. they differ for apps and websites. To display main image, carousel and video playback, you must use an instance of MediaAdView class from myTarget SDK. Visual component should be added to com.my.target.nativeads.views.NativeAdContainer.

Visual component's elements should have id's provided with myTarget SDK

Visual component's element
id
Root View
R.id.nativeads_ad_view
Advertising Label
R.id.nativeads_advertising
Title
R.id.nativeads_title
Description
R.id.nativeads_description
Rating
R.id.nativeads_rating
Domain
R.id.nativeads_domain
Age Restrictions
R.id.nativeads_age_restrictions
Disclaimer
R.id.nativeads_disclaimer
Call-to-Action
R.id.nativeads_call_to_action
Icon
R.id.nativeads_icon
MediaAdView
R.id.nativeads_media_view
After initializing a visual component, use the registerView method to register it with the NativeAd instance. If you are going to use the same visual component to display other ads, you must first call the unregisterView method on the current NativeAd instance before calling registerView on another NativeAd instance. Impressions and clicks are processed automatically, and the application should not block or capture user touch events on this visual component. The properties available are described below, and examples of initializing visual component.
@Override
public void onLoad(NativePromoBanner banner, NativeAd ad)
{  
    // Ad title
    String title = banner.getTitle();
    // Main text
    String description = banner.getDescription();
    // Age limit. May be null
    String ageRestrictions = banner.getAgeRestrictions();
    // Disclaimer. May be null
    String disclaimer = banner.getDisclaimer();
    // "Advertising" label text
    String advertisingLabel = banner.getAdvertisingLabel();
    // Icon
    ImageData icon = banner.getIcon();
    // Call-to-action text for the button
    String ctaText = banner.getCtaText();
    // Properties available only for ads promoting apps
    if (banner.getNavigationType().equals(NavigationType.STORE))
    {
        // App rating (0-5)
        float rating = banner.getRating();
        // Number of votes
        int votes = banner.getVotes();
        // App category
        String category = banner.getCategory();
        // App subcategory
        String subcategory = banner.getSubcategory();
    }
    // Properties available only for ads promoting websites
    else if (banner.getNavigationType().equals(NavigationType.WEB))
    {
        // Website domain
        String domain = banner.getDomain();
    }
     
    // Use properties to build your visual component
 
    Context context = YourActivity.this;
    LinearLayout adViewLayout = new LinearLayout(context);
    adViewLayout.setId(R.id.nativeads_ad_view);
    TextView titleView = new TextView(context);
    titleView.setId(R.id.nativeads_title);
    titleView.setText(title);
    adViewLayout.addView(titleView);
    TextView descriptionView = new TextView(context);
    descriptionView.setId(R.id.nativeads_description);
    titleView.setText(description);
    adViewLayout.addView(descriptionView);
    Button btn = new Button(context);
    btn.setId(R.id.nativeads_call_to_action);
    btn.setText(ctaText);
    adViewLayout.addView(btn);
     
    // Create an instance of MediaAdView
    MediaAdView mediaView = NativeViewsFactory.getMediaAdView(context);
    mediaView.setId(R.id.nativeads_media_view);
    // Create an instance of IconAdView
    IconAdView iconView = new IconAdView(context);
    mediaView.setId(R.id.nativeads_icon);
 
    // If you have enabled autoloading of images, you can assign the loaded icon here: iconView.setImageBitmap(image.getBitmap());
     
    adViewLayout.addView(mediaView);
    adViewLayout.addView(iconView);
 
 
    // Create container
    NativeAdContainer nativeAdContainer = new NativeAdContainer(context);
    // Add view to container
    nativeAdContainer.addView(adViewLayout);
         
    // Register the view
    ad.registerView(adViewLayout);
  
    // Add it to the layout
    mainLayout.addView(nativeAdContainer, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
}
App:
Web-site:

AdChoices icon

myTarget SDK automatically adds the AdChoices icon to each visual component. The AdChoices icon is added to the top right corner by default. You can select the preferred corner using the setAdChoicesPlacement(int placement) method:
...
ad.setAdChoicesPlacement(AdChoicesPlacement.TOP_RIGHT);
ad.load(); 

Setting clickable area

In the example above, the visual component is registered using the registerView(View view) method. In this case, the entire area of the visual component is clickable. MyTarget SDK provides the ability to register list of visual components that can be clicked. To do this, use the registerView(View view, List <View> clickableViews) method:
@Override
public void onLoad(NativePromoBanner banner, NativeAd ad)
{
    ...
    ...
    ...
     
    // Create an array of clickable visual components, titleView and cta button
    ArrayList<View> clickableViews = new ArrayList<>();
    clickableViews.add(titleView);
    clickableViews.add(btn);
    // Register the view with clickable titleView and button
    ad.registerView(adViewLayout, clickableViews);
  
    // Add it to the layout
    mainLayout.addView(adViewLayout, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
}

Using built-in visual component

MyTarget SDK provides a built-in custom visual component, the appearance of which you can customize to the design of your application.

The following are examples and the properties that are available for customization.

We give some examples below and describe the settings available.
NativeAdView
public void onLoad(NativePromoBanner banner, NativeAd ad)
{
    // Create visual component
    NativeAdView nativeAdView = NativeViewsFactory.getNativeAdView(ad, YourActivity.this);
     
    // Available inner components
    TextView advLabelView = nativeAdView.getAdvertisingTextView();
    TextView ageRestrictionView = nativeAdView.getAgeRestrictionTextView();
    TextView disclaimerView = nativeAdView.getDisclaimerTextView();
    TextView titleView = nativeAdView.getTitleTextView();
    TextView descriptionView = nativeAdView.getDescriptionTextView();  
    Button ctaBtn = nativeAdView.getCtaButtonView();
    TextView votesView = nativeAdView.getVotesTextView();
    StarsRatingView starsRatingView = nativeAdView.getStarsRatingView();
    TextView domainView = nativeAdView.getDomainOrCategoryTextView();
    MediaAdView mediaView = nativeAdView.getMediaAdView();
    IconAdView iconView = nativeAdView.getIconImageView();
 
    // Create container for creative
    NativeAdContainer nativeAdContainer = new NativeAdContainer(context);
    // Add ad creative to container
    nativeAdContainer.addView(nativeAdView);
     
    // Register visual component
    ad.registerView(nativeAdContainer);
     
    // Add to screen
    mainLayout.addView(nativeAdContainer, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
}
Application:
Web resource:
Video:
Cards:

Loading multiple banners

The myTarget SDK provides the class NativeAdLoader, which allows you to load from 1 to 20 banners per one request. For NativeAdLoader, you can configure all the same settings that are available for customization in NativeAd (for example, user's gender and age, autoloading images and videos). There is no guarantee that the number of banners specified in the COUNT parameter will be loaded - this parameter indicates the maximum number of banners that you want to receive.
// Enabling debug mode
// NativeAdLoader.setDebugMode(true);  
 
 
// Create an instance of NativeAdLoader
NativeAdLoader nativeAdLoader = NativeAdLoader.newLoader(YOUR_SLOT_ID, COUNT, this);
 
// Set the OnLoad callback and loading ads
nativeAdLoader.setOnLoad(new OnLoad()
{
   @Override
   public void onLoad(@NonNull List<NativeAd> ads)
   {
      for (NativeAd ad : ads)
      {
        // Set the event listener
        ad.setListener(nativeAdListener);
         
        NativePromoBanner promoBanner = ad.getBanner();
        // The same code as in onLoad method of NativeAdListener interface
      }
   }
}).load();
The resulting array will contain from 0 to COUNT instances of NativeAd - each of them contains already loaded NativePromoBanner and with each of them it is necessary to work the same as described in this documentation above, starting from the onLoad method.

Mediation with other advertising SDKs

Mediation with the Mopub SDK is supported for native ads. Mediation is configured using the Mopub interface.

Mopub mediation

Mediation is configured at http://www.mopub.com/

To create a mediation network, go to the Networks section and add a Custom Native Network, specifying the following parameters:

Field
Value
CUSTOM EVENT CLASS
com.mopub.nativeads.MyTargetCustomEventNative
CUSTOM EVENT CLASS DATA
{"slotId":"<your slot id>"}
Was this article helpful?
Да
Нет