Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display AdMob ad at bottom of screen in Android app with Xamarin/Monogame?

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, "<my id here>");
            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, "<my id here>");
            adView.LayoutParameters = layoutParams;
            parent.AddView(adView, index);
            AdMobHelper.RequestFreshAd(adView);
            g.Run();
        }
    }

And my additional Main.axml file:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <SurfaceView
        android:id="@+id/GameView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />
    <com.google.ads.AdView
        android:id="@+id/Ad"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_gravity="bottom" />
</FrameLayout>

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!

like image 547
DaManWitDaPlan Avatar asked Apr 12 '13 01:04

DaManWitDaPlan


3 Answers

This is code for adding admob . I have done successfully , it might work for you

<com.google.ads.AdView
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:id="@+id/adView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
ads:adSize="BANNER"
ads:adUnitId="Your Admob 15 digit alpha numeric ID"
/>

and in your activity , in onCreate method add following code

AdView adview = (AdView)findViewById(R.id.adView);
AdRequest re = new AdRequest();
re.setTesting(true);
adview.loadAd(re);

This is work for me. For more detail you can visit the admob site

like image 125
UnderGround Avatar answered Oct 06 '22 21:10

UnderGround


Solution #2
A quick way without any coding is to just add the xmlns to the application element and ads:loadAdOnCreate="true" to the adView element in AndroidManifest.xml

like image 27
user3019799 Avatar answered Oct 06 '22 22:10

user3019799


This should work for you, I am using it this way with no problems.

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);
        LinearLayout ll = new LinearLayout(this);
        ll.setOrientation(LinearLayout.VERTICAL);
        ll.setGravity(Gravity.BOTTOM);
        fl.AddView(g.Window);
        adView = AdMobHelper.CreateAdView(this, "<my id here>");
        ll.addView(adView);
        fl.AddView(ll);
        SetContentView(fl);
        AdMobHelper.RequestFreshAd(adView);
        g.Run();


    }
}
like image 37
a54studio Avatar answered Oct 06 '22 22:10

a54studio