How to display AdMob ad at bottom of screen in Android app with Xamarin/Monogame?-Collection of common programming errors
I’ve been developing a game with MonoGame for a few months, and I just released it a few days ago. Now, I’m trying to get AdMob to work correctly so I can release a free lite version of the app. I’m a newbie to Android layouts, so I have no idea what I’m doing.
I was able to get an ad to display within my app after scouring the web for some info on implementing AdMob with MonoGame, but the app always displays on the top of the screen. I’ve had very little luck finding any info about re-positioning the ad specifically with MonoGame/Xamarin.
Here is the code that works, and displays the ad on the top of the screen:
Activity1.cs:
public class Activity1 : Microsoft.Xna.Framework.AndroidGameActivity
{
internal View adView = null;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
Game1.Activity = this;
var g = new Game1();
FrameLayout fl = new FrameLayout(this);
fl.AddView(g.Window);
adView = AdMobHelper.CreateAdView(this, "");
var layoutParams = new ViewGroup.LayoutParams(Android.Views.ViewGroup.LayoutParams.FillParent, Android.Views.ViewGroup.LayoutParams.WrapContent);
fl.AddView(adView, layoutParams);
SetContentView(fl);
AdMobHelper.RequestFreshAd(adView);
g.Run();
}
}
This works without any additional layouts. Now, here is the code I’ve assimilated from a few websites covering the topic:
Activity1.cs:
public class Activity1 : Microsoft.Xna.Framework.AndroidGameActivity
{
internal View adView = null;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
Game1.Activity = this;
var g = new Game1();
SetContentView(Resource.Layout.Main);
View gameView = FindViewById(Resource.Id.GameView);
ViewGroup parent = (ViewGroup)gameView.Parent;
int index = parent.IndexOfChild(gameView);
parent.RemoveView(gameView);
parent.AddView(g.Window, index);
adView = FindViewById(Resource.Id.Ad);
parent = (ViewGroup)adView.Parent;
index = parent.IndexOfChild(adView);
parent.RemoveView(adView);
var layoutParams = adView.LayoutParameters;
adView = AdMobHelper.CreateAdView(this, "");
adView.LayoutParameters = layoutParams;
parent.AddView(adView, index);
AdMobHelper.RequestFreshAd(adView);
g.Run();
}
}
And my additional Main.axml file:
However, when I run this, I run into a crash at the line “SetContentView(Resource.Layout.Main);” in Activity1.cs. Visual Studio gives the error:
Unhandled Exception:
Android.Views.InflateException: Binary XML file line #1: Error inflating class com.admob.android.ads.AdView
Although there aren’t any errors in the error output window to check… Any help? Ideally, I’d like to just get this admob ad to show at the bottom of my app. Thanks!