Skip to main content

Setting up a Banner Ad Placement - View based

The SDK supports Banner Ads using the CIBannerView view and the setPlacement API. Below is an example of how to set up a fixed CIBannerView in an XML layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:fitsSystemWindows="false">

    <com.contentignite.mobilesdk.ui.CIBannerView
        android:id="@+id/banner"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:paddingHorizontal="16dp"/>

</RelativeLayout>
The setPlacement API has the following parameters:

Required

  • placementID: String: Unique ID for this Ad Placement provided by Content Ignite.
  • pageUrl: String?: This is used for targeting for example if the ad is displayed inline within an article or section of the app which has an equivalent URL on the web.

Optional

  • eventListener: CIBannerEventListener: Interface which enables you to respond to events related to the banner ad.
  • targeting: Map<String, List<String>>: Pass custom key-value pairs to ad requests.
  • publisher: (() -> View)?: This function parameter is used to return a fallback view, It allows you to fall back to your existing Ad implementation for this particular placement. This gives publishers full control over the use of the SDK and the rate at which this fallback is used can be controlled directly through the Fusion platform.
import com.contentignite.mobilesdk.ui.CIBannerView

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_main)
        
        val banner = findViewById<CIBannerView>(R.id.banner)

        banner.setPlacement(
            placementID = "<PLACEMENT-ID>",
            eventListener = eventListener,
            targeting = mapOf("segment" to listOf("health", "fitness")),
            pageUrl = "https://publisher.com/sport",
            publisher = {
            val imageView = ImageView(application)
            imageView.setLayoutParams(
                LayoutParams(
                    LayoutParams.MATCH_PARENT,
                    LayoutParams.WRAP_CONTENT
                )
            )
            imageView.adjustViewBounds = true
            imageView.setImageResource(R.drawable.fallback)
            imageView
        })
    }
}

Handling ad events

To listen and respond to events from an Interstitial ad, use the CIBannerEventListener protocol:
private val eventListener = object : CIBannerEventListener {
    override fun bannerLoaded() {
        println("Banner ad loaded.")
    }

    override fun bannerLoadFailed(error: CIBannerLoadError) {
        println("Banner ad failed to load: $error")
    }

    override fun bannerClicked() {
        println("Banner ad clicked.")
    }

    override fun bannerImpression() {
        println("Banner ad impression.")
    }
}