0

I have added a Admob banner to a scene in my app by using the example from: https://forums.xamarin.com/discussion/51779/cocossahrp-and-admob.

The ad displays at the top of the screen but I want to display it at the bottom. I couldn't find any CocosSharp examples but I did find one for cocos2d-x(last post): http://discuss.cocos2d-x.org/t/admob-bottom-placement-tutorial-v3-0/13854/11

I have tried this with my code(see below) but can't get it working. Something I have noticed though is if I change the LayoutParams from 200 to size.Y the ad banner appears in the middle of the screen instead of the top but this happens even when AlignParentBottom or Gravity.Bottom is not used.

Using a RelativeLayout with AddRule:

        Android.Graphics.Point size = new Android.Graphics.Point();
        prActivity.WindowManager.DefaultDisplay.GetSize(size);
        RelativeLayout.LayoutParams adParams = new RelativeLayout.LayoutParams(
            size.X,
            200);
        var bannerAd = new AdView(prActivity);
        bannerAd.AdSize = AdSize.SmartBanner;
        bannerAd.AdUnitId = "ca-app-pub-3940256099942544/6300978111"; // Test UnitId
        bannerAd.LayoutParameters = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WrapContent, ViewGroup.LayoutParams.WrapContent);
        var lcRequestbuilder = new AdRequest.Builder();
        bannerAd.LoadAd(lcRequestbuilder.Build());
        adParams.AddRule(LayoutRules.AlignParentBottom); // Rule to place ad at the bottom
        prActivity.AddContentView(bannerAd, adParams);

        ScheduleOnce(t =>
        {
            bannerAd.BringToFront();
        }, 3f);

Using a LinearLayout with Gravity:

        Android.Graphics.Point size = new Android.Graphics.Point();
        prActivity.WindowManager.DefaultDisplay.GetSize(size);
        LinearLayout.LayoutParams adParams = new LinearLayout.LayoutParams(
            size.X,
            200);
        var bannerAd = new AdView(prActivity);
        bannerAd.AdSize = AdSize.SmartBanner;
        bannerAd.AdUnitId = "ca-app-pub-3940256099942544/6300978111"; // Test UnitId
        bannerAd.LayoutParameters = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WrapContent, ViewGroup.LayoutParams.WrapContent);
        var lcRequestbuilder = new AdRequest.Builder();
        bannerAd.LoadAd(lcRequestbuilder.Build());
        adParams.Gravity = GravityFlags.Bottom; // Gravity Flag to place ad at the bottom
        prActivity.AddContentView(bannerAd, adParams);

        ScheduleOnce(t =>
        {
            bannerAd.BringToFront();
        }, 3f);

1 Answers1

0

I use AdMob Ad in this way in my App and it's work for me(Ad is in the bottom of the screen) :

 private static final String AD_UNIT_ID = "";
 private AdView adView;

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    LinearLayout.LayoutParams adParams = new LinearLayout.LayoutParams(getWindowManager().getDefaultDisplay().getWidth(), (getWindowManager().getDefaultDisplay().getHeight() + getWindowManager().getDefaultDisplay().getHeight()) - 100);
        adView = new AdView(this);
        adView.setAdSize(AdSize.BANNER);
        adView.setAdUnitId(AD_UNIT_ID);
        adView.setBackgroundColor(Color.TRANSPARENT);
        addContentView(this.adView, adParams);

    }

getWidth method of Display is deprecated but still i am using it.

Abhishek Aryan
  • 19,936
  • 8
  • 46
  • 65