Help Center

Native Banners Android (available on request)

my Target SDK provides the ability to display ads in your application using your own visual components. The SDK downloads the data and provides the application with an ad model with certain properties for filling in the visual component, as well as methods for calculating impressions and processing clicks. The SDK also provides a ready-made, customizable 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 (videos, flashcards, large picture).

Initialization

To display native banners in your application, you need to create an instance of the NativeBannerAd class. To create an instance of NativeBannerAd, you must specify your slotId.

private NativeBannerAd ad;

private void initAd()
{
   // Enabling debugging mode
   // MyTargetManager.setDebugMode(true); 


   // Creating an instance of NativeBannerAd
   ad = new NativeBannerAd(YOUR_SLOT_ID, this);
}

Loading ads

To receive notifications (such as a successful download of data or a download error, a click on an advertisement), you need to create an instance of NativeBannerAdListener and set it as an event listener, after which you can start downloading data.

private NativeBannerAd ad;

private void initAd()
{
   // Creating an instance of NativeBannerAd
   ad = new NativeBannerAd(YOUR_SLOT_ID, this);
    
   // Installing the event listener
   ad.setListener(new NativeBannerAd.NativeBannerAdListener()
   {
       @Override
       public void onLoad(NativeBanner banner, NativeBannerAd ad)
       {
       }

       @Override
       public void onNoAd(IAdLoadingError adLoadingError, NativeBannerAd ad)
       {
       }

       @Override
       public void onClick(NativeBannerAd ad)
       {
       }
 
       @Override
       public void onShow(NativeBannerAd ad)
       {
       }
   });
        
   // Starting the data load
   ad.load();
}

Autoloading images

By default, all images of the advertising banner are loaded and cached. You can disable automatic image loading, but keep in mind that it will take additional time to download them, which will create an additional delay in displaying ads in your application.

ad.setCachePolicy(CachePolicy.NONE);
ad.load();

Acceptable values: cachePolicy.ALL (default), cachePolicy.IMAGE, cachePolicy.NONE.

If preloading is enabled, the corresponding images will be uploaded and stored in the cache in parallel with loading the main data of the advertising banner.

If preloading of images is disabled, they will be asynchronously and automatically loaded at the time of the call to the method registerView. No additional actions are required.

Successful asset loading notifications

It is possible to set a listener to receive notifications about the successful download of the icon image file in case of automatic download.

ad.setMediaListener(new NativeBannerAdMediaListener() {
           @Override
           public void onIconLoad(@NonNull NativeBannerAd ad)
           {
               // notification of successful icon loading
           }
       });
ad.load();  

Displaying ads

After successfully uploading 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. They vary for applications and websites.

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

Visual components (both standard and proprietary) should be placed inside the binder, an application object that implements the NativeBannerAdViewBinder interface.

After creating the binder, you must register it in the NativeAd instance using the registerView method. If you are going to use the same visual components to display other ads, you need to first call the unregisterView method on the current instance of NativeAd before calling the registrerview on another instance. It is also necessary to change all text or informational texts so that information from another advertisement is displayed. Impressions and clicks are processed automatically, and the application should not block or intercept user events on this visual component. The available properties are described below and examples of filling in visual components for various types of advertised objects are provided.

@Override
void onLoad(NativeBanner banner, NativeBannerAd ad)
{
   // Example of filling in a visual component
   Context context = YourActivity.this; // текущая активити

   // RelativeLayout взят для примера - this is a container view for a banner
   RelativeLayout adViewLayout = new RelativeLayout(context);

   TextView ageView = new TextView(context);
   // Age limit
   ageView.setText(banner.getAgeRestrictions());

   TextView advertisingLabel = new TextView(context);
   // The text of the "Ads" label
   advertisingLabel.setText(banner.getAdvertisingLabel());

   TextView titleView = new TextView(context);
   // The title of the ads
   titleView.setText(banner.getTitle());

   titleView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16.0f);
   titleView.setTypeface(null, Typeface.BOLD);
   titleView.setMaxLines(2);
   titleView.setEllipsize(TextUtils.TruncateAt.END);

   TextView domainView = new TextView(context);
   // Valid only if (banner.navigationType == NavigationType.WEB)
   domainView.setText(banner.getDomain());

   IconAdView iconView = new IconAdView(context);
   // IconAdView - this is a view from the SDK, it will display the icon inside this view itself.
   // Additional settings are not required

   TextView disclaiberLabel = new TextView(context);
   disclaiberLabel.setText(banner.getDisclaimerInfo().text);

   Button ctaButton = new Button(context);
   // The text of the action for the button
   ctaButton.setText(banner.getCtaText());

   //
   // Placing the views inside the RelativeLayout
   //

   RelativeLayout.LayoutParams adViewLp = new RelativeLayout.LayoutParams(MATCH_PARENT, WRAP_CONTENT);
   adViewLayout.setLayoutParams(adViewLp);

   ageView.setId(View.generateViewId());
   // ...

   RelativeLayout.LayoutParams ageViewLp = new RelativeLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT);
   ageViewLp.addRule(RelativeLayout.ALIGN_PARENT_TOP);
   ageViewLp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);

   ageView.setLayoutParams(ageViewLp);
   adViewLayout.addView(ageView);

   //
   // Here is the rest of the code for placing views inside the ad View Layout
   // ...

   // Creating a native banner binder
   // Note: not all views are created in this example.
   // This is done so as not to complicate the example.
   NativeBannerAdViewBinder binder = new NativeBannerAdViewBinder() {
       @NonNull @Override public View getAgeRestrictionView() { return ageView; }
       @NonNull @Override public View getAdvertisingView() { return advertisingLabel; }
       @NonNull @Override public IconAdView getIconView() { return iconView; }
       @Nullable @Override public View getTitleView() { return titleView; }
       @NonNull @Override public View getDomainView() { return domainView; }
       @Nullable @Override public View getCtaView() { return ctaButton; }
       @Nullable @Override public View getVotesView() { return null; }
       @Nullable @Override public View getStarsRatingView() { return null; }
       @NonNull @Override public View getDisclaimerView() { return disclaiberLabel; }
       @Nullable @Override public View getAdChoicesView() { return null; }
       @NonNull @Override public ViewGroup getRootAdBannerView() { return adViewLayout; }
   };

   // Registering a binder with visual components
   ad.registerView(binder);

   // Adding it to the screen.
   // mainLayout sets the size and location of the native banner on the application screen.
   mainLayout.addView(binder.getRootAdBannerView(), MATCH_PARENT, WRAP_CONTENT);
}

