i am new to android and kotline but i have been taking the course "
Android Basics with Compose" and am in the second pathway, i built an app that i wanted to add a banner ad to but no matter how explicit i follow the documentation of admob banner implimentation i always endup with a crashed app, can someone please share with me a sample app with only a greeting activity which is twiked to use test ads and add any information i am to remember about the manifest etc, below is my current mainactivity.kt
package com.example.admobtest3
import android.os.Build
import android.os.Bundle
import android.view.WindowMetrics
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import com.example.admobtest3.ui.theme.AdmobTest3Theme
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.MobileAds
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
class MainActivity : ComponentActivity() {
// Get the ad size with screen width.
private val adSize: AdSize
get() {
val displayMetrics = resources.displayMetrics
val adWidthPixels =
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
val windowMetrics: WindowMetrics = this.windowManager.currentWindowMetrics
windowMetrics.bounds.width()
} else {
displayMetrics.widthPixels
}
val density = displayMetrics.density
val adWidth = (adWidthPixels / density).toInt()
return AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize(this, adWidth)
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
/// new admob initialization code
val backgroundScope = CoroutineScope(Dispatchers.IO)
backgroundScope.launch {
// Initialize the Google Mobile Ads SDK on a background thread.
MobileAds.initialize(this@MainActivity) {}
}
// end of admob initialization code
enableEdgeToEdge()
setContent {
AdmobTest3Theme {
Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding ->
Greeting(
name = "Android",
modifier = Modifier.padding(innerPadding)
)
}
}
}
}
}
private fun loadBanner() {
// [START create_ad_view]
// Create a new ad view.
val adView = AdView(this)
adView.adUnitId = ca-app-pub-3940256099942544/9214589741
adView.setAdSize(adSize)
this.adView = adView
// Replace ad container with new ad view.
binding.adViewContainer.removeAllViews()
binding.adViewContainer.addView(adView)
// [END create_ad_view]
// [START load_ad]
// Start loading the ad in the background.
val adRequest = AdRequest.Builder().build()
adView.loadAd(adRequest)
// [END load_ad]
}
@Composable
fun Greeting(name: String, modifier: Modifier = Modifier) {
Text(
text = "Hello $name!",
modifier = modifier
)
}
@Preview(showBackground = true)
@Composable
fun GreetingPreview() {
AdmobTest3Theme {
Greeting("Android")
}
}