Skip to content

Commit

Permalink
Getting the anchored adaptive banner width from the parent view
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 713668292
  • Loading branch information
Justin Malandruccolo authored and copybara-github committed Jan 9, 2025
1 parent 02f8943 commit 640860e
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 89 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,11 @@

package com.google.android.gms.example.bannerexample;

import android.os.Build.VERSION;
import android.os.Build.VERSION_CODES;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowMetrics;
import android.widget.FrameLayout;
import android.widget.PopupMenu;
import android.widget.Toast;
Expand Down Expand Up @@ -53,6 +49,9 @@ public class MyActivity extends AppCompatActivity {
private final AtomicBoolean isMobileAdsInitializeCalled = new AtomicBoolean(false);
private GoogleMobileAdsConsentManager googleMobileAdsConsentManager;
private AdManagerAdView adView;
// [START set_ad_size]
private AdSize adSize;
// [END set_ad_size]
private FrameLayout adContainerView;

@Override
Expand Down Expand Up @@ -86,6 +85,28 @@ protected void onCreate(Bundle savedInstanceState) {
}
});

// [START get_ad_size]
// Wait for the layout to be completed before calculating the ad size.
adContainerView.post(
new Runnable() {
@Override
public void run() {
int adWidthPixels = adContainerView.getWidth();
float density = getResources().getDisplayMetrics().density;
int adWidth = (int) (adWidthPixels / density);
adSize =
AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize(MyActivity.this, adWidth);

// Load the banner ad with the calculated ad size.
// [START_EXCLUDE silent]
if (googleMobileAdsConsentManager.canRequestAds()) {
loadBanner();
}
// [END_EXCLUDE]
}
});
// [END get_ad_size]

// This sample attempts to load ads using consent obtained in the previous session.
if (googleMobileAdsConsentManager.canRequestAds()) {
initializeMobileAdsSdk();
Expand Down Expand Up @@ -168,7 +189,7 @@ private void loadBanner() {
// Create a new ad view.
adView = new AdManagerAdView(this);
adView.setAdUnitId(AD_UNIT);
adView.setAdSize(getAdSize());
adView.setAdSize(adSize);

// Replace ad container with new ad view.
adContainerView.removeAllViews();
Expand Down Expand Up @@ -199,25 +220,10 @@ private void initializeMobileAdsSdk() {
MobileAds.initialize(this, initializationStatus -> {});

// Load an ad on the main thread.
runOnUiThread(this::loadBanner);
if (adSize != null) {
runOnUiThread(this::loadBanner);
}
})
.start();
}

// [START get_ad_size]
// Get the ad size with screen width.
public AdSize getAdSize() {
DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
int adWidthPixels = displayMetrics.widthPixels;

if (VERSION.SDK_INT >= VERSION_CODES.R) {
WindowMetrics windowMetrics = this.getWindowManager().getCurrentWindowMetrics();
adWidthPixels = windowMetrics.getBounds().width();
}

float density = displayMetrics.density;
int adWidth = (int) (adWidthPixels / density);
return AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize(this, adWidth);
}
// [END get_ad_size]
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,11 @@

package com.google.android.gms.example.bannerexample;

import android.os.Build.VERSION;
import android.os.Build.VERSION_CODES;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowMetrics;
import android.widget.FrameLayout;
import android.widget.PopupMenu;
import android.widget.Toast;
Expand Down Expand Up @@ -53,6 +49,9 @@ public class MyActivity extends AppCompatActivity {
private final AtomicBoolean isMobileAdsInitializeCalled = new AtomicBoolean(false);
private GoogleMobileAdsConsentManager googleMobileAdsConsentManager;
private AdView adView;
// [START set_ad_size]
private AdSize adSize;
// [END set_ad_size]
private FrameLayout adContainerView;

@Override
Expand Down Expand Up @@ -86,6 +85,28 @@ protected void onCreate(Bundle savedInstanceState) {
}
});

// [START get_ad_size]
// Wait for the layout to be completed before calculating the ad size.
adContainerView.post(
new Runnable() {
@Override
public void run() {
int adWidthPixels = adContainerView.getWidth();
float density = getResources().getDisplayMetrics().density;
int adWidth = (int) (adWidthPixels / density);
adSize =
AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize(MyActivity.this, adWidth);

// Load the banner ad with the calculated ad size.
// [START_EXCLUDE silent]
if (googleMobileAdsConsentManager.canRequestAds()) {
loadBanner();
}
// [END_EXCLUDE]
}
});
// [END get_ad_size]

