admob positioning problem

@juan.csdc, what ad module are you using for Android.  iAds is for iOS/Apple only.

Hi Rob, 

Sorry for the confusion, I use iAds for apple devices and admob for android devices.

For iAds was very easy to place it on the bottom because it’s reference point seems to be LeftBottom instead of TopBottom.

To calculate the height for iOS I’m using the following code:

local scaleY = 1/display.contentScaleY if string.sub(system.getInfo("model"),1,4) == "iPad" then fxAds.bannerHeight = 50\*scaleY else fxAds.bannerHeight = 66\*scaleY end

While for Android and Admob:
 

local scaleY = 1/display.contentScaleY fxAds.bannerHeight = 50\*scaleY

I’ve tried different alternatives for calculating the bannerHeight for Android but any of them seems to be working.

I just used the adMob sample app, added my AppID, changed it from interstitial to banner, changed the positioning code to:

showAd = function( adType )         local adX, adY = display.screenOriginX, display.contentHeight - 50         statusText.text = ""         ads.show( adType, { x=adX, y=adY } ) end

And the ad is anchored to the bottom of the screen as I would expect.  It appears to be similar to a “TopLeft” reference point.   You’re doing some unusual scaling and I don’t know if that’s impacting you or not.  Ad’s are not really “display” objects, but more like native.newWebViews() in how they behave.

I’m using the lastest config.lua published here

--calculate the aspect ratio of the device local aspectRatio = display.pixelHeight / display.pixelWidth application = { content = { width = aspectRatio \> 1.5 and 800 or math.ceil( 1200 / aspectRatio ), height = aspectRatio \< 1.5 and 1200 or math.ceil( 800 \* aspectRatio ), scale = "letterBox", fps = 30, imageSuffix = { ["@2x"] = 1.3, }, }, }

Hi Rob,

thank you very much trying to solve this but unfortunately your solution does not work (at least for me).

On the Samsung Tab 2 and Transformer tablets the banner is a bit to low (bottom of ad is cut-off), on the Xperia phone the banner is to high.

Config.lua:

application = {
    content = {
        width = 640,
        height = 960,
        scale = “letterBox”,
        fps = 60,
        launchPad = false,

    },
}

Using latest public release 1202.

Regards,

Damir.

@juan.csdc I’m using the same config.lua, but I’m not using the same content scale.   I suspect it has to do with the fact that your content area is over twice as big and you have to adjust accordingly.  After some experimentation this worked for me with 800 and 1200

        local adX, adY = 0, display.contentHeight - 125

This comes from:  800 / 320 = 2.5 * 50px high ad = 125 total scaled height. Now this put the box a touch high.  It’s not flush to the bottom, but it’s in same place the sample app had it at 320x480 screens size (just a little space below).  Using 120 instead of 125 made it flush, but I suspect the ad itself may be a little short and the 125 is the right amount (and it mathematically works out)

Damir, in our case, you are using a content area that is an aspect ratio of 1:1.5, which will fill the screen on an iPhone 4.  It does not fill the screen on 16:9 type devices like the Xperia (1:1.7778), so that screen is longer than your content area.  The Samsung is a 1024x600 device or a 1:1.70 It’s shorter than the Xperia.  You config.lua needs to adapt to your device.  Given your config.lua you’re going to have to calculate what your real screen height is which will be something like:

adY = (display.content.pixelHeight / content.pixelWidth * disiplay.contentWidth) / 320 * 50

or something similar.

Thank you Rob.

Is support for “normal” banners for admob planned ?

(one of my games needs 320x50 banner in landscape)

If I got it right, Corona has implemented only smart-banners and those always fill the whole screen width.

Regards,

Damir.

I’m not sure.  Your best bet is to goto http://feedback.coronalabs.com and put in a feature request for it.

Thanks Rob,

I’ll try what you have mentioned before. Those same calculations will apply for apple right? So instead of using the scaleY I should be using the math you explained before?

Potentially.  Probably.

Hi Rob,

I finally got it working nicely on my android devices and my iPad mini, unfortunately I couldn’t test it on an iphone 4 and 5, but I’ve tried it also with a few Android devices, here it’s the code of my ads class for any “config.lua” (maybe)

