Forum Xamarin.Forms
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

Why Interstitial type Ads .IsLoaded always return false?

ViktorBylbasViktorBylbas UAMember ✭✭
edited September 2018 in Xamarin.Forms

Android

    public class AdInterstitial_Droid : Controls.Ad.IAdInterstitial
    {
        InterstitialAd interstitialAd;

        public AdInterstitial_Droid()
        {
            interstitialAd = new InterstitialAd(Android.App.Application.Context)
            {

                // TODO: change this id to your admob id  
                AdUnitId = "ca-app-pub-xxx/xxx"
            };
            LoadAd();
        }

        void LoadAd()
        {
            var requestbuilder = new AdRequest.Builder();
            requestbuilder.AddTestDevice("xxx");
            interstitialAd.LoadAd(requestbuilder.Build());
        }

        public void ShowAd()
        {
            if (interstitialAd.IsLoaded) // getting false
                interstitialAd.Show();

            LoadAd();
        }
    }

C#

    DependencyService.Get<IAdInterstitial>().ShowAd();

In iOS .IsReady return false too.

Answers

  • Umar3xUmar3x FRMember ✭✭✭

    For Android :

    [assembly: Dependency(typeof(InterstitialAdRenderer))]
    namespace InterstitialAd.Droid
    {
        public class InterstitialAdRenderer : AdListener, IInterstitialAds
        {
            Android.Gms.Ads.InterstitialAd _ad;
    
            public void Show()
            {
                var context = Android.App.Application.Context;
    
                Android.Gms.Ads.InterstitialAd ad = new Android.Gms.Ads.InterstitialAd(context);
                ad.AdUnitId = "YOURADSID";
    
                _ad = ad;
                OnAdLoaded();
                ad.AdListener = this;
    
                var requestbuilder = new AdRequest.Builder();
                ad.LoadAd(requestbuilder.Build());
    
            }
    
            public override void OnAdLoaded()
            {
                base.OnAdLoaded();
    
                if (_ad.IsLoaded)
                    _ad.Show();
            }
    
        }
    
    }
    
  • ViktorBylbasViktorBylbas UAMember ✭✭
    edited September 2018

    @Umar3x
    Anyway, I get false

    and this:

    public override void OnAdFailedToLoad(int errorCode)
        {
            base.OnAdFailedToLoad(errorCode);
        }
    

    errorCode = 0

    XAML:

    <StackLayout VerticalOptions="CenterAndExpand" Padding="40,0,40,0">  
        <Button Text="Interstitial" HorizontalOptions="FillAndExpand" TextColor="Blue"  BackgroundColor="White" Clicked="InterstitialAdShowClick"/>  
    </StackLayout>  
    

    C#

    void InterstitialAdShowClick(object sender, System.EventArgs e)
        {
            DependencyService.Get<IInterstitialAds>().ShowAd();
        }
    
  • ViktorBylbasViktorBylbas UAMember ✭✭

    With the test ids that is provided on the site, ads work

    https://developers.google.com/admob/android/test-ads#sample_ad_units

Sign In or Register to comment.