Search code examples
c#androidxamarininterstitial

xamarin Interstitia ad needs to click button twice to show up


I want to show an interstitial ad on button click. The issue is that it needs to press the button twice in order to show up.

Interface

public interface IAdInterstitial
{
    void ShowAd();
}

Android class

[assembly: Dependency(typeof(AdMobInterstitial))]
namespace CoronavirusTest.Droid
{
    class AdMobInterstitial:IAdInterstitial
    {
        InterstitialAd interstitialAd;
        public AdMobInterstitial()
        {
            interstitialAd = new InterstitialAd(Android.App.Application.Context);
            interstitialAd.AdUnitId = "ca-app-pub-2981452032483899/1747111924";
            LoadAd();
        }
        void LoadAd()
        {
            var requestbuilder = new AdRequest.Builder();
            interstitialAd.LoadAd(requestbuilder.Build());
        }
        public void ShowAd()
        {
            if (interstitialAd.IsLoaded)
                interstitialAd.Show();
            LoadAd();
        }
    }
}

Button event

private void Button_Clicked(object sender, EventArgs e)
{
    IAdInterstitial adInterstitial = DependencyService.Get<IAdInterstitial>();
    adInterstitial.ShowAd();
}

Also added in manifest some code I found online which I read is needed in the application section

<activity android:name="com.google.android.gms.ads.AdActivity" android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" >
</activity>

Solution

  • The original code load ad at first button click and then IsLoaded property return true to show the ad at second button click.

    You could try the code below.

      public interface IAdInterstitial
      {
        void LoadAd();
        void ShowAd();
      }
    

    Android implement:

      [assembly: Dependency(typeof(AdMobInterstitial))]
      namespace XamarinDemo.Droid.DependencyService
      {
        public class AdMobInterstitial : IAdInterstitial
        {
          InterstitialAd interstitialAd;
    
          public AdMobInterstitial()
          {
            interstitialAd = new InterstitialAd(Android.App.Application.Context);
            interstitialAd.AdUnitId = "ca-app-pub-3940256099942544/1033173712"; //PUT YOUR ID HERE
            LoadAd();
          }
    
          public void LoadAd()
          {
            var requestbuilder = new AdRequest.Builder();
            interstitialAd.LoadAd(requestbuilder.Build());
          }
    
          public void ShowAd()
          {
            if (interstitialAd.IsLoaded)
                interstitialAd.Show();          
          }
       }  
    }
    

    Usage:

    public partial class Page1 : ContentPage
    {
        public Page1()
        {
            InitializeComponent();
        }
        protected override void OnAppearing()
        {
            base.OnAppearing();
            DependencyService.Get<IAdInterstitial>().LoadAd();
        }
        private void btnLoad_Clicked(object sender, EventArgs e)
        {
           DependencyService.Get<IAdInterstitial>().ShowAd();
        }     
    }
    

    enter image description here