if( not \_G.fx.ads ) then \_G.fx.ads = {height = 0} end local fxAds = \_G.fx.ads ads = require "ads"; local function fxAdListener(event) table.dump(event) end function fxAds.init() -- Load Corona 'ads' library if fx.device.isApple then if fx.device.isTablet then fxAds.bannerHeight = display.contentWidth / 320 \* 50 else fxAds.bannerHeight = display.contentWidth / 320 \* 66 end -- The name of the ad provider. local adNetwork = "iads"; local appID = appInfo.ads.iAds; ads.init(adNetwork, appID, fxAdListener); elseif fx.device.isAndroid then if fx.device.isTablet then fxAds.bannerHeight = display.contentWidth / 320 \* 90 else fxAds.bannerHeight = display.contentWidth / 320 \* 50 end -- The name of the ad provider. local adNetwork = "admob"; local appID = appInfo.ads.adMob; ads.init(adNetwork, appID, fxAdListener); end end function fxAds.showBanner(x, y, params) if fx.device.isSimulator then if params.srpH == "bottom" then y = y - fxAds.bannerHeight end fxAds.adBox = display.newRect(x, y, w, fxAds.bannerHeight) fxAds.adBox:setFillColor(255, 0, 0) elseif fx.device.isApple then ads.show("banner", {x=x, y=y, testMode=false}); elseif fx.device.isAndroid then if params.srpH == "bottom" then y = y - fxAds.bannerHeight end ads.show("banner", {x=x, y=y, testMode=false}); end end function fxAds.hide() ads.hide() end fxAds.init()

Take in consideration that I’m using this code from a part of a larger framework I’m currently developing so there are a few variables that are self explanatory, but that those are not calculated here.
I’m planning to release this framework to all developers as soon as I finish with the basics.

Hope this can helpful for others =)

I am trying to place Admob ad at the bottom of the screen, and found admob works differently for iOS & Android for the y value.

My config.lua is 320x480, letterbox.

For iOS, with following code, I can put the ad at the exact bottom: (by the way, the calculation of the y value looks strange)

ads.show( "banner", { x=display.screenOriginX, y=display.contentHeight - display.screenOriginY\*2})

I have tested it with iPad 4 & iPhone 5S and they both work well.

However, for Android, the above code does not work, and I still don’t know how to put it at the bottom.