Native banner ad block example for site ads
Native banner ad block example for app ads
1 of 2
Native banner ad block example for site ads

AdChoices icon

The myTarget SDK automatically adds the 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 corner using the adChoicesPlacement property.:

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

Manual setting of AdChoices

If you need to set your own image for AdChoices, you need to use the NativeAdChoicesView class and transfer the image there using the setImageBitmap() or setImageDrawable() methods.

...
NativeAdChoicesView myAdChoicesView = createMyAdChoicesView(); // создание своей NativeAdChoicesView
myAdChoicesView.setImageBitmap(bitmap);
myAdChoicesView.setImageDrawable(drawable);
...

If you need to manually position AdChoicesView (setting gravity, margins) inside your View to display native ads, you must specify AdChoicesPlacement.MANUAL

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

Customizing the AdChoices button

The developer can draw the adchoice button himself as he needs, but in this case, the NativeBannerAd property must be set to AdChoicesPlacement.DRAWING_MANUAL. You also need to call the ad.handleAdChoicesClick(context) method to process clicks on attachments.

...
ad.setAdChoicesPlacement(AdChoicesPlacement.DRAWING_MANUAL);
ad.load();
...
customAdChoicesView.setOnClickListener(v->{
ad.handleAdChoicesClick(context)
})

To get the ads icon, you can access the ad object directly or use the NativeAdChoicesListener, which must be set before loading the ad.

//getting an AdChoises icon through a variable
ad.getBanner().getAdChoicesIcon();
...
//getting an AdChoises icon through the listener
ad.setAdChoicesListener(new NativeBannerAd.NativeBannerAdChoicesListener() {
   @Override
   public void onAdChoicesIconLoad(@Nullable ImageData imageData, boolean success, @NonNull NativeAd ad)
   {
       customAdChoicesView.setImageBitmap(imageData.getBitmap)
   }
});

Customizing the rendering of AdChoices options

To change the design of the rendering of access options, you must pass an object implementing the MenuFactory interface in the NativeBannerAd constructor, which in turn must return an object implementing the Menu interface.

MenuFactory menuFactory = new MenuFactory()
   {
       @Override
       @NonNull public Menu createMenu()
       {
           return new Menu()
           {
               @Override
               public void setListener(@Nullable Listener listener)
               {
                   // The listener who needs to call the action Click(menu Action) method when clicking on the UI element that was drawn with the corresponding menuAction header.
               }

               @Override
               public void addAction(@NonNull MenuAction menuAction)
               {
                   // This is an object that contains the title and the type of the add option. These objects must be used when drawing.
               }

               @Override
               public void present(@NonNull Context context)
               {
                   // This method is called when it is necessary to display the add options with those menuactions that were previously obtained from the addAction() method.
               }

               @Override
               public void dismiss()
               {
                   // This method is called when it is necessary to close the rendering of AdChoises options.
               }
           };
       }
   };
...
ad = new NativeBannerAd(YOUR_SLOT_ID, menuFactory, this);
ad.load();

Managing ad closures

