Native Banners for Android

myTarget SDK provides the ability to display ads in your application using its own visual components. The SDK downloads data and provides the application with an ad model with specific properties to populate the visual component, as well as methods for counting impressions and processing clicks. The SDK also provides a built-in custom visual component that you can use in your application, instead of creating your own.

The native banner does not support MediaAdView and therefore cannot display media content (video, cards, large image).

Initialization

To display native banners in your application, you must create an instance of the NativeBannerAd class. To create an instance of NativeBannerAd, you must specify your slotId.
private NativeBannerAd ad;
 
private void initAd()
{
    // Turn on debug mode
    // NativeBannerAd.setDebugMode(true);  
 
 
    // Creating instance of NativeBannerAd
    ad = new NativeBannerAd(YOUR_SLOT_ID, this);
}

Loading ads

To receive notifications (such as a successful data download or a download error, click on an advertisement), you must create an instance of NativeBannerAdListener and set it as an event listener, after which you can start the data download.
private NativeBannerAd ad;
 
private void initAd()
{
    // Creating instance of NativeBannerAd
    ad = new NativeBannerAd(YOUR_SLOT_ID, this);
     
    // add listener
    ad.setListener(new NativeBannerAd.NativeBannerAdListener()
    {
        @Override
        public void onLoad(NativeBanner banner, NativeBannerAd ad)
        {
        }
 
        @Override
        public void onNoAd(String reason, NativeBannerAd ad)
        {
        }
 
        @Override
        public void onClick(NativeBannerAd ad)
        {
        }
  
        @Override
        public void onShow(NativeBannerAd ad)
        {
        }
    });
         
    // Start loading data
    ad.load();
}

Autoloading images

By default, all banner images are loaded and cached. You can turn off the automatic loading of images, but keep in mind that they will take extra time to load, which will create an additional delay in displaying ads in your application.
ad.setCachePolicy(CachePolicy.NONE);
ad.load();
Valid values are CachePolicy.ALL (by default), CachePolicy.IMAGE, 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 data, you can use the properties of the resulting banner instance to populate your visual component. The availability of properties depends on the type of advertised object - for applications and sites they differ.

To display the icon, use the IconAdView provided by the SDK.

Visual components (both standard and proprietary) should be placed inside the NativeAdContainer container.
At the same time, both the component itself and the container can be passed to the registerView method.
When creating the container, the visual component adView will be added as a subview to the container and stretched to fit the container.

Elements of the visual component must be assigned the corresponding id supplied with the SDK

