How to make AdMob work with the latest MG?

I was trying to integrate AdMob into my MG Android project following the instructions in this post:

However, as the author has pointed out, frameLayout.AddView(window) is not working with the latest MG source. So I was wondering if anyone could show me how to fix this error and get AdMob running on the latest MG Android? Thanks in advance! :smile:

Hi,

Here is the source code of my game project for MG 3.4. You need Google play service component.

private static InterstitialAd interstitial;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
var g = new Engine.GameEngine();

        g.activity = this;

     
        interstitial = AdWrapper.ConstructFullPageAdd(this, addid);
                          
        fl = new FrameLayout(this);
        fl.AddView((View)g.Services.GetService(typeof(View)));          
      
        SetContentView(fl);


        g.Run();
     
    }

public void ShowAd()
{

      RunOnUiThread(() =>
      {
          if (interstitial.AdListener != null)              
              interstitial.AdListener.Dispose();
          interstitial.AdListener = null;

          var intlistener = new AdMob.adlistener();           
          intlistener.AdLoaded += () => { if (interstitial.IsLoaded)interstitial.Show(); };

          interstitial.AdListener = intlistener;             
          interstitial.CustomBuild();
      });
    }

class adlistener : AdListener
{
// Declare the delegate (if using non-generic pattern).
public delegate void AdLoadedEvent();
public delegate void AdClosedEvent();
public delegate void AdOpenedEvent();

    // Declare the event. 
    public event AdLoadedEvent AdLoaded;
    public event AdClosedEvent AdClosed;
    public event AdOpenedEvent AdOpened;
    public event AdClosedEvent AdFailed;

    public override void OnAdFailedToLoad(int p0)
    {
        if (AdFailed != null) this.AdFailed();
        base.OnAdFailedToLoad(p0);
    }

    public override void OnAdLoaded()
    {
        if (AdLoaded != null) this.AdLoaded();
        base.OnAdLoaded();
    }


    public override void OnAdClosed()
    {
        if (AdClosed != null) this.AdClosed();
        base.OnAdClosed();
    }
    public override void OnAdOpened()
    {
        if (AdOpened != null) this.AdOpened();
        base.OnAdOpened();
    }
}

Hi @Kartal_Turgut, thanks so much for your help!!! The code you posted is not very easy to read, mainly because of the formatting. Also it is very different from the example I posted in the OP… How do you use the adlistener class? Is there a code snippet (i.e. a github example) that I can use to study from?

Also I wonder if there is any chance that you show me how to display a banner ad? Your code is mainly about displaying interstitials, which is really great, but I still need to get a banner ad working in my game first… :smiley:

You can find all you need with this link.

You should also know that admob elements just do not appear. Some times you need to wait. You can also track logs in debug. For test admob you must call requestbuilder.AddTestDevice(“YOUR DEVICE ID”) command. Device id appear in the build log(Output in VS) and in the test mode Google sends only test ads.

Hi @Kartal_Turgut, thanks so much for your example! Finally got both banner ad and interstitial working in my game today! :smiley:

HI Guys,

I have used your complete code in my game.But still ads are not displaying.
Even my game screen is coming bit black.
I have a small question,where do we need to call these methods ShowInterstitialAd,RefreshBannerAd or it
will automatically load from layout.
I know its a silly question,but I really want it,I am stuck with this ad thing.

Hi @tonyyy ,

The code below looks kind of messy, but it works for me.

Inside Activity1.cs:

