Advanced Banner API
Warning
This is the documentation for the Advanced API. Most developers should use our Simplified API, which comes with several built-in features such as:
-
automatic ad loading
-
retries for failed requests
See the documentation for the Simplified API here:
Banner ads occupy a small portion of the user interface, usually at the bottom or the top of the screen. To show one in your app, you access its view
property and add it to the current view hierarchy.
Create a Banner instance
When creating a banner, in addition to your placementId, you have to provide:
size
: Size of the banner. See Size for the available sizes.
Register ad callbacks
banner.listener = object : Banner.Listener {
override fun onPrebiddingFinished(result: PrebiddingResults) {
Log.d("Banner", "Banner client-side bidding finished!")
}
override fun onLoaded(loadResult: LoadResult) {
Log.d("Banner", "Banner loaded!")
}
override fun onFailedToLoad(loadError: LoadError, loadResult: LoadResult?) {
Log.d("Banner", "Banner failed to load. Reason: ${loadError.message}")
}
// Impression Callback
override fun onImpression(impressionData: ImpressionData) {
Log.d("Banner", "Banner impression, with revenue: ${impressionData.revenue}")
}
}
banner.setListener(new Banner.Listener() {
@Override
public void onPrebiddingFinished(@NonNull PrebiddingResults result) {
Log.d("Banner", "Banner client-side bidding finished!");
}
@Override
public void onLoaded(@NonNull LoadResult loadResult) {
Log.d("Banner", "Banner loaded!");
}
@Override
public void onFailedToLoad(@NonNull LoadError loadError, @Nullable LoadResult loadResult) {
Log.d("Banner", "Banner failed to load. Reason: " + loadError.getMessage());
}
// Impression Callback
@Override
public void onImpression(@NonNull ImpressionData impressionData) {
Log.d("Banner", "Banner impression, with revenue: " + impressionData.getRevenue());
}
@Override
public void onClicked() {
}
@Override
public void onNetworkImpression(@NonNull LoadResult loadResult) {
}
@Override
public void onShowed() {
}
@Override
public void onFailedToShow(@NonNull ShowError showError) {
}
@Override
public void onDismissed() {
}
});
Load an ad
Load an ad (with custom properties)
Displaying the banner
The most common practice is to add the banner view to the view hierarchy either:
- After creating the
Banner
instance. - After the
onLoaded(loadResult)
listener callback is called.
Banner autorefresh
After being displayed for some time, our banner fires a Load()
call automatically to refresh its contents.
You can configure this time through our Admin tool for each one of your banners.
Banner retry
Our banner has a built in auto retry for failed loads attempts. This means that when a banner fails to load, it will retry again until it loads successfully. Time between each retry attemp will increase using an exponential backoff. You should not add any retry logic on your end, as it may interefere with our retry behaviour.
Code example
BannerSample.kt
import android.os.Bundle
import android.util.Log
import android.view.ViewGroup
import android.widget.Button
import androidx.appcompat.app.AppCompatActivity
import com.etermax.xmediator.core.api.Banner
import com.etermax.xmediator.core.api.entities.ImpressionData
import com.etermax.xmediator.core.api.entities.LoadError
import com.etermax.xmediator.core.api.entities.LoadResult
import com.example.yourapp.R
class BannerSample : AppCompatActivity() {
private var banner: Banner? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_banner)
findViewById<Button>(R.id.button_load).setOnClickListener {
Log.d("BannerSample", "Load button clicked")
// Dispose previous banner instance
banner?.destroy()
findViewById<ViewGroup>(R.id.banner_container).removeAllViews()
// Create an banner instance
banner = Banner.create(
activity = this,
placementId = "3-15/28",
size = Banner.Size.Phone,
)
// Add banner to view hierarchy
findViewById<ViewGroup>(R.id.banner_container).addView(banner?.view)
// Setup callbacks
banner?.listener = object : Banner.Listener {
override fun onLoaded(loadResult: LoadResult) {
Log.d("BannerSample", "Loaded!")
}
override fun onFailedToLoad(loadError: LoadError, loadResult: LoadResult?) {
Log.d("BannerSample", "Failed to load. Reason: ${loadError.message}")
}
override fun onImpression(impressionData: ImpressionData) {
Log.d("BannerSample", "Impression with revenue: ${impressionData.revenue}")
}
}
// Request an ad
banner?.load()
}
}
}