Search code examples
xamarinxamarin.formsxamarin.androidadmob

AdMob SmartBanner in Xamain Forms Grid with Auto height not showing up


I created a Hello World Xamarin Forms app and added code to add AdMob Smart Banner ad following this tutorial. This was working fine until I received an email from AdMob saying "Restricted Ad Serving". So I pulled out my old code gave it a run in Emulator, and the ad wasn't showing up at all, not even the white space(although I can still see blank space in the App Store version).

So I created a blank 'Hello World' app followed the same tutorial. The Ad didn't showed up at all. I verified everything.

XAML -

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="*"/>
        <RowDefinition Height="Auto"/>
    </Grid.RowDefinitions>
    <!-- Place new controls here -->
    <Label Text="Welcome to Xamarin.Forms!" 
       HorizontalOptions="Center"
       VerticalOptions="CenterAndExpand" />
    <ctrl:GoogleAdView Grid.Row="1"/>
</Grid>

Then just out of curiosity I specified some random height to the Ad row and voila Ad shows up.

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="*"/>
        <RowDefinition Height="50"/>
    </Grid.RowDefinitions>
    <!-- Place new controls here -->
    <Label Text="Welcome to Xamarin.Forms!" 
       HorizontalOptions="Center"
       VerticalOptions="CenterAndExpand" />
    <ctrl:GoogleAdView Grid.Row="1"/>
</Grid>

Below are the details of my project (Android) -

NuGet packages list for Xamarin Forms Android project

Is something changed in how we use AdMob in Xamarin Forms?


Solution

  • I have always had to set a Height on my smart banner control. My layout is a bit different since I have my ad control inside of a ViewCell but the height logic should be similar for your RowDefinition.Height. Try the following method (I use Xamarin.Essentials plugin to get access to DeviceDisplay.MainDisplayInfo but there are other ways to get these values)

        public static int GetSmartBannerHeight() {
            DisplayInfo metrics = DeviceDisplay.MainDisplayInfo;
    
            double heightDp = metrics.Height / metrics.Density;
    
            if (heightDp <= 400) {
                return 32;
            }
    
            if (heightDp <= 720) {
                return 50;
            }
    
            return 90;
        }