Banner Ad Handler
A handler for banner ads on Android.
Parameters
activity
The activity context.
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)
}Content copied to clipboard
Parameters
activity
The platform-specific activity or context required for displaying the ad (e.g., an Android Activity).
Properties
banner View
Link copied to clipboard
The underlying BannerView instance.
Link copied to clipboard
Determines the AdState of the BannerAdHandler.
Determines the AdState of the BannerAdHandler
Functions
Link copied to clipboard
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.