File tree Expand file tree Collapse file tree
admanager/BannerExample/app/src/main/java/com/google/android/gms/example/bannerexample
admob/BannerExample/app/src/main/java/com/google/android/gms/example/bannerexample
kotlin/admanager/BannerExample/app/src/main/kotlin/com/google/android/gms/example/bannerexample Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -51,7 +51,6 @@ public class MyActivity extends AppCompatActivity {
5151 static final String AD_UNIT = "/21775744923/example/adaptive-banner" ;
5252 private static final String TAG = "MyActivity" ;
5353 private final AtomicBoolean isMobileAdsInitializeCalled = new AtomicBoolean (false );
54- private final AtomicBoolean initialLayoutComplete = new AtomicBoolean (false );
5554 private GoogleMobileAdsConsentManager googleMobileAdsConsentManager ;
5655 private AdManagerAdView adView ;
5756 private FrameLayout adContainerView ;
@@ -91,18 +90,6 @@ protected void onCreate(Bundle savedInstanceState) {
9190 if (googleMobileAdsConsentManager .canRequestAds ()) {
9291 initializeMobileAdsSdk ();
9392 }
94-
95- // Since we're loading the banner based on the adContainerView size, we need to wait until this
96- // view is laid out before we can get the width.
97- adContainerView
98- .getViewTreeObserver ()
99- .addOnGlobalLayoutListener (
100- () -> {
101- if (!initialLayoutComplete .getAndSet (true )
102- && googleMobileAdsConsentManager .canRequestAds ()) {
103- loadBanner ();
104- }
105- });
10693 }
10794
10895 @ Override
@@ -212,12 +199,7 @@ private void initializeMobileAdsSdk() {
212199 MobileAds .initialize (this , initializationStatus -> {});
213200
214201 // Load an ad on the main thread.
215- runOnUiThread (
216- () -> {
217- if (initialLayoutComplete .get ()) {
218- loadBanner ();
219- }
220- });
202+ runOnUiThread (this ::loadBanner );
221203 })
222204 .start ();
223205 }
Original file line number Diff line number Diff line change @@ -54,7 +54,6 @@ public class MyActivity extends AppCompatActivity {
5454 private GoogleMobileAdsConsentManager googleMobileAdsConsentManager ;
5555 private AdView adView ;
5656 private FrameLayout adContainerView ;
57- private final AtomicBoolean initialLayoutComplete = new AtomicBoolean (false );
5857
5958 @ Override
6059 protected void onCreate (Bundle savedInstanceState ) {
@@ -91,18 +90,6 @@ protected void onCreate(Bundle savedInstanceState) {
9190 if (googleMobileAdsConsentManager .canRequestAds ()) {
9291 initializeMobileAdsSdk ();
9392 }
94-
95- // Since we're loading the banner based on the adContainerView size, we need to wait until this
96- // view is laid out before we can get the width.
97- adContainerView
98- .getViewTreeObserver ()
99- .addOnGlobalLayoutListener (
100- () -> {
101- if (!initialLayoutComplete .getAndSet (true )
102- && googleMobileAdsConsentManager .canRequestAds ()) {
103- loadBanner ();
104- }
105- });
10693 }
10794
10895 @ Override
@@ -210,12 +197,7 @@ private void initializeMobileAdsSdk() {
210197 MobileAds .initialize (this , initializationStatus -> {});
211198
212199 // Load an ad on the main thread.
213- runOnUiThread (
214- () -> {
215- if (initialLayoutComplete .get ()) {
216- loadBanner ();
217- }
218- });
200+ runOnUiThread (this ::loadBanner );
219201 })
220202 .start ();
221203 }
Original file line number Diff line number Diff line change @@ -41,7 +41,6 @@ import kotlinx.coroutines.launch
4141class MyActivity : AppCompatActivity () {
4242
4343 private val isMobileAdsInitializeCalled = AtomicBoolean (false )
44- private val initialLayoutComplete = AtomicBoolean (false )
4544 private var adView: AdManagerAdView ? = null
4645 private lateinit var binding: ActivityMyBinding
4746 private lateinit var googleMobileAdsConsentManager: GoogleMobileAdsConsentManager
@@ -91,14 +90,6 @@ class MyActivity : AppCompatActivity() {
9190 if (googleMobileAdsConsentManager.canRequestAds) {
9291 initializeMobileAdsSdk()
9392 }
94-
95- // Since we're loading the banner based on the adContainerView size, we need to wait until this
96- // view is laid out before we can get the width.
97- binding.adViewContainer.viewTreeObserver.addOnGlobalLayoutListener {
98- if (! initialLayoutComplete.getAndSet(true ) && googleMobileAdsConsentManager.canRequestAds) {
99- loadBanner()
100- }
101- }
10293 }
10394
10495 /* * Called when leaving the activity. */
@@ -185,9 +176,7 @@ class MyActivity : AppCompatActivity() {
185176
186177 runOnUiThread {
187178 // Load an ad on the main thread.
188- if (initialLayoutComplete.get()) {
189- loadBanner()
190- }
179+ loadBanner()
191180 }
192181 }
193182 }
You can’t perform that action at this time.
0 commit comments