How to display an Admob interstitial, after a page has loaded in Xamarin Forms?

J0hsJ0hs Member ✭✭

The interstitial works fine when displayed through a button press. I have tried OnAppearing, but it runs before the page is loaded on Android. It seems no matter what I do, even with delayed calls, the page display overrides the ad.

Best Answer

Answers

  • jezhjezh Member, Xamarin Team Xamurai

    Could you please some main code snippet or a basic demo so that we can see with it ?

  • J0hsJ0hs Member ✭✭

    IAdInterstitial_Droid

    using System;
    using Android.Gms.Ads;
    using Verse.Droid;
    using Xamarin.Forms;
    
    [assembly: Dependency(typeof(IAdInterstitial_Droid))]
    namespace Verse.Droid
    {
        public class IAdInterstitial_Droid : IAdInterstitial
        {
            InterstitialAd interstitialAd;
    
            public IAdInterstitial_Droid()
            {
                interstitialAd = new InterstitialAd(Android.App.Application.Context);
                interstitialAd.AdUnitId = "ca-app-pub-3940256099942544/1033173712"; // Admob test ad
                LoadAd();
            }
    
            void LoadAd()
            {
                var requestbuilder = new AdRequest.Builder();
                interstitialAd.LoadAd(requestbuilder.Build());
            }
    
            public void ShowAd()
            {
                if (interstitialAd.IsLoaded)
                    interstitialAd.Show();
    
                LoadAd();
            }
        }
    }
    

    IAdInterstitial.cs

        using System;
        using System.Collections.Generic;
        using System.Text;
    
        namespace Verse
        {
            interface IAdInterstitial
            {
                void ShowAd();
            }
        }
    

    Show the ad

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

    Thanks in advance :smile:

  • J0hsJ0hs Member ✭✭

    Okay, I guess that is impossible :/

  • EddyBoughioulEddyBoughioul USMember ✭✭

    Hello i begin to integrate admob on my app, my page load before my ads in my case, so we see the page and after the ads, I do the same integration.

  • J0hsJ0hs Member ✭✭

    Where do you place the code that displays the ad?

  • J0hsJ0hs Member ✭✭
    Accepted Answer

    After working on the project on and off, I finally solved the problem by using the MarcTron.Admob plugin, instead of my initial implementation.

    Here is a tutorial for the plugin

Sign In or Register to comment.