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!
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ā¦
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.
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.
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!
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();
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ā¦