Banner Ads Integration Guide¶
This page provides a comprehensive guide on how to integrate banner ads into your Android application using the Mondiad SDK.
Adding the SDK to Your Project¶
To add the Mondiad SDK to your Android project, include the following dependency in your build.gradle file:
dependencies {
// your other dependencies here
implementation("com.mndiad:android-sdk:1.1.0")
}
Adding Banners to Your Layout¶
The Mondiad Banners SDK supports traditional XML layouts, View-based layouts, and Jetpack Compose. Follow the instructions below for your preferred layout method.
Banners can be displayed in the following positions:
TOP,BOTTOM,LEFT,RIGHT: Banner is displayed at the respective edge of the screen.VIEW: Banner is displayed inside a specific view layout.
The SDK supports the following banner sizes:
SMARTPHONE_BANNER(320x50)MEDIUM_RECTANGLE(300x250)MEDIUM_SQUARE(300x300)LEADERBOARD(728x90)
XML Layouts¶
-
Add the
mndiadnamespace to your root layout element in the XML file:<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:mndiad="http://schemas.android.com/apk/res-auto" ... /> -
BannerViewcan be added to your layout XML file like any other view. Here is an example of how to add a banner ad to your layout:<com.mndiad.android.sdk.banners.BannerView android:layout_width="match_parent" android:layout_height="wrap_content" mndiad:zone_id="---your_zone_id---" mndiad:position="BOTTOM" mndiad:size="SMARTPHONE_BANNER" mndiad:apply_safe_offset="true" />
Please refer to the BannerView Attributes section below for more details on the available attributes.
View-based Layouts¶
In your activity or fragment, create an instance of BannerView and set its attributes programmatically:
import com.mndiad.android.sdk.banners.BannerView
import com.mndiad.android.sdk.banners.BannerCallbacks
import com.mndiad.android.sdk.banners.BannerSize
import com.mndiad.android.sdk.banners.BannerPosition
class MyActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// your other initialization code here
BannerView(this).apply {
zoneId = "---your-zone-id---"
position = BannerPosition.LEFT
bannerCallbacks = object : BannerCallbacks {
override fun onAdShown() {
// Ad is shown
}
override fun onAdFailedToLoad(error: String) {
// Ad failed to load
}
}
start()
}
}
}
import com.mndiad.android.sdk.banners.BannerCallbacks;
import com.mndiad.android.sdk.banners.BannerPosition;
import com.mndiad.android.sdk.banners.BannerSize;
import com.mndiad.android.sdk.banners.BannerView;
public class MyActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// your other initialization code here
BannerView banner = new BannerView(this);
banner.setZoneId("---your-zone-id---");
banner.setPosition(BannerPosition.LEFT);
banner.setBannerCallbacks(new BannerCallbacks() {
@Override
public void onAdShown() {
// Ad is shown
}
@Override
public void onAdFailedToLoad(@NonNull String s) {
// Ad failed to load
}
}
);
banner.start();
}
}
Notice
Calling start() is required to load and display the banner ad for all banner positions except
VIEW. For the VIEW position, you may omit the start() call and the banner will load
automatically when added to a view hierarchy.
Jetpack Compose¶
To display a banner with the TOP, BOTTOM, LEFT, or RIGHT position in a Jetpack Compose
layout, instantiate a BannerView inside the onCreate method of an activity. Refer to
the View-based layouts section above for an example. Note that you need to
call the start() method to load and display the banner.
To display a banner with the VIEW position in a Jetpack Compose layout, use the following
composable function to wrap the BannerView:
@Composable
fun Banner(
zoneId: String,
modifier: Modifier = Modifier,
init: BannerView.() -> Unit = {}
) {
val context = LocalContext.current
val inspection = LocalInspectionMode.current
// Show placeholder in preview / if no Activity
val activity = context.findActivity()
if (inspection || activity == null) {
Surface(modifier = Modifier.fillMaxWidth()) {
Box(contentAlignment = Alignment.Center) {
Text("Banner placeholder")
}
}
return
}
Box(modifier) {
AndroidView(
modifier = Modifier.fillMaxWidth(),
factory = {
BannerView(context).apply {
this.zoneId = zoneId
init()
start()
}
}
)
}
}
private tailrec fun Context.findActivity(): Activity? =
when (this) {
is Activity -> this
is ContextWrapper -> baseContext.findActivity()
else -> null
}
You can then use this Banner composable in your Jetpack Compose layout like this:
Banner("---your-zone-id---", modifier = yourModifier) {
size = BannerSize.MEDIUM_RECTANGLE
bannerCallbacks = object : BannerCallbacks {
override fun onAdShown() {
// Ad is shown
}
override fun onAdFailedToLoad(error: String) {
// Ad failed to load
}
}
}
BannerView Attributes¶
Both XML and programmatic layouts support the following attributes for BannerView:
zoneId(String, required): Your banner ad zone ID obtained from the Mondiad dashboard.position(BannerPosition, defaultVIEW): The position where the banner will be displayed. Possible values areTOP,BOTTOM,LEFT,RIGHT, andVIEW. TheVIEWposition is used to display the banner inside a specific view layout.size(BannerSize, defaultSMARTPHONE_BANNER): The size of the banner.offset(Non-negativeInt, default0): Offset in pixels from the specified position. This attribute is available for all positions exceptVIEW.applySafeOffset(Boolean, defaultfalse): Whether to apply a safe area offset for banners positioned at the edges of the screen. This helps avoid overlapping with system UI elements such as the status bar, navigation bar, or display cutouts (notches). This attribute is available for all positions exceptVIEW.
applySafeOffset takes precedence over offset. If both are set, the safe area offset will be applied.
Programmatic layouts also support the bannerCallbacks property to handle banner events.
bannerCallbacks should be an implementation of the BannerCallbacks interface, which includes
the following methods:
onAdShown(): Called when the banner ad is successfully shown.onAdFailedToLoad(error: String): Called when the banner ad fails to load, with an error message.
Global Configuration Parameters¶
The Mondiad SDK allows you to set global configuration parameters that will apply to all banner ads
in your application. You can set these parameters in your Application class or in any activity,
before loading any banner ads.
import com.mndiad.android.sdk.Mnd
class MyApp : Application() {
override fun onCreate() {
super.onCreate()
// Enable demo mode for testing; Mondiad test ads will be shown
Mnd.banners.demoMode = true
// Set log level to control verbosity; default is Log.INFO
// You can disable logging by setting it to Log.ASSERT
Mnd.banners.logLevel = Log.ASSERT
}
}
import com.mndiad.android.sdk.Mnd;
public class MainApp extends Application {
@Override
public void onCreate() {
super.onCreate();
// Enable demo mode for testing; Mondiad test ads will be shown
Mnd.banners.setDemoMode(true);
// Set log level to control verbosity; default is Log.INFO
// You can disable logging by setting it to Log.ASSERT
Mnd.banners.setLogLevel(Log.ASSERT);
}
}