简体   繁体   中英

Xamarin Forms Adaptive Banner Ad Won't Load in Landscape

I recently upgraded from a Galaxy S6 to a Galaxy S21 Ultra and now the AdMob adaptive banner advert will not load in landscape. On the S6 it is fine, but on the S21 I get the error:

[Ads] Ad failed to load : 1

The code I use to create the advert is here:

private void NewAdaptiveBannerAd()
{
    try
    {
        adAdaptiveBannerView = new AdAdaptiveBannerView();
        adAdaptiveBannerView.Padding = new Thickness(0);
        adAdaptiveBannerView.BackgroundColor = (Color)Application.Current.Resources["baseBackground_Light"];

        AdGrid.Children.Add(adAdaptiveBannerView, 0, 1);

        adAdaptiveBannerView.HeightRequest = getCurrentOrientationAnchoredAdaptiveBannerAdSize(Android.App.Application.Context, (int)(DeviceDisplay.MainDisplayInfo.Width / DeviceDisplay.MainDisplayInfo.Density));

        static int getCurrentOrientationAnchoredAdaptiveBannerAdSize(Context context, int adWidth)
        {
            int var3 = 0;

            if (context == null)
            {
                return 0;
            }
            else
            {
                int var7 = 2;
                switch (DeviceDisplay.MainDisplayInfo.Orientation)
                {
                    case DisplayOrientation.Unknown: var7 = 0; break;
                    case DisplayOrientation.Portrait: var7 = 1; break;
                    case DisplayOrientation.Landscape: var7 = 2; break;
                    default: return 0;
                }
                if (var3 == 0)
                {
                    var3 = var7;
                }

                int var8 = var3 == var7 ? (int)Math.Round((float)DeviceDisplay.MainDisplayInfo.Height / DeviceDisplay.MainDisplayInfo.Density) : (int)Math.Round((float)DeviceDisplay.MainDisplayInfo.Width / DeviceDisplay.MainDisplayInfo.Density);
                int var9 = (int)Math.Min(90, Math.Round((float)var8 * 0.15F));
                int var10;
                if (adWidth > 655)
                {
                    var10 = (int)Math.Round((float)adWidth / 728.0F * 90.0F);
                }
                else if (adWidth > 632)
                {
                    var10 = 81;
                }
                else if (adWidth > 526)
                {
                    var10 = (int)Math.Round((float)adWidth / 468.0F * 60.0F);
                }
                else if (adWidth > 432)
                {
                    var10 = 68;
                }
                else
                {
                    var10 = (int)Math.Round((float)adWidth / 320.0F * 50.0F);
                }

                var10 = Math.Max(Math.Min(var10, var9), 50);
                return var10;
            }
        }
    }
    catch (Exception)
    {
        Console.WriteLine("Creation of banner ad in SavedCharts failed.");
    }
}

adAdaptiveBannerView is declared at the top of the pages class. The getCurrentOrientationAnchoredAdaptiveBannerAdSize function is from another post on here that I can't find now.

I can't find anything that would explain this, or why it would behave differently between these two 'phones (is it the difference in Android versions, the different aspect ratio or what?). Testing on the emulator is difficult because it doesn't report orientation correctly.

Does anyone have any idea what's going on?

My app will, by it's nature, mostly be used in landscape mode, so this problem means there's a chance many people won't see the adverts.

PS I'm using Xamarin.GooglePlayServices.Ads version 119.8.0. I haven't been able to implement version 120 because there's no documentation or anything that would help me do it.

Edit 1: I've tried conditionally adding an adaptive banner ad when in portrait, and a standard banner ad using MarcTron.Admob. It doesn't work.

I wondered if it's because the S21 Ultra's display is 1440 x 3200 pixels with an aspect ration of 20:9, and Google Ad Mob can't cope with it. It's rather stupid if that's the case ('adaptive'?!). So I tried giving it a screen width of 2560 on the basis that it worked on the S6 (1440x3200), and 1600 (ie half), but neither worked.

Edit 2: I realised I'd made a mistake when I tried limiting the screen width to 2560px, which was that I only did that for the height request of the view, whereas I also needed to do it for the AdSize property of the AdMob class itself. Anyway, that is the problem - adaptive banner ads can't cope with a display 3200px wide.

Google AdMab adaptive banner adverts cannot cope with a screen width of 3200px. I don't know what the actual limit is but since it works on my other device that has a screen width of 2560px I've limited it to that, like so:

adView.AdSize = AdSize.GetCurrentOrientationAnchoredAdaptiveBannerAdSize(Android.App.Application.Context, (int)(Math.Min(2560,DeviceDisplay.MainDisplayInfo.Width) / DeviceDisplay.MainDisplayInfo.Density));

and applied the same Math.Min() limiter to the code in my question.

The has the slightly unfortunate side-effect of a gap either side of the banner ad (3200-2560)/2=320 pixels wide. This doesn't violate Google's policies though ( https://developers.google.com/admob/android/banner/adaptive ).

I suggest this is a bug in Google's API (or at least the NuGet package), as an 'adaptive' ad that can't cope with all screen resolutions in real devices doesn't live up to its name.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM