I’m close to publishing my first Xamarin Monogame app and now I’m putting in ads to monetize it. I knew this would be tricky, but after hours of sifting through bad examples and misleading forum posts, I was pulling my hair out. “Why is this soooo hard?” Finally, I found a sample that actually worked. It was too complicated of course, so I simplified it and I am presenting the results here for you. What you see below is the bare minimum code to get ads working in a Monogame project. Enjoy!
Step 1: put in required activities and permissions. My android manifest looks like this:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="AdMobInMonogame.AdMobInMonogame" android:versionCode="1" android:versionName="1.0">
<uses-sdk />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<application android:label="AdMobInMonogame" android:icon="@drawable/Icon">
<meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />
<activity android:name="com.google.android.gms.ads.AdActivity" android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" />
</application>
</manifest>
The android activity look like this:
using Android.App;
using Android.Gms.Ads;
using Android.OS;
using Android.Views;
using Android.Widget;
namespace AdMobInMonogame
{
[Activity(Label = "AdMobInMonogame" , MainLauncher = true)]
public class Activity1 : Microsoft.Xna.Framework.AndroidGameActivity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
var game = new MyGame();
var gameView = (View)game.Services.GetService(typeof(View));
// A container to show the add at the bottom of the page
var adContainer = new LinearLayout(this);
adContainer.Orientation = Orientation.Horizontal;
adContainer.SetGravity(GravityFlags.CenterHorizontal | GravityFlags.Bottom);
adContainer.SetBackgroundColor(Android.Graphics.Color.Transparent); // Need on some devices, not sure why
// A layout to hold the ad container and game view
var mainLayout = new FrameLayout(this);
mainLayout.AddView(gameView);
mainLayout.AddView(adContainer);
SetContentView(mainLayout);
// The actual ad
var bannerAd = new AdView(this)
{
AdUnitId = "ca-app-pub-3940256099942544/6300978111", // Get this id from admob "Monetize" tab
AdSize = AdSize.Banner,
};
bannerAd.LoadAd(new AdRequest.Builder()
.AddTestDevice("DEADBEEF9A2078B6AC72133BB7E6E177") // Prevents generating real impressions while testing
.Build());
// Give the game methods to show/hide the ad.
game.ShowAd = () => adContainer.AddView(bannerAd);
game.HideAd = () => adContainer.RemoveView(bannerAd);
game.Run();
}
}
}
And finally, the game class is set up to do a simple demonstration that turns the add on or off every 3 seconds:
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace AdMobInMonogame
{
public class MyGame : Game
{
GraphicsDeviceManager _graphics;
double _secondsPassed = 0;
bool _adIsVisible = false;
// Methods back into the Android activity to control ads
public Action ShowAd { get; set; }
public Action HideAd { get; set; }
public MyGame()
{
_graphics = new GraphicsDeviceManager(this);
}
protected override void Draw(GameTime gameTime)
{
// Some simple code to turn the add on and off every three seconds
_secondsPassed += gameTime.ElapsedGameTime.TotalSeconds;
if(_secondsPassed > 3)
{
_secondsPassed = 0;
if (_adIsVisible) HideAd();
else ShowAd();
_adIsVisible = !_adIsVisible;
}
GraphicsDevice.Clear(Color.CornflowerBlue);
base.Draw(gameTime);
}
}
}
Thanks! It works!
This is the only tutorial I could find on the world wide web about AdMob banners in MonoGame. AdMobBuddy is great but it only works for Interstital and Rewarded Ads, not banners. Now I only need to get this working on iOS as well. I think I can use your code as inspiration. Thanks again!
LikeLike