BannerAdHandler

actual class BannerAdHandler(activity: Any?)

A handler for banner ads on Android.

Parameters

activity

The activity context.

expect class BannerAdHandler(activity: Any?)

A handler for creating and managing banner ads that cover part of the host app screen.

This class is responsible for loading and managing the state of a banner advertisement provided by Google Mobile Ads. It requires a platform-specific activity or context for its operations.

Usage Example:

// In your Composable function or where you have access to an Activity:
val activity = LocalContext.current as Activity

// Instantiate the BannerAdHandler
val adHandler = rememberBannerAd(activity = activity)

// Load the Ad (typically in a LaunchedEffect or similar)
LaunchedEffect(Unit) {
adHandler.load(
adUnitId = "YOUR_ADMOB_BANNER_AD_UNIT_ID", // Replace with your actual Ad Unit ID
adSize = AdSize.BANNER, // Or any other desired AdSize
onLoad = {
// Ad loaded successfully
println("Banner Ad loaded!")
},
onFailure = { exception ->
// Ad failed to load
println("Banner Ad failed to load: ${exception.message}")
},
onDismissed = {
// Ad was dismissed (not typical for banners, but callback exists)
println("Banner Ad dismissed.")
},
onShown = {
// Ad is now visible on screen
println("Banner Ad shown.")
},
onImpression = {
// Ad impression has been recorded
println("Banner Ad impression recorded.")
},
onClick = {
// User clicked on the ad
println("Banner Ad clicked.")
}
)
}

// Display the ad in your Composable UI
if (adHandler.state == AdState.Loaded) { // Or handle other states like Loading, Error
BannerAd(adHandler)
}

Parameters

activity

The platform-specific activity or context required for displaying the ad (e.g., an Android Activity).

actual class BannerAdHandler(activity: Any?)

Constructors

Link copied to clipboard
actual constructor(activity: Any?)
expect constructor(activity: Any?)
actual constructor(activity: Any?)

Properties

Link copied to clipboard
actual val adSize: AdSize

Holds the active AdSize of the BannerAdHandler.

expect val adSize: AdSize

The active AdSize of the banner ad.

actual val adSize: AdSize

Holds the active AdSize of the BannerAdHandler

bannerView
Link copied to clipboard

The underlying BannerView instance.

Link copied to clipboard
actual val state: AdState

Determines the AdState of the BannerAdHandler.

expect val state: AdState

The current AdState of the banner ad.

actual val state: AdState

Determines the AdState of the BannerAdHandler

Functions

Link copied to clipboard
@RequiresPermission(value = "android.permission.INTERNET")
actual fun load(adUnitId: String, adSize: AdSize, onLoad: () -> Unit, onFailure: (Exception) -> Unit, onDismissed: () -> Unit, onShown: () -> Unit, onImpression: () -> Unit, onClick: () -> Unit)

Loads a banner ad.

expect fun load(adUnitId: String = AdUnitId.BANNER_DEFAULT, adSize: AdSize = AdSize.FULL_BANNER, onLoad: () -> Unit = {}, onFailure: (Exception) -> Unit = {}, onDismissed: () -> Unit = {}, onShown: () -> Unit = {}, onImpression: () -> Unit = {}, onClick: () -> Unit = {})

Loads a banner ad. Note: Make all calls to the Mobile Ads SDK on the main thread.

actual fun load(adUnitId: String, adSize: AdSize, onLoad: () -> Unit, onFailure: (Exception) -> Unit, onDismissed: () -> Unit, onShown: () -> Unit, onImpression: () -> Unit, onClick: () -> Unit)