In order to receive notifications about ad closures and manage ad closures, you must pass an object that implements the NativeBannerAdChoicesOptionListener interface. The interface contains several methods.

  • boolean shouldCloseAutomatically() – tells the SDK whether to close ads automatically. If the method returns true, the SDK will automatically hide the ad. If false, then the opportunity to hide is provided to the developer;
  • onClose Automatically() notifies the developer that the advertisement was hidden automatically using the SDK. Thus, this method will be called only if shouldCloseAutomatically returns true;
  • closeIfAutomaticallyDisabled() notifies the developer that he needs to hide the ad himself. Thus, this method will be called only if shouldCloseAutomatically returns false.

NativeBannerAd.NativeBannerAdChoicesOptionListener adChoicesOptionListener = new NativeBannerAd.NativeBannerAdChoicesOptionListener()
{
   @Override
   public boolean shouldCloseAutomatically()
   {
       return false;
   }

   @Override
   public void onCloseAutomatically(@NonNull NativeBannerAd ad)
   {

   }

   @Override
   public void closeIfAutomaticallyDisabled(@NonNull NativeBannerAd ad)
   {

   }
};
        
ad.setAdChoicesOptionListener(adChoicesOptionListener);

Setting clickable area

In the example above, the visual component is registered using the registerView(NativeBannerAdViewBinder binder) method. In this case, the entire area of ​​the visual component is clickable. The MyTarget SDK provides the ability to specify specific visual components whose clicks will be tracked. To do this, use the registerView(NativeBannerAdViewBinder binder, List<View> clickableViews) method:

@Override
void onLoad(NativeBanner banner, NativeBannerAd ad)
{
   ...
   ...
   ...

   // Creating an array of clickable visual components, a title, and a call-to-action button
   List<View> clickableViews = List.of(titleView, ctaButton);

   // We register a binder with views, with a clickable title and a call-to-action button
   ad.registerView(binder, clickableViews);

   // Adding it to the screen
   // mainLayout sets the size and location of the native banner on the application screen.
   mainLayout.addView(binder.getRootAdBannerView(), MATCH_PARENT, WRAP_CONTENT);
}

Using a built-in visual component

My Target SDK provides a ready-made, customizable visual component, the appearance of which you can customize to match the design of your application.

The examples below describe the properties available for configuration.

NativeBannerAdView

@Override
void onLoad(NativeBanner banner, NativeBannerAd ad)
{
   // Creating and initializing the visual component
   NativeBannerAdView nativeBannerAdView = NativeViewsFactory.getNativeBannerAdView(YourActivity.this);
   nativeBannerAdView.setupView(banner);

   // Configurable internal visual components
   TextView advLabelView = nativeBannerAdView.getAdvertisingTextView();
   TextView ageRestrictionView = nativeBannerAdView.getAgeRestrictionTextView();
   TextView disclaimerView = nativeBannerAdView.getDisclaimerTextView();
   TextView titleView = nativeBannerAdView.getTitleTextView();
   View ctaBtn = nativeBannerAdView.getCtaButtonView();
   TextView votesView = nativeBannerAdView.getVotesTextView();
   StarsRatingView starsRatingView  = nativeBannerAdView.getStarsRatingView();
   TextView domainView = nativeBannerAdView.getDomainTextView();
   IconAdView iconView = nativeBannerAdView.getIconView();

   // Getting a binder
   NativeBannerAdViewBinder binder = nativeBannerAdView.getNativeBannerAdViewBinder();

   // Registering the visual component
   ad.registerView(binder);

   // Adding it to the screen.
   // mainLayout sets the size and location of the native banner on the application screen.
   mainLayout.addView(binder.getRootAdBannerView(), MATCH_PARENT, WRAP_CONTENT);
}

Native banner ad block example for site ads
Native banner ad block example for app ads
1 of 2
Native banner ad block example for site ads

Loading of multiple banners

My Target SDK provides the NativeBannerAdLoader class, which allows you to load from 1 to 20 banners in one request. For NativeBannerAdLoader, you can configure all the same parameters that are available for configuration in NativeBannerAd (for example, user's gender and age parameters, auto-upload of images and videos). The 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 you want to receive.

// Enabling debugging mode
// MyTargetManager.setDebugMode(true); 

// Enabling debugging mode using NativeBannerAdLoader
NativeBannerAdLoader nativeBannerAdLoader = NativeBannerAdLoader.newLoader(YOUR_SLOT_ID, COUNT, this);

// We install the OnLoad callback and load the banners
nativeBannerAdLoader.setOnLoad(new OnLoad()
{
  @Override
  public void onLoad(@NonNull List<NativeBannerAd> ads)
  {
     for (NativeBannerAd ad : ads)
     {
       // Installing the event listener
       ad.setListener(nativeBannerAdListener);
        
       NativeBanner banner = ad.getBanner();
       // Кcode similar to the onLoad method of the interface NativeAdListener
     }
  }
}).load();

The resulting array will contain from 0 to COUNT NativeBannerAd objects. Each of them contains an already loaded NativeBanner, and each of them must be handled in the same way as described in this documentation above, starting with calling the onLoad method.