Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

issue with android banner ad implimentation

26 views
Skip to first unread message

Owino Tonny

unread,
Feb 12, 2025, 3:36:32 AMFeb 12
to Google Mobile Ads SDK Developers
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")
}
}

Mobile Ads SDK Forum Advisor

unread,
Feb 12, 2025, 7:35:47 AMFeb 12
to 2win...@gmail.com, google-adm...@googlegroups.com
Hi,

Thank you for contacting the Mobile Ads SDK Support team.

You can refer to our Google Sample Project for integration help. In case you face any issue again then reach out to us with stack trace along with Sample project reproducing crash issue for further investigation.
 
This message is in relation to case "ref:!00D1U01174p.!5004Q02vH1Sj:ref" (ADR-00288015)

Thanks,
 
Google Logo Mobile Ads SDK Team

Feedback
How was our support today?

rating1    rating2    rating3    rating4    rating5



Mobile Ads SDK Forum Advisor

unread,
Feb 12, 2025, 7:42:30 AMFeb 12
to 2win...@gmail.com, google-adm...@googlegroups.com

Hi,

Thank you for contacting the Mobile Ads SDK Support team.

You can refer to our Google Sample Project for integration help.
In case you face any issue again then reach out to us with stack trace along with Sample project reproducing crash issue for further investigation.

If the file(s) you are looking to share are less than 25mb in total you can attach them to this case on your next reply. If you are having trouble attaching your file to this case or if your file(s) are larger than 25mb, you can share your files with me by performing the following steps:

1. Navigate to

https://docs.google.com/forms/d/e/1FAIpQLSfkAiXMeYP-fw1W3Z-tT9uwmATEKO5X6S-th0gR2ezdKaaqfg/viewform?usp=pp_url&entry.400550049=Mobile+Ads+SDK&entry.460850823=5004Q00002vH1SjQAK&entry.80707362=00288015

2. Fill out all fields, and attach your file(s).

3. Please reply back on this thread when you have uploaded your file(s). Please do not share this link.

Reply all
Reply to author
Forward
0 new messages