Ad View Element
id
Root View (your AdView)
R.id.nativeads_ad_view
Advertising Label
R.id.nativeads_advertising
Title
R.id.nativeads_title
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
After filling in the visual component, you must register it with the NativeAd instance using the registerView method. If you intend to use the same visual component to display other advertisements, you must first call the unregisterView method on the current NativeAd instance before calling registerView on the other instance. Processing impressions and clicks is carried out automatically, while the application should not block or intercept user events on this visual component. Available properties are described below and examples of filling visual components for various types of advertised objects are given.
@Override
public void onLoad(NativeBanner banner, NativeBannerAd ad)
{
    // Ad Title
String title = banner.getTitle ();
// Age limit. May be null
String ageRestrictions = banner.getAgeRestrictions ();
// Disclaimer. May be null
String disclaimer = banner.getDisclaimer ();
// The text of the label "Advertising"
String advertisingLabel = banner.getAdvertisingLabel ();
 
 
// Icon
ImageData icon = banner.getIcon ();
// The action text for the button
String ctaText = banner.getCtaText ();
// Properties Available Only For Ads Promoting Applications
if (banner.getNavigationType (). equals (NavigationType.STORE))
{
// Application Rating (0-5)
float rating = banner.getRating ();
// Number of ratings
int votes = banner.getVotes ();
}
// Properties Available Only For Ads Promoting Websites
else if (banner.getNavigationType (). equals (NavigationType.WEB))
{
// site domain
String domain = banner.getDomain ();
}
 
// An example of filling a 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);
Button btn = new Button (context);
btn.setId (R.id.nativeads_call_to_action);
btn.setText (ctaText);
adViewLayout.addView (btn);
 
IconAdView iconView = new IconAdView(context);
mediaView.setId (R.id.nativeads_icon);
 
adViewLayout.addView (iconView);
 
 
// Create a creative container
NativeAdContainer nativeAdContainer = new NativeAdContainer (context);
// Add ad creative to container
nativeAdContainer.addView (adViewLayout);
 
// Register the visual component
ad.registerView (adViewLayout);
 
// Add to the screen
mainLayout.addView (nativeAdContainer, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
}
App:
Web resource:

AdChoices icon

myTarget SDK automatically adds an AdChoices icon to each visual component. By default, the icon is added to the upper right corner of the visual component, but you can select your preferred angle using the adChoicesPlacement property:

...
ad.setAdChoicesPlacement(AdChoicesPlacement.TOP_RIGHT);
ad.load(); 

Setting clickable area

In the above example, the visual component is registered using the registerView (View view) method. In this case, the entire area of the visual component is clickable. The MyTarget SDK provides the ability to specify specific visual components that clicks will be tracked. To do this, use the registerView method (View view, List <View> clickableViews):
@Override
public void onLoad(NativeBanner banner, NativeBannerAd ad)
{
    ...
    ...
    ...
     
    // Create an array of clickable visual components, a title and a call to action button
    ArrayList<View> clickableViews = new ArrayList<>();
    clickableViews.add(titleView);
    clickableViews.add(btn);
    // Register the visual component, with a clickable title and a call to action button
    ad.registerView(adViewLayout, clickableViews);
  
    // Add to the screen
    mainLayout.addView(adViewLayout, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
}

Using a 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.

NativeBannerAdView
public void onLoad(NativeBanner banner, NativeBannerAd ad)
{
    // Create a visual component
NativeBannerAdView nativeBannerAdView = NativeViewsFactory.getNativeBannerAdView (YourActivity.this);
nativeBannerAdView.setupView(banner);
 
// Customizable internal visual components
TextView advLabelView = nativeBannerAdView.getAdvertisingTextView ();
TextView ageRestrictionView = nativeBannerAdView.getAgeRestrictionTextView ();
TextView disclaimerView = nativeBannerAdView.getDisclaimerTextView ();
TextView titleView = nativeBannerAdView.getTitleTextView ();
Button ctaBtn = nativeBannerAdView.getCtaButtonView ();
TextView votesView = nativeBannerAdView.getVotesTextView ();
StarsRatingView starsRatingView = nativeBannerAdView.getStarsRatingView ();
TextView domainView = nativeBannerAdView.getDomainOrCategoryTextView ();
IconAdView iconView = nativeBannerAdView.getIconImageView ();
 
// Create a creative container
NativeAdContainer nativeAdContainer = new NativeAdContainer (context);
// Add ad creative to container
nativeAdContainer.addView (nativeBannerAdView);
 
// Register the visual component
ad.registerView (nativeAdContainer);
 
// Add to the screen
mainLayout.addView (nativeAdContainer, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
}
Application:
Web resource:

Loading of multiple banners

MyTarget SDK provides the NativeBannerAdLoader class, which makes it possible to load from 1 to 20 banners in one request. For NativeBannerAdLoader, you can configure all the same parameters that are available for setting in NativeBannerAd (for example, gender and age parameters of the user, autoload images and videos). myTarget SDK does not 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.
// Turn on debug mode
// NativeBannerAdLoader.setDebugMode(true);
 
// Creating NativeBannerAdLoader
NativeBannerAdLoader nativeBannerAdLoader = NativeBannerAdLoader.newLoader(YOUR_SLOT_ID, COUNT, this);
 
// Install OnLoad callback and load banners
nativeBannerAdLoader.setOnLoad(new OnLoad()
{
   @Override
   public void onLoad(@NonNull List<NativeBannerAd> ads)
   {
      for (NativeBannerAd ad : ads)
      {
        // add listener
        ad.setListener(nativeBannerAdListener);
         
        NativeBanner banner = ad.getBanner();
      }
   }
}).load();
The resulting array will contain from 0 to COUNT NativeBannerAd objects - each of them contains an already loaded NativeBanner and you need to work with each of them in the same way as described in this documentation above, starting with calling the onLoad method.
Was this article helpful?
Да
Нет