Created
July 26, 2022 05:50
-
-
Save ericksli/d4e4ab21ddfcb0054bd8852f3b9ff31e to your computer and use it in GitHub Desktop.
AdMob AdView in Jetpack Compose
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.view.ViewGroup | |
import androidx.compose.foundation.background | |
import androidx.compose.foundation.layout.Box | |
import androidx.compose.foundation.layout.defaultMinSize | |
import androidx.compose.foundation.layout.padding | |
import androidx.compose.material.Text | |
import androidx.compose.runtime.Composable | |
import androidx.compose.runtime.DisposableEffect | |
import androidx.compose.ui.Alignment | |
import androidx.compose.ui.Modifier | |
import androidx.compose.ui.graphics.Color | |
import androidx.compose.ui.platform.LocalContext | |
import androidx.compose.ui.platform.LocalInspectionMode | |
import androidx.compose.ui.platform.LocalLifecycleOwner | |
import androidx.compose.ui.tooling.preview.Preview | |
import androidx.compose.ui.unit.dp | |
import androidx.compose.ui.unit.sp | |
import androidx.compose.ui.viewinterop.AndroidView | |
import androidx.lifecycle.Lifecycle | |
import androidx.lifecycle.LifecycleEventObserver | |
import com.google.android.gms.ads.AdRequest | |
import com.google.android.gms.ads.AdSize | |
import com.google.android.gms.ads.AdView | |
private const val ADMOB_TEST_AD_UNIT_ID = "ca-app-pub-3940256099942544/2247696110" | |
@Composable | |
fun AdMobBannerAd( | |
modifier: Modifier = Modifier, | |
adSize: AdSize = AdSize.BANNER, | |
adUnitId: String = ADMOB_TEST_AD_UNIT_ID, | |
) { | |
val minSizeModifier = when (adSize) { | |
AdSize.BANNER -> Modifier.defaultMinSize(minWidth = 320.dp, minHeight = 50.dp) | |
AdSize.LARGE_BANNER -> Modifier.defaultMinSize(minWidth = 320.dp, minHeight = 100.dp) | |
AdSize.MEDIUM_RECTANGLE -> Modifier.defaultMinSize(minWidth = 300.dp, minHeight = 250.dp) | |
AdSize.FULL_BANNER -> Modifier.defaultMinSize(minWidth = 468.dp, minHeight = 60.dp) | |
AdSize.LEADERBOARD -> Modifier.defaultMinSize(minWidth = 728.dp, minHeight = 90.dp) | |
else -> Modifier | |
} | |
if (LocalInspectionMode.current) { | |
Box( | |
modifier = modifier | |
.then(minSizeModifier) | |
.background(Color.LightGray) | |
.padding(16.dp) | |
) { | |
Text( | |
modifier = Modifier.align(Alignment.Center), | |
text = "AdMob Banner Ad", | |
fontSize = 14.sp, | |
color = Color.DarkGray, | |
) | |
} | |
} else { | |
val adView = rememberAdViewWithLifecycle(adSize, adUnitId) | |
AndroidView( | |
modifier = modifier.then(minSizeModifier), | |
factory = { adView }, | |
) | |
} | |
} | |
@Composable | |
fun rememberAdViewWithLifecycle( | |
adSize: AdSize, | |
adUnitId: String, | |
adRequest: AdRequest = AdRequest.Builder().build(), | |
): AdView { | |
val context = LocalContext.current | |
val adView = AdView(context).apply { | |
layoutParams = ViewGroup.LayoutParams( | |
ViewGroup.LayoutParams.MATCH_PARENT, | |
ViewGroup.LayoutParams.WRAP_CONTENT, | |
) | |
this.adSize = adSize | |
this.adUnitId = adUnitId | |
loadAd(adRequest) | |
} | |
val lifecycle = LocalLifecycleOwner.current.lifecycle | |
DisposableEffect(lifecycle, adView) { | |
val lifecycleObserver = getAdLifecycleObserver(adView) | |
lifecycle.addObserver(lifecycleObserver) | |
onDispose { | |
lifecycle.removeObserver(lifecycleObserver) | |
adView.destroy() | |
} | |
} | |
return adView | |
} | |
private fun getAdLifecycleObserver(adView: AdView): LifecycleEventObserver = | |
LifecycleEventObserver { _, event -> | |
when (event) { | |
Lifecycle.Event.ON_RESUME -> adView.resume() | |
Lifecycle.Event.ON_PAUSE -> adView.pause() | |
Lifecycle.Event.ON_DESTROY -> adView.destroy() | |
else -> {} | |
} | |
} | |
@Preview | |
@Composable | |
fun AdMobBannerAdPreview() { | |
AdMobBannerAd() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
hey champ use
setAdSize(adSize)
as we cannot initialize val variables with new data