Monogame & Google Ads

Hello there!

If someone could help me (or anyone) on the subject of integrating Ads (maybe AdMob) in Monogame 3.3, it would be great :smile: How to do it in the most current version? Any tutorial? I’ve never done it before, so any help is apprieciated.

Thanks!

I did a rough implementation of AdMob in Monogame in Doom & Destiny Free for Android and IOS.
You essentially have to use the Xamarin AdMob component and follow the samples in the component…
If you have some specific question i’ll be glad to help!

Well… thank you! Thats a start :smile:.

Download the Google Play Services component from the Xamarin Components store. The code below is a test app I put together using the standard Xamarin Studio new Android app template - modify accordingly:

using System;

using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;

using Android.Gms.Ads;

namespace GoogleServicesTest
{
[Activity (Label = “GoogleServicesTest”, MainLauncher = true, Icon = “@drawable/icon”)]
public class MainActivity : Activity
{
int count = 1;

    protected override void OnCreate (Bundle bundle)
    {
        base.OnCreate (bundle);

        // Set our view from the "main" layout resource
        SetContentView (Resource.Layout.Main);

        // Get our button from the layout resource,
        // and attach an event to it
        Button button = FindViewById<Button> (Resource.Id.myButton);
        button.Click += delegate {
            button.Text = string.Format ("{0} clicks!", count++);
        };

        // Begin Admob integration
        var ad = new AdView(this);
        ad.AdSize = AdSize.SmartBanner;
        ad.AdUnitId = "[YOUR ADMOB ID HERE]";
        var requestbuilder = new AdRequest.Builder();
        ad.LoadAd(requestbuilder.Build());

        var layout = FindViewById<LinearLayout>(Resource.Id.mainLayout);

        layout.AddView(ad);
    }
}

}

Hope this helps!

Thanks for the sample! However I did not manage to compile it. After getting Xamarin Google Play Component (succesfully added it to fresh andro project) I could not compile it. Errors were something like resource not found declared in values.xml. After that schemas not declared…

EDIT 2:
Schema and compile errors solved by adding 1G to java max heap size. Unfortunately, there are still lots of warnings on compile. But I believe it’s Xamarin component fault.

EDIT 1:
Okay I’ve successfuly managed to run admob. However I ran into one main problem - ad is not showing up on startup (after first request build). It’s there because when I pause my app and then resume it - ad comes visible. Ad also becomes visible adter scheduled refresh…

EDIT 3:
Ad not showing up on startup fixed. The only way I found is by adding adlistener like this (https://blog.tommyparnell.com/admob-with-xamarin-part-2-interstitialad/) and then pinning up AdLoaded event. In that event you need to call RequestLayout(), ie. like this:

adlistener.AdLoaded += () => { ad.RequestLayout(); };

I hope my time to this solution will help some of you ;).
By the way, next helpful article for beginners - https://blog.tommyparnell.com/admob-with-xamarin-part-1-banner-ads/

Cheers.

2 Likes

@mpeg88 - awesome stuff, glad to hear you got those errors resolved! And apologies for neglecting to mention that my sample was for banner ads, not interstitials…

You also found a solution to a problem that has been bugging me for a while - the first of my banner ads to load was not being displayed, so there was about a minute’s delay between the game opening and an ad actually showing. But that little trick with the AdListener calling RequestLayout on load seems to have done the trick, so thanks a lot!

I love you ! I got serveral errors too with values.xml and other things … It was horrible ! But adding 1G to java max heap size resolve every things ! You re my hero ! Love you <3