YandexBanner
import androidx.compose.runtime.Composable
import androidx.compose.ui.viewinterop.AndroidView
import com.yandex.mobile.ads.banner.AdSize
import com.yandex.mobile.ads.banner.BannerAdEventListener
import com.yandex.mobile.ads.banner.BannerAdView
import com.yandex.mobile.ads.common.AdRequest
import com.yandex.mobile.ads.common.AdRequestError
import com.yandex.mobile.ads.common.ImpressionData
@Composable
fun Banner(id: Int) {
AndroidView(factory = { context ->
BannerAdView(context).apply {
setAdUnitId(context.getString(id))
setAdSize(AdSize.stickySize(context, 300))
val adRequest = AdRequest.Builder().build()
setBannerAdEventListener(object : BannerAdEventListener{
override fun onAdLoaded() {
}
override fun onAdFailedToLoad(p0: AdRequestError) {
}
override fun onAdClicked() {
}
override fun onLeftApplication() {
}
override fun onReturnedToApplication() {
}
override fun onImpression(p0: ImpressionData?) {
}
})
loadAd(adRequest)
}
})
}
AdMobBanner.kt
import androidx.compose.runtime.Composable
import androidx.compose.ui.viewinterop.AndroidView
import com.google.android.gms.ads.AdListener
import com.google.android.gms.ads.AdRequest
import com.google.android.gms.ads.AdSize
import com.google.android.gms.ads.AdView
import com.google.android.gms.ads.LoadAdError
@Composable
fun AdMobBanner(id: Int) {
AndroidView(factory = { context ->
AdView(context).apply {
setAdSize(AdSize.BANNER)
adUnitId = context.getString(id)
val adRequest = AdRequest.Builder().build()
adListener = object : AdListener(){
override fun onAdClicked() {
super.onAdClicked()
}
override fun onAdClosed() {
super.onAdClosed()
}
override fun onAdFailedToLoad(p0: LoadAdError) {
super.onAdFailedToLoad(p0)
}
override fun onAdImpression() {
super.onAdImpression()
}
override fun onAdLoaded() {
super.onAdLoaded()
}
override fun onAdOpened() {
super.onAdOpened()
}
override fun onAdSwipeGestureClicked() {
super.onAdSwipeGestureClicked()
}
}
loadAd(adRequest)
}
})
}
App.kt
import android.app.Application
import com.google.android.gms.ads.MobileAds
class App : Application() {
override fun onCreate() {
super.onCreate()
com.yandex.mobile.ads.common.MobileAds.initialize(this){ }
MobileAds.initialize(this) {}
}
}
зависимости:
implementation 'com.yandex.android:mobileads:5.8.0'
implementation 'com.google.android.gms:play-services-ads:22.1.0'
Id для манифеста (только для AdMob):
<!-- Sample AdMob app ID: ca-app-pub-3940256099942544~3347511713 -->
<meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="ca-app-pub-3940256099942544~3347511713"/>
strings.xml
<resources>
<string name="app_name">Banner</string>
<string name="banner_1">demo-banner-yandex</string>
<string name="banner_ad_mob">ca-app-pub-3940256099942544/6300978111</string>
</resources>
MainActivity.kt
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.height
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.neco_desarrollo.banner.ui.theme.BannerTheme
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
BannerTheme {
Column(
modifier = Modifier.fillMaxSize(),
verticalArrangement = Arrangement.Bottom,
horizontalAlignment = Alignment.CenterHorizontally
) {
AdMobBanner(id = R.string.banner_ad_mob)
Spacer(modifier = Modifier.height(30.dp))
Banner(id = R.string.banner_1)
}
}
}
}
}