How can I get Admob height (actual height in the stage)

How can I get Admob height (actual height in the stage)

for example

 

I use letterBox scale mode

scale = "letterBox"

How can I get the admob height 

I konw there is a method - 

ads.height()

But it’s not the actual height relative to the stage

let’s say , I need a Rect , the height must be the same as Admob height

How to do it ?

Thanks

Tang

First the scale “letterbox” is supposed to be all lower case. I think we respond to both, but officially it’s all lower case.

Can you provide the height coming from ads.height() and your config.lua?

Rob

Hi Rob 

application = { content = { width = 640, height = 480,  scale = "letterbox", fps = 60, --[[imageSuffix = {    ["@2x"] = 2,    ["@4x"] = 4, }, --]] }, }

here this code : main.lua

local ads\_testMode = true local myID\_banner\_v = "ca-app-pub-XXXXXXXXXXXXXX" local myID\_banner\_h = "ca-app-pub-ZZZZZZZZZZZZZZ" local rect = display.newRect(0,0,400,10) rect.y = 300 rect.x = 200 local function init() ads.init( "admob", myID\_banner\_v, adListener ) showAdmob() &nbsp; --newGame() &nbsp; end function showAdmob() ads.show( "banner", { x=display.screenOriginX, y=99999999, targetingOptions=targetingParams, appId=myID\_banner\_v ,testMode = ads\_testMode} ) end function adListener( event ) print("ad event handler fire!") &nbsp; &nbsp; if ( event.isError ) then &nbsp; &nbsp; &nbsp; &nbsp; print("ad error: type: "..event.type) &nbsp; &nbsp; else if(event.phase =="shown") then&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;print("ads.height() : "..ads.height()) &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;rect.height = ads.height() &nbsp; &nbsp;end&nbsp; &nbsp; &nbsp; end end function resize( event ) local theID local isV = (display.actualContentWidth \<= display.actualContentHeight) &nbsp; &nbsp; if(isV) then&nbsp; &nbsp; &nbsp;&nbsp; theID = myID\_banner\_v &nbsp; &nbsp;&nbsp; print("isV") &nbsp; &nbsp; else &nbsp; &nbsp;&nbsp; theID = myID\_banner\_h &nbsp; &nbsp;&nbsp; print("isH") &nbsp; &nbsp; end &nbsp; &nbsp; ads.show( "banner", { x=display.screenOriginX, y=99999999, targetingOptions=targetingParams, appId=theID ,testMode = ads\_testMode }) end Runtime:addEventListener("resize",resize)

Thanks

Tang

I used this code:

function adListener( event ) print("ad event handler fire!") if ( event.isError ) then print("ad error: type: "..event.type) elseif(event.phase =="shown") then local adHeight = ads.height() print("ads.height() : ", adHeight) rect.height = display.actualContentHeight - adHeight rect.anchorY = 0 rect.y = adHeight end end

And the rectangle fit perfectly.  Now I did a couple of things to make my life easier. First I saved the value of ads.height() to a local variable so I don’t have to keep calling the function over and over.

Next I made the height of the box the actualContentHeight - the adHeight. Since Corona wants to position things based on the center, I would have to try and calculate a new center based on half the height and offset from the center by the height of the ad. Or I could set the anchorY to either 0 and use 0 as the .y, (which I guess would have been simpler) or set .anchorY to 1 and use the adHeight as the bottom most place to position the rectangle.

Rob

Hi Bob

Thanks for your help , but I still can not get it work correctly

first , you might made a mistake

rect.height = display.actualContentHeight - adHeight rect.anchorY = 0 rect.y = adHeight

this line :

rect.y = adHeight 

should be below like ?

rect.y = display.screenOriginY

but after I fix it , it still not works

here is my source code :

https://dl.dropboxusercontent.com/u/29345465/imgs/AdmobTest.zip

if I set config.lua

width = 600, height = 800,&nbsp;

here is what I get (see screenshoots)

