Display Ads
Display ads can be added to your application in two ways. You can either add it programatically to your code and handle the view hierarchies accordingly, or you can add it through storyboards (iOS) or XML (Android) without writing any code.
Creating display ads programmatically
To create a display ad programmatically, simply create a SomAdContainer
, specify the ad type and add it to your view:
val bannerAd = SomAdContainer(AdType.banner)
let bannerAd = SomAdContainer(ofType: .banner)
Creating display ads through XML or Storyboard
If you prefer to add the display ads through UI, you can do the following:
Android
To add an ad through XML on Android, add a SomAdContainer
to your view and specify the adType:
<com.sevenonemedia.sevenoneads.containers.SomAdContainer
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:adType="@string/adType_banner"
/>
iOS
To add an ad through the Storyboard on iOS, you need to do the following:
- Drag a
UIView
representing the ad into your application's view in the storyboard. Set the class of thisUIView
toSomAdContainer
. Make sure you connect the outlet of your view to your corresponding view controller and handle the constraints programatically as you like. - In the storyboard, under the UIView’s
User Defined Runtime Attributes
, add an entry with the Key PathstoryboardAdType
of typeString
, and set its value to the ad type you prefer. e.g.rectangle
,banner
, etc. Note that only single slot ads are supported. If no value is set, the default valuebanner
will be used.
Note The ads are automatically requested and loaded when the container is within the boundary of the user's screen. Ads can be added to the view at any point of the lifetime of the application. If SevenOneAds is not done initialising, the ads will automatically wait for initialisation to complete. There exist dedicated ad sizes for tablets:
tabletBanner
,tabletBillboard
. It is up to the developer to ensure that the correct AdType is used for mobile and tablets devices.
Supported Display Ad Types
banner
: 320x50performanceBanner
: 320x50sponsor
: 320x50rectangle
: 300x250billboard
: 320x150tabletBanner
: 728x90tabletBillboard
: 728x250
Android: Recycle-able views
Important Note:If your Android application is using recycle-able views such as RecycleView, please read this section carefully, otherwise your application may be marked as fraudulent! Recycle-able views allow views to be recycled/re-used. This can cause issues if this view holds an advertisement because the reused view can continue to trigger new ad requests in the background leading to multiple ad requests even though only a single ad is visible.
To avoid this happening, inside the function onBindViewHolder()
in your custom RecyleView.Adapter class (see here on how to create an custom adapter), any cell that contains an advertisement must disable recycling for that single view by calling: viewHolder.setIsRecyclable(false)
.
override fun onBindViewHolder(viewHolder: MyViewHolder, position: Int) {
// Get the custom cell
val myCustomCell: CustomCell = dataset[position]
// Given the cell knows if it should hold an ad or content
// Check if the cell is ad or content
if (myCustomCell.showAd) {
// Define the AdViewHolder
val adViewHolder: AdViewHolder = viewHolder as AdViewHolder
// IMPORTANT: Set recycleable to false
adViewHolder.setIsRecyclable(false)
// Add ad to view
adViewHolder.layout.addView(SomAdContainer(AdType.banner))
} else {
// Create a viewholder and add content to view, no need to set isRecycleable
}
}
Memory management
To optimize the memory management of your app and to prevent possible memory leaks from happening, you will have to implement the SomAdContainer's destroy()
method into your app in the appropriate places:
class ExampleActivity : AppCompatActivity() {
private var myAd: SomAdContainer? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
...
myAd = SomAdContainer(AdType.banner)
val myAdContainer: FrameLayout = findViewById(R.id.my_ad_container)
myAdContainer.addView(myAd)
}
...
override fun onDestroy() {
myAd?.destroy()
super.onDestroy()
}
}
class ExampleViewController: UIViewController {
var myAd: SomAdContainer? = nil
...
override func viewDidLoad() {
super.viewDidLoad()
myAd = SomAdContainer(ofType: AdType.banner)
if let myAd = myAd {
myAd.heightAnchor.constraint(equalToConstant: 50).isActive = true
myAd.center.x = self.view.center.x
self.view.addSubview(myAd)
}
}
override func viewWillDisappear(_ animated: Bool) {
myAd?.destroy()
super.viewWillDisappear(animated)
}
}
Note: Depending on your implementation, you might need to add the destroy method in other locations as well. This is especially the case for Recycler Views and View Pagers on Android and UICollectionViews on iOS.
Android: Additional Lifecycle Methods
On Android, it is highly recommended to implement the pause()
and resume()
methods as well:
class ExampleActivity : AppCompatActivity() {
...
override fun onPause() {
myAd.pause()
super.onPause()
}
override fun onResume() {
super.onResume()
myAd.resume()
}
}
Note: Adding these methods guarantees that background tasks for loading and reloading ads are paused and resumed correctly according to their parent's lifecycle events.
Multislot Ads
SevenOneAds allows you to create a multislot ad
by specifying multiple ad types which may be eligible to your ad view. This way, the ad may be any of the specified ad types and sizes.
To create a multislot ad, simply create a SomAdContainer
, pass in a list with desired ad types and add it to your view:
val multiAd = SomAdContainer(arrayListOf(AdType.rectangle, AdType.banner))
let multiAd = SomAdContainer(adTypes: [AdType.rectangle, AdType.banner])
Note Your layout should be able to automatically adapt to the different ad sizes by responsively matching the ad view's dimensions to the received ad's size.
Ad Reloading
To increase revenue, ads can be reloaded after a minimum time of 30 seconds after the ad rendered on the screen. If configured, this will happen automatically in the background.
Ad Events
It is also possible to add live information to a display ad to improve targeting. This could be for example information about a live football match or weather information. To add an AdEvent to an ad such as goal
or rain
use:
ad.addAdEvent(“goal”)
ad.addAdEvent(adEvent: "goal")
The ad event is only added to the request if it's called before the ad is rendered. If the ad event is not configured it will not be added the request.
Note: Ad events must be agreed apon beforehand and set up by SOM, otherwise they will be ignored. To enable certain ad events, please contact your publisher manager at SOM.
Ad Exclusions
If you want to prevent certain categories of ads from appearing on your content (e.g. alcohol
, betting
etc.), you can contact SOM and specify which categories you wish to exclude, and SOM will handle it for you.