Banner Advertisement with Xamarin.Forms

2.4k views Asked by At

I just want to know about the banner advertisements supported with Xamarin.Forms without any patch or loophole. Is there any advertisement provider who are providing their SDKs with the Xamarin.Forms?

Thanks in advance.

1

There are 1 answers

4
Zverev Evgeniy On BEST ANSWER

There are both SDK and step-by-step examples for Google AdMob for Xamarin.Android. You are going to need the Xamarin.GooglePlaySerives.Ads nuget.

I use it to show ads in my Xamarin.Forms app published at Google Play.

Here is the sample code for the android part of your application:

using System;

using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using Android.Support.V7.App;
using Android.Gms.Ads;
using Android;

namespace AdMobExample
{
    [Activity (Label = "@string/app_name", MainLauncher = true)]
    public class MainActivity : AppCompatActivity
    {
        protected AdView mAdView;
        protected InterstitialAd mInterstitialAd;
        protected Button mLoadInterstitialButton;

        protected override void OnCreate (Bundle savedInstanceState)
        {
            base.OnCreate (savedInstanceState);
            SetContentView (Resource.Layout.activity_main);

            mAdView = FindViewById<AdView> (Resource.Id.adView);
            var adRequest = new AdRequest.Builder ().Build ();
            mAdView.LoadAd (adRequest);

            mInterstitialAd = new InterstitialAd (this);
            mInterstitialAd.AdUnitId = GetString (Resource.String.test_interstitial_ad_unit_id);

            mInterstitialAd.AdListener = new AdListener (this);

            mLoadInterstitialButton = FindViewById<Button> (Resource.Id.load_interstitial_button);
            mLoadInterstitialButton.SetOnClickListener (new OnClickListener (this));
        }

        protected void RequestNewInterstitial ()
        {
            var adRequest = new AdRequest.Builder ().Build ();
            mInterstitialAd.LoadAd (adRequest);
        }

        protected void BeginSecondActivity ()
        {
            var intent = new Intent (this, typeof(SecondActivity));
            StartActivity (intent);
        }

        protected override void OnPause ()
        {
            if (mAdView != null) {
                mAdView.Pause ();
            }
            base.OnPause ();
        }

        protected override void OnResume ()
        {
            base.OnResume ();
            if (mAdView != null) {
                mAdView.Resume ();
            }
            if (!mInterstitialAd.IsLoaded) {
                RequestNewInterstitial ();
            }
        }

        protected override void OnDestroy ()
        {
            if (mAdView != null) {
                mAdView.Destroy ();
            }
            base.OnDestroy ();
        }

        class AdListener : Android.Gms.Ads.AdListener
        {
            MainActivity that;

            public AdListener (MainActivity t)
            {
                that = t;
            }

            public override void OnAdClosed ()
            {
                that.RequestNewInterstitial ();
                that.BeginSecondActivity ();
            }
        }

        class OnClickListener : Java.Lang.Object, View.IOnClickListener
        {
            MainActivity that;

            public OnClickListener (MainActivity t)
            {
                that = t;
            }

            public void OnClick (View v)
            {
                if (that.mInterstitialAd.IsLoaded) {
                    that.mInterstitialAd.Show ();
                } else {
                    that.BeginSecondActivity ();
                }
            }
        }
    }
}

There is also a ste-by-step guide for AdMob ads for Xamarin.iOS:

using Google.MobileAds;
...

const string intersitialId = "<Get your ID at google.com/ads/admob>";

Interstitial adInterstitial;

public override void ViewDidLoad ()
{
    base.ViewDidLoad ();

    CreateAndRequestInterstitial ();
}

public void AfterSomeTime ()
{   
    if (adInterstitial.IsReady)
        adInterstitial.PresentFromRootViewController (navController);
}

void CreateAndRequestInterstitial ()
{
    adInterstitial = new Interstitial (intersitialId);
    adInterstitial.ScreenDismissed += (sender, e) => {
        // Interstitial is a one time use object. That means once an interstitial is shown, HasBeenUsed 
        // returns true and the interstitial can't be used to load another ad. 
        // To request another interstitial, you'll need to create a new Interstitial object.
        adInterstitial.Dispose ();
        adInterstitial = null;
        CreateAndRequestInterstitial ();
    };

    var request = Request.GetDefaultRequest ();
    // Requests test ads on devices you specify. Your test device ID is printed to the console when
    // an ad request is made. GADBannerView automatically returns test ads when running on a
    // simulator. After you get your device ID, add it here
    request.TestDevices = new [] { Request.SimulatorId.ToString () };

    adInterstitial.LoadRequest (request);
}