portrait : (not fix :( )

600x800_v.jpg

landscape :  (seems it works on landscape   :smiley: , I don’t know it’s a coincidence or it’s prospective)

600x800_h.jpg

if I set config.lua (width and height changed)

width = 500, height = 1600,&nbsp;

or

width = 500, height = 1500,&nbsp;

here is what I get (see screenshoots)

portrait :  (fit perfectly. :lol:  , but after I rotate the phone to landscape , will not fit <_< )

500x1600_v.jpg

landscape : (not fix)

500x1600_h.jpg

here is my source code :

https://dl.dropboxusercontent.com/u/29345465/imgs/AdmobTest.zip

rect.png :

rect.png

main.lua :

----- coronasdk version : 2016.2906 ----------------------------- ----- test device : iphone 6s , ipad mini 3---------------------- ----- admob para ------------------------------------------------ display.setDefault("background", 1,0/255, 0/255) local ads = require( "ads" ) local ads\_testMode = true local myID = "ca-app-pub-9829826968851971/0123456789" local targetingParams = { tagForChildDirectedTreatment = true } ----- load rect.png (200x200) ----------------------------------- local rect = display.newImage("rect.png") rect.anchorX = 0 rect.anchorY = 0 rect.x = display.screenOriginX rect.y = display.screenOriginY print(rect.height) ----- output 200 -------- admob event listener ----------------------------------- function adListener( event ) &nbsp; &nbsp; print("ad event handler fire!") &nbsp; &nbsp; if ( event.isError ) then &nbsp; &nbsp; &nbsp; &nbsp; print("ad error: type: "..event.type) &nbsp; &nbsp; elseif(event.phase =="shown") then&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; local adHeight = ads.height() &nbsp; &nbsp; &nbsp; &nbsp; print("ads.height() : ", adHeight) &nbsp; &nbsp; &nbsp; &nbsp; rect.anchorX = 0 &nbsp; &nbsp; &nbsp; &nbsp; rect.anchorY = 0 &nbsp; &nbsp; &nbsp; &nbsp; rect.x = display.screenOriginX &nbsp; &nbsp; &nbsp; &nbsp; rect.y = display.screenOriginY &nbsp; &nbsp; &nbsp; &nbsp; rect.height = display.actualContentHeight - adHeight &nbsp; &nbsp; &nbsp; &nbsp; end end -------initialize admob then show the banner ad ----------------- ads.init( "admob", myID, adListener ) -------I set the banner x to 100 , so I can if the rect perfectly fit ----- ads.show( "banner", { x=display.screenOriginX + 100, y=99999999, targetingOptions=targetingParams, appId=myID ,testMode = ads\_testMode} ) -------- resize , call after orientation changed ----- function resize( event ) &nbsp; &nbsp; print("resize , show admob banner") &nbsp; &nbsp; -------I set the banner x to 100 , so I can if the rect perfectly fit ----- &nbsp; &nbsp; ads.show( "banner", { x=display.screenOriginX+100, y=99999999, targetingOptions=targetingParams, appId=myID ,testMode = ads\_testMode }) end Runtime:addEventListener("resize",resize) -------- end that' all-----------------------------------------

Thanks 

Tang

After several times of test (accurately demonic test), I found the height of admob is scaled while the width and height is not the same as actual width and height

I’m not sure if this is a bug or carelessness or prospective

anyway  I found a solution

use ads.height() multiply (display.actualContentHeight/display.contentHeight)

like so :

ads.height() &nbsp;\* display.actualContentHeight / display.contentHeight

Finally,  fit perfectly  now  :slight_smile:

Thanks again Rob for your help

Tang

First the scale “letterbox” is supposed to be all lower case. I think we respond to both, but officially it’s all lower case.

Can you provide the height coming from ads.height() and your config.lua?

Rob

Hi Rob 

application = { content = { width = 640, height = 480,&nbsp; scale = "letterbox", fps = 60, --[[imageSuffix = { &nbsp; &nbsp;["@2x"] = 2, &nbsp; &nbsp;["@4x"] = 4, }, --]] }, }

here this code : main.lua

local ads\_testMode = true local myID\_banner\_v = "ca-app-pub-XXXXXXXXXXXXXX" local myID\_banner\_h = "ca-app-pub-ZZZZZZZZZZZZZZ" local rect = display.newRect(0,0,400,10) rect.y = 300 rect.x = 200 local function init() ads.init( "admob", myID\_banner\_v, adListener ) showAdmob() &nbsp; --newGame() &nbsp; end function showAdmob() ads.show( "banner", { x=display.screenOriginX, y=99999999, targetingOptions=targetingParams, appId=myID\_banner\_v ,testMode = ads\_testMode} ) end function adListener( event ) print("ad event handler fire!") &nbsp; &nbsp; if ( event.isError ) then &nbsp; &nbsp; &nbsp; &nbsp; print("ad error: type: "..event.type) &nbsp; &nbsp; else if(event.phase =="shown") then&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;print("ads.height() : "..ads.height()) &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;rect.height = ads.height() &nbsp; &nbsp;end&nbsp; &nbsp; &nbsp; end end function resize( event ) local theID local isV = (display.actualContentWidth \<= display.actualContentHeight) &nbsp; &nbsp; if(isV) then&nbsp; &nbsp; &nbsp;&nbsp; theID = myID\_banner\_v &nbsp; &nbsp;&nbsp; print("isV") &nbsp; &nbsp; else &nbsp; &nbsp;&nbsp; theID = myID\_banner\_h &nbsp; &nbsp;&nbsp; print("isH") &nbsp; &nbsp; end &nbsp; &nbsp; ads.show( "banner", { x=display.screenOriginX, y=99999999, targetingOptions=targetingParams, appId=theID ,testMode = ads\_testMode }) end Runtime:addEventListener("resize",resize)

Thanks

Tang

I used this code:

function adListener( event ) print("ad event handler fire!") if ( event.isError ) then print("ad error: type: "..event.type) elseif(event.phase =="shown") then local adHeight = ads.height() print("ads.height() : ", adHeight) rect.height = display.actualContentHeight - adHeight rect.anchorY = 0 rect.y = adHeight end end

And the rectangle fit perfectly.  Now I did a couple of things to make my life easier. First I saved the value of ads.height() to a local variable so I don’t have to keep calling the function over and over.

Next I made the height of the box the actualContentHeight - the adHeight. Since Corona wants to position things based on the center, I would have to try and calculate a new center based on half the height and offset from the center by the height of the ad. Or I could set the anchorY to either 0 and use 0 as the .y, (which I guess would have been simpler) or set .anchorY to 1 and use the adHeight as the bottom most place to position the rectangle.

Rob

Hi Bob

Thanks for your help , but I still can not get it work correctly

first , you might made a mistake

rect.height = display.actualContentHeight - adHeight rect.anchorY = 0 rect.y = adHeight

this line :

rect.y = adHeight 

should be below like ?

rect.y = display.screenOriginY

but after I fix it , it still not works

here is my source code :

https://dl.dropboxusercontent.com/u/29345465/imgs/AdmobTest.zip

if I set config.lua

width = 600, height = 800,&nbsp;

here is what I get (see screenshoots)

portrait : (not fix :( )

600x800_v.jpg

landscape :  (seems it works on landscape   :smiley: , I don’t know it’s a coincidence or it’s prospective)

600x800_h.jpg

if I set config.lua (width and height changed)

width = 500, height = 1600,&nbsp;

or

width = 500, height = 1500,&nbsp;

here is what I get (see screenshoots)

portrait :  (fit perfectly. :lol:  , but after I rotate the phone to landscape , will not fit <_< )

500x1600_v.jpg

landscape : (not fix)

500x1600_h.jpg

here is my source code :

https://dl.dropboxusercontent.com/u/29345465/imgs/AdmobTest.zip

rect.png :

rect.png

main.lua :

----- coronasdk version : 2016.2906 ----------------------------- ----- test device : iphone 6s , ipad mini 3---------------------- ----- admob para ------------------------------------------------ display.setDefault("background", 1,0/255, 0/255) local ads = require( "ads" ) local ads\_testMode = true local myID = "ca-app-pub-9829826968851971/0123456789" local targetingParams = { tagForChildDirectedTreatment = true } ----- load rect.png (200x200) ----------------------------------- local rect = display.newImage("rect.png") rect.anchorX = 0 rect.anchorY = 0 rect.x = display.screenOriginX rect.y = display.screenOriginY print(rect.height) ----- output 200 -------- admob event listener ----------------------------------- function adListener( event ) &nbsp; &nbsp; print("ad event handler fire!") &nbsp; &nbsp; if ( event.isError ) then &nbsp; &nbsp; &nbsp; &nbsp; print("ad error: type: "..event.type) &nbsp; &nbsp; elseif(event.phase =="shown") then&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; local adHeight = ads.height() &nbsp; &nbsp; &nbsp; &nbsp; print("ads.height() : ", adHeight) &nbsp; &nbsp; &nbsp; &nbsp; rect.anchorX = 0 &nbsp; &nbsp; &nbsp; &nbsp; rect.anchorY = 0 &nbsp; &nbsp; &nbsp; &nbsp; rect.x = display.screenOriginX &nbsp; &nbsp; &nbsp; &nbsp; rect.y = display.screenOriginY &nbsp; &nbsp; &nbsp; &nbsp; rect.height = display.actualContentHeight - adHeight &nbsp; &nbsp; &nbsp; &nbsp; end end -------initialize admob then show the banner ad ----------------- ads.init( "admob", myID, adListener ) -------I set the banner x to 100 , so I can if the rect perfectly fit ----- ads.show( "banner", { x=display.screenOriginX + 100, y=99999999, targetingOptions=targetingParams, appId=myID ,testMode = ads\_testMode} ) -------- resize , call after orientation changed ----- function resize( event ) &nbsp; &nbsp; print("resize , show admob banner") &nbsp; &nbsp; -------I set the banner x to 100 , so I can if the rect perfectly fit ----- &nbsp; &nbsp; ads.show( "banner", { x=display.screenOriginX+100, y=99999999, targetingOptions=targetingParams, appId=myID ,testMode = ads\_testMode }) end Runtime:addEventListener("resize",resize) -------- end that' all-----------------------------------------

Thanks 

Tang

After several times of test (accurately demonic test), I found the height of admob is scaled while the width and height is not the same as actual width and height

I’m not sure if this is a bug or carelessness or prospective

anyway  I found a solution

use ads.height() multiply (display.actualContentHeight/display.contentHeight)

like so :

ads.height() &nbsp;\* display.actualContentHeight / display.contentHeight

Finally,  fit perfectly  now  :slight_smile:

Thanks again Rob for your help

Tang