public class Activity1 : Microsoft.Xna.Framework.AndroidGameActivity
    {
		private AdView bannerAd;
		public AdView BannerAd {
			get { return bannerAd; }
		}
		private static InterstitialAd interstitial;
		private const string bannerID = "your banner id";
		private const string interstitialID = "your interstitial id";
		private const string testDeviceID = "your test device id";
		private LinearLayout ll;
		private FrameLayout fl;

        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
			SupportClass.Version = GetCurrentAppVersion ();
            var g = new Game1(this);

			interstitial = AdWrapper.ConstructFullPageAd(this, interstitialID);

			fl = new FrameLayout (this);
			fl.AddView ((View)g.Services.GetService (typeof(View)));
            
			CreateBannerAd();

			SetContentView(fl);
            g.Run();
        }

		public void CreateBannerAd()
		{
			if (ll == null)
			{
				HideBannerAd();
				ll = new LinearLayout(this);
				ll.Orientation = Orientation.Horizontal;
				ll.SetGravity(GravityFlags.Left | GravityFlags.Bottom);

				bannerAd = new AdView(this);
				bannerAd.AdSize = AdSize.Banner;
				bannerAd.AdUnitId = bannerID;
				ll.AddView(bannerAd);
				fl.AddView(ll);
			}

			bannerAd.CustomBuild (testDeviceID);
		}

		public void HideBannerAd()
		{
			if (ll != null)
			{
				fl.RemoveView(ll);
				ll.RemoveView(bannerAd);
				bannerAd.Dispose();
				bannerAd = null;
				ll.Dispose();
				ll = null;
			}
		}

		public void ShowInterstitialAd()
		{
			RunOnUiThread(() =>
				{
					if (interstitial.AdListener != null)
						interstitial.AdListener.Dispose();
					interstitial.AdListener = null;
					var intlistener = new adlistener();
					intlistener.AdLoaded += () => { if (interstitial.IsLoaded)interstitial.Show(); };
					interstitial.AdListener = intlistener;

					interstitial.CustomBuild(testDeviceID);
				});
		}
    }

Inside AdWrapper.cs:

internal static class AdWrapper
	{
		public static InterstitialAd ConstructFullPageAd(Context con, string UnitID)
		{
			var ad = new InterstitialAd(con);
			ad.AdUnitId = UnitID;
			return ad;
		}

		public static AdView ConstructStandardBanner(Context con, AdSize adsize, string UnitID)
		{
			var ad = new AdView(con);
			ad.AdSize = adsize;
			ad.AdUnitId = UnitID;
			return ad;
		}

		public static InterstitialAd CustomBuild(this InterstitialAd ad, string testDeviceID)
		{
			var requestbuilder = new AdRequest.Builder()
				.AddTestDevice(AdRequest.DeviceIdEmulator)
				.AddTestDevice(testDeviceID)
				.Build();
			ad.LoadAd(requestbuilder);
			return ad;
		}

		public static AdView CustomBuild(this AdView ad, string testDeviceID)
		{
			var requestbuilder = new AdRequest.Builder()
				.AddTestDevice(AdRequest.DeviceIdEmulator)
				.AddTestDevice(testDeviceID)
				.Build();
			ad.LoadAd(requestbuilder);
			return ad;
		}
	}

Whenever you want to create a banner/interstitial, just call the CreateBannerAd() method or the ShowInterstitial() method in Activity1. If you want to hide the banner ad, call the HideBannerAd() method. That’s it!

HI greyp,
Thanks for replying.I am using your same code.But still I am not able to get ad.Pls…help…

Hey @tonyyy are there any errors in the log? Did you put in your own ad IDs and test device ID?

Hi greyp…
Here is my complete code…

var g = new Game1();
//interstitial = AdWrapper.ConstructFullPageAd(this, interstitialID);
fl = new FrameLayout (this);
fl.AddView ((View)g.Services.GetService (typeof(View)));
CreateBannerAd();
SetContentView(fl);
g.Run();

Rest is all same you have given…

In log also it is not showing anything

Did you edit AndroidManifest.xml to give permissions? Like Kartal_Turgut mentioned in his blog:

[code]




Also add INTERNET and ACCESS_NETWORK_STATE permissions.

[/code]

Yes greyp,

I have already edited my manifest file like above.

Then I really have no idea what the problem might be… Theoretically if you have entered your ad id and test device id, there should be some info in the log…

Here ya go the, full page ads AND reward ads