Banners for Android

Initialization

To display 320x50, 300x250, 728x90 and adaptive 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.

Once instantiated, it needs to set your slotId.

The default is adaptive banner format. In this case, the banner is automatically stretched to the width of the screen while maintaining the proportions, and the height will be at least 50dip, but not more than 15% of the screen height. The system itself monitors screen resizing / flipping and resizes the banner accordingly.

It is possible to set the adSize property. In this case, the SDK will stop tracking changes in device orientation and resize the adaptive banner. In this case, when using adaptive banners, you need to track the changes yourself.
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);
     
    // Set slot id
    adView.setSlotId(YOUR_SLOT_ID);
 
    // optional: if not set, banner will be adaptive
    adView.setAdSize(AdSize.ADSIZE_320x50);
}

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);
     
    // Set slot id
    adView.setSlotId(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();
}

Set MyTargetView in XML

It is possible to set MyTargetView as part of XML layout.
<com.my.target.ads.MyTargetView
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/view_ad"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:myTarget_isRefreshAd="false"  // turning off banner rotation
    app:myTarget_adSize="banner_320x50" // set format, possible values: banner_320x50, banner_30x250, banner_728x90, adaptive
    app:myTarget_slotId="7250"> // slot number
</com.my.target.ads.MyTargetView>

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.setRefreshAd(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?
Yes
No