Banners for Android

Initialization

To display 320x50, 300x250 and 728x90 banners in your app, you must create an instance of the MyTargetView class. MyTargetView is a visual component (subclass of ViewGroup) that should be added to the app's screen.

MyTargetView sizes must be specified in device-independent pixels (dp = px * density), i.e. to display a 320x50 advertisement on a device with a density of 1.5, specify a size of 480x75 or more. After the instance is created, it must be initialized with your slotId. To specify the format, you must set one of AdSize values. The default format is 320x50.
private MyTargetView adView;
 
@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_layout);
 
 
    // Enabling debug mode
    // MyTargetView.setDebugMode(true);
 
    // Create an instance of MyTargetView
    adView = new MyTargetView(this);
     
    // Initialize the instance, 320x50 format
    adView.init(YOUR_SLOT_ID);
 
    // Initialize the instance, 300x250 format
    // adView.init(YOUR_SLOT_ID, AdSize.BANNER_300x250);
}

Loading and displaying ads

The newly created and initialized MyTargetView instance must be added to the app's screen. To receive notifications (such as ad load succeeded, ad load failed, or ad clicked), you must create an instance of MyTargetView.MyTargetViewListener, set it as an event listener, and then start loading ad. After ad has loaded successfully, you can start displaying ad.
private MyTargetView adView;
 
@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_layout);
 
 
    RelativeLayout layout = (RelativeLayout) findViewById(R.id.activityLayout);
 
    // Create an instance of MyTargetView
    adView = new MyTargetView(this);
     
    // Initialize the instance
    adView.init(YOUR_SLOT_ID);
 
    // Set the LayoutParams
    adViewLayoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    adViewLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
    adView.setLayoutParams(adViewLayoutParams);
     
    // Set the event listener
    adView.setListener(new MyTargetView.MyTargetViewListener()
    {
        @Override
        public void onLoad(MyTargetView myTargetView)
        {
            // The ad is successfully loaded. Start displaying ad
            layout.addView(adView);
        }
 
        @Override
        public void onNoAd(String reason, MyTargetView myTargetView)
        {  
        }
 
        @Override
        public void onShow(MyTargetView myTargetView)
        {
        }
 
        @Override
        public void onClick(MyTargetView myTargetView)
        {
        }
    });
     
    // Start loading ad
    adView.load();
}
     
@Override
protected void onDestroy()
{
    if (adView != null) adView.destroy();
    super.onDestroy();
}

Rotation

Ads are rotated every 60 seconds. You can disable automatic ad rotation by specifying the optional isRefreshAd parameter when initializing the instance. Rotation is only available for 320x50 and 728x90 formats.
// Disable automatic ad rotation
adView.init(YOUR_SLOT_ID, null, false);
Banner 320x50

Mediation with other advertising SDKs

Mediation with Admob SDK or Mopub SDK is supported for 320х50 banners. Mediation is configured using the corresponding interfaces. Automatic ad rotation is disabled during mediation.

Admob mediation

Mediation is configured at https://apps.admob.com/#monetize/

To create a mediation network, go to the app section and select "Edit mediation" on the current 320x50 adunit (or create it if it doesn't exist). In mediation edit mode, add a custom event (New ad network > Custom event) by specifying the following parameters:
Field
Value
Class Name
com.my.target.ads.mediation.MyTargetAdmobCustomEventBanner
Parameter
{"slotId":"<your slot id>"}

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.my.target.ads.mediation.MyTargetMopubCustomEventBanner
CUSTOM EVENT CLASS DATA
{"slotId":"<your slot id>"}

Set AdSize

myTarget SDK supported 320х50, 300х250 and 728х90 formats defined in MyTargetView.AdSize interface:
moPubView = new MoPubView(this);
         
Map<String, Object> localExtras = new HashMap<>();
...
localExtras.put("mytarget_adsize", MyTargetView.AdSize.BANNER_320x50);
 
moPubView.setLocalExtras(localExtras);
Was this article helpful?
Да
Нет