// This sample attempts to load ads using consent obtained in the previous session.
if (googleMobileAdsConsentManager.canRequestAds()) {
initializeMobileAdsSdk();
Expand Down Expand Up @@ -168,7 +189,7 @@ private void loadBanner() {
// Create a new ad view.
adView = new AdView(this);
adView.setAdUnitId(AD_UNIT_ID);
adView.setAdSize(getAdSize());
adView.setAdSize(adSize);

// Replace ad container with new ad view.
adContainerView.removeAllViews();
Expand Down Expand Up @@ -199,25 +220,10 @@ private void initializeMobileAdsSdk() {
MobileAds.initialize(this, initializationStatus -> {});

// Load an ad on the main thread.
runOnUiThread(this::loadBanner);
if (adSize != null) {
runOnUiThread(this::loadBanner);
}
})
.start();
}

// [START get_ad_size]
// Get the ad size with screen width.
public AdSize getAdSize() {
DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
int adWidthPixels = displayMetrics.widthPixels;

if (VERSION.SDK_INT >= VERSION_CODES.R) {
WindowMetrics windowMetrics = this.getWindowManager().getCurrentWindowMetrics();
adWidthPixels = windowMetrics.getBounds().width();
}

float density = displayMetrics.density;
int adWidth = (int) (adWidthPixels / density);
return AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize(this, adWidth);
}
// [END get_ad_size]
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,11 @@

package com.google.android.gms.example.bannerexample

import android.os.Build
import android.os.Bundle
import android.util.Log
import android.view.Menu
import android.view.MenuItem
import android.view.View
import android.view.WindowMetrics
import android.widget.PopupMenu
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
Expand All @@ -44,25 +42,10 @@ class MyActivity : AppCompatActivity() {
private var adView: AdManagerAdView? = null
private lateinit var binding: ActivityMyBinding
private lateinit var googleMobileAdsConsentManager: GoogleMobileAdsConsentManager
// [START set_ad_size]
private lateinit var adSize: AdSize

// [START get_ad_size]
// 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)
}

// [END get_ad_size]
// [END set_ad_size]

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand All @@ -89,6 +72,23 @@ class MyActivity : AppCompatActivity() {
}
}

// [START get_ad_size]
// Wait for the layout to be completed before calculating the ad size.
binding.adViewContainer.post {
val adWidthPixels = binding.adViewContainer.width
val density = resources.displayMetrics.density
val adWidth = (adWidthPixels / density).toInt()
adSize = AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize(this, adWidth)

// Load the banner ad with the calculated ad size.
// [START_EXCLUDE silent]
if (googleMobileAdsConsentManager.canRequestAds) {
loadBanner()
}
// [END_EXCLUDE]
}
// [END get_ad_size]

// This sample attempts to load ads using consent obtained in the previous session.
if (googleMobileAdsConsentManager.canRequestAds) {
initializeMobileAdsSdk()
Expand Down Expand Up @@ -188,7 +188,9 @@ class MyActivity : AppCompatActivity() {

runOnUiThread {
// Load an ad on the main thread.
loadBanner()
if (adSize != null) {
loadBanner()
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,11 @@

package com.google.android.gms.example.bannerexample

import android.os.Build
import android.os.Bundle
import android.util.Log
import android.view.Menu
import android.view.MenuItem
import android.view.View
import android.view.WindowMetrics
import android.widget.PopupMenu
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
Expand All @@ -44,25 +42,10 @@ class MainActivity : AppCompatActivity() {
private var adView: AdView? = null
private lateinit var binding: ActivityMainBinding
private lateinit var googleMobileAdsConsentManager: GoogleMobileAdsConsentManager
// [START set_ad_size]
private lateinit var adSize: AdSize

// [START get_ad_size]
// 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)
}

// [END get_ad_size]
// [END set_ad_size]

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand All @@ -89,11 +72,27 @@ class MainActivity : AppCompatActivity() {
}
}

// [START get_ad_size]
// Wait for the layout to be completed before calculating the ad size.
binding.adViewContainer.post {
val adWidthPixels = binding.adViewContainer.width
val density = resources.displayMetrics.density
val adWidth = (adWidthPixels / density).toInt()
adSize = AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize(this, adWidth)

// Load the banner ad with the calculated ad size.
// [START_EXCLUDE silent]
if (googleMobileAdsConsentManager.canRequestAds) {
loadBanner()
}
// [END_EXCLUDE]
}
// [END get_ad_size]

// This sample attempts to load ads using consent obtained in the previous session.
if (googleMobileAdsConsentManager.canRequestAds) {
initializeMobileAdsSdk()
}
// [END can_request_ads]
}

/** Called when leaving the activity. */
Expand Down Expand Up @@ -189,7 +188,9 @@ class MainActivity : AppCompatActivity() {

runOnUiThread {
// Load an ad on the main thread.
loadBanner()
if (adSize != null) {
loadBanner()
}
}
}
}
Expand Down

0 comments on commit 640860e

Please sign in to comment.