Besides, if admob uses “smart banner”, the size varies depending on devices (https://developers.google.com/mobile-ads-sdk/docs/admob/smart-banners). It’s really hard to determine the right banner size admob uses for the current Android device. For example, Galaxy Tab 3, which one does it belong to? Is it an Android Tablet or Android widescreen devices?

For different devices, the screenOriginY is different, should this be considered into the calculation too?

Anyone knows how to put admob ad at the exact bottom of the screen?

@juan.csdc

Can you explain what is the value of ‘y’ you are passing to showBanner();

What is your content width and height in config file?

Hi @tarun9, I’ve updated my library, here is the code:

if( not \_G.fx.ads ) then \_G.fx.ads = {height = 0, rateShown = false} end local fxAds = \_G.fx.ads

_G.w = display.contentWidth

_G.h = display.contentHeight

local ads = require(“ads”) local function fxAdListener(event) table.dump(event) end function fxAds.init() print(‘initializing ads’) – Load Corona ‘ads’ library local baseCalculation = w / 320 if system.orientation ~= “portrait” then baseCalculation = h / 320 end if fx.device.isApple then if fx.device.isTablet then fxAds.bannerWidth = w if system.orientation ~= “portrait” then fxAds.bannerHeight = baseCalculation * 30 else fxAds.bannerHeight = baseCalculation * 32 end else fxAds.bannerWidth = w if w > h then fxAds.bannerHeight = baseCalculation * 32 else fxAds.bannerHeight = baseCalculation * 50 end end – The name of the ad provider. local adNetwork = “iads” local appID = appInfo.ads.iAds ads.init(adNetwork, appID, fxAdListener) elseif fx.device.isAndroid then if fx.device.isTablet then fxAds.bannerWidth = w fxAds.bannerHeight = baseCalculation * 90 else fxAds.bannerWidth = w fxAds.bannerHeight = baseCalculation * 50 end --fxAds.bannerHeight = fxAds.bannerHeight / 2 – The name of the ad provider. local adNetwork = “admob” local appID = appInfo.ads.adMob ads.init(adNetwork, appID, fxAdListener) end end function fxAds.showBanner(x, y, params) if fx.device.isSimulator then if params.srpH == “bottom” then y = y - fxAds.bannerHeight end fxAds.adBox = display.newGroup() local o = display.newRect(fxAds.adBox, x+fxAds.bannerWidth/2, y+fxAds.bannerHeight/2, fxAds.bannerWidth, fxAds.bannerHeight) o:setFillColor(255, 0, 0) --o = fx.ui.newText(fxAds.adBox, “Ads Here”, 0, 0, native.systemFont, iif(fx.device.isTablet, 30, 25)) --o.x = x + fxAds.bannerWidth/2 --o.y = y + fxAds.bannerHeight/2 elseif fx.device.isApple then ads.show(“banner”, {x=x, y=y, testMode=appInfo.mode == “TESTING”}) elseif fx.device.isAndroid then ads.show(“banner”, {x=x, y=y, testMode=appInfo.mode == “TESTING”}) end end function fxAds.hide() ads.hide() end fxAds.init() — Example to show the ads on the Top fx.ads.showBanner(0, 0, {}) — Example to show the ads on the bottom fx.ads.showBanner(0, display.contentHeight, {srpH = “bottom”})

At last there are 2 example of how to place the ads on the top and the bottom of the screen. I’ve also created a github repository for my framework. You can download the code at: https://github.com/juancsdc/fx.sdk

The framework is still under development, but at least you can use the ads module.

Hope this can help you.

Does this work for landscape orientation?

If not, any idea what should it look like for landscape?

Regards,

Damir.

The example I gave before, or the code on github works both in portrait and landscape. I could not test it in many devices, if it is not working for some device please let me know and I’ll fix it.

I need ads at the bottom (Android).

I see you use this for Android:

y = display.contentHeight

That does not work for me.

Thanks for your quick reply.

In short this is what you are using for android (portrait mode):

if tablet then adY = display.contentHeight - (display.contentWidth / 320 \* 90) else adY = display.contentHeight - (display.contentWidth / 320 \* 50) end &nbsp;

Right?

But this did not work well. I have tried this on Nexus 7 (2013), LG optimus G pro and in both case the ad was way up from the bottom.

Then I modified and used this code:

if table then&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;adY = display.contentHeight - display.screenOriginY - (display.contentWidth / 320 \* 90) else &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;adY = display.contentHeight - display.screenOriginY - (display.contentWidth / 320 \* 50) end &nbsp;

The above code worked well for the devices I mentioned. 

What’s your view on the above code???

I tested with Galaxy Tab & Sony Xperia, and I think tarun9 is right, his modified code works

adY = display.contentHeight - display.screenOriginY - (display.contentWidth / 320 \* 50)

However I am still seeing two problems:

(1) In Sony Xperia, the ad is placed at the exact bottom, which is perfect. However in Galaxy Tab, there is still 3 pixels space under the ad where I can see my background underneath. My config.lua is 320x480, letterbox. Where does this 3-pixels space come from? How to avoid it?

(2) How to determine if Android device is a tablet or not? Should I use this (from this article:  http://www.coronalabs.com/blog/2013/04/30/new-system-info-properties/)

if ( system.getInfo( "androidDisplayWidthInInches" ) \> 5 or system.getInfo( "androidDisplayHeightInInches" ) \> 5 ) then print( "Is tablet" ) end

However, if I use this to decide, my Galaxy Tab 3 is categorized as a tablet (the height is over 6 inches). And if it’s a tablet, the ad height should be 90. But it’s wrong. It’s still 50.

Anyone tested with a real Android tablet? The ad height is 90 or always 50?

The question is where from did the 90 and 50 come from?

Smart-banners for Android in landscape are 32px.