Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

admob positioning problem
Started by sparrowdrunken Aug 15 2013 07:17 AM

* * * * * 5 votes
68 replies to this topic
admob problem positioning zoomeven android
[TOPIC CONTROLS]
Page 1 of 3 1 2 3
This topic has been archived. This means that you cannot reply to this topic.
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

sparrowdrunken

[GLOBAL: userInfoPane.html]
sparrowdrunken
  • Observer

  • 7 posts
  • Corona SDK

   Warning: sorry for my English. Didnt find admob forum in plugins section, so ask here.

 

Here the problem - I successfully integrate admob into my app, but i cant position it well for different android devices.

 

My config.lua

application = {
	content = {
		width = 780,
		height = 1024, 
		scale = "zoomEven",
		fps = 60,
	},   
}

Yes, I use zoomEven, cause dont like any croping.

 

Here my build.setting

-- Supported values for orientation:
-- portrait, portraitUpsideDown, landscapeLeft, landscapeRight

settings = {
	
	orientation = {
		default = "portrait",
		supported = { "portrait", }
	},
	
	iphone = {
		plist = {
			UIStatusBarHidden = false,
			UIPrerenderedIcon = true, -- set to false for "shine" overlay
			--UIApplicationExitsOnSuspend = true, -- uncomment to quit app on suspend

            --[[
            -- iOS app URL schemes:
            CFBundleURLTypes =
            {
                {
                    CFBundleURLSchemes =
                    {
                        "fbXXXXXXXXXXXXXX", -- example scheme for facebook
                        "coronasdkapp", -- example second scheme
                    }
                }
            }
            --]]
		}
	},
	
	-- Android permissions

	androidPermissions = {
  		"android.permission.INTERNET",
         "android.permission.ACCESS_NETWORK_STATE",
         "android.permission.READ_PHONE_STATE",
  	},
	
    plugins =
    {
        -- key is the name passed to Lua's 'require()'
        ["CoronaProvider.ads.admob"] =
        {
            -- required
            publisherId = "com.coronalabs",
        },
    },  
	
}

 

For this app I want banner ad in the bottom of the screen. I read somewhere, that corona supports only smart banners for admob. 

From here https://developers.google.com/mobile-ads-sdk/docs/admob/smart-banners  I learn that there are only 50 and 90 pixel heights for portait orientered apps for any android device. 90 for tablets and 50 for others. 

So, for correct positioning, I need to know if the device is a tablet or phone, then convert my config pixels into real ones.

ads.show( "banner", { x = display.screenOriginX , y = display.screenOriginY + display.viewableContentHeight - adsDy } )

Where adsDy - height of banner in real size. To get it i use next code:


local adsDy = 0
if  system.getInfo( "androidDisplayWidthInInches" ) then
	if( system.getInfo( "androidDisplayWidthInInches" ) > 5 or
		system.getInfo( "androidDisplayHeightInInches" ) > 5 ) then
		--is a tablet
		adsDy = 90
	else
		adsDy = 50
	end

	adsDy = adsDy * ( display.viewableContentHeight / (system.getInfo("androidDisplayYDpi") * system.getInfo( "androidDisplayHeightInInches" ))  )
	
end

(system.getInfo("androidDisplayYDpi") * system.getInfo( "androidDisplayHeightInInches" ))  - this must return the real pixel height of device. But for my asus tpad 300 it returns 1232 instead of 1280. Or it doesnt count the navigation bar and it's 48 pix in height?

 

Anyway, with this way I can get good positioning on Asus TPad 300 and it begins from left side of screen, but it's a little wider than the screen. But Galaxy SIV shows it very bad:


vpngF.jpg

 

It seems about 33% of real height of banner and 80% of its width.

 

I try to solve this case 2 days but have no idea yet. Searching on corona forums doesnt give a result also.

 

I would be glad to hear any ideas and solutions!



[TOPIC: post.html]
#2

sparrowdrunken

[GLOBAL: userInfoPane.html]
sparrowdrunken
  • Observer

  • 7 posts
  • Corona SDK

lets up it



[TOPIC: post.html]
#3

bjsorrentino

[GLOBAL: userInfoPane.html]
bjsorrentino
  • Veteran

  • 8,506 posts
  • Corona SDK

Hi @sparrowdrunken,

 

In your screenshot, is the banner ad the grey+lime thing on the button, where I can see "FRE" on the right side?

 

If I understand the issue, your content area is bleeding off the side edges on one of these devices, because you use ZoomEven? That is probably why the ad is larger than the screen in some cases.

 

You may want to approach the positioning differently, instead of using the "DPI" and "InInches" fetching info. I believe that the nav bar space is "not always" regarded on Android devices, which leads to unpredictable results.

 

I know this isn't a "solution" in itself, but perhaps you can calculate the positioning without using the system.getInfo APIs, and instead base it off the content area.

 

Out of curiosity, what are the values returned when you simply print these to the Terminal/console?

 

print( display.screenOriginX )
print( display.screenOriginY )

 

Thanks,

Brent



[TOPIC: post.html]
#4

ubj3d.android

[GLOBAL: userInfoPane.html]
ubj3d.android
  • Contributor

  • 741 posts
  • Corona SDK

Have the same problem, positioning the ad at the bottom of the screen (portrait orientation).

 

Guess lots of you use admob and position the banner at the bottom.

 

Anyone would be so kind to share his solution ?

 

Regards,

Damir.



[TOPIC: post.html]
#5

juan.csdc

[GLOBAL: userInfoPane.html]
juan.csdc
  • Enthusiast

  • 69 posts
  • Corona SDK

Yep, same issue here, for iAds I could figure it out, for for some reason on Android nothing seems to be working.



[TOPIC: post.html]
#6

Rob Miracle

[GLOBAL: userInfoPane.html]
Rob Miracle
  • Moderator

  • 25,786 posts
  • Enterprise

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



[TOPIC: post.html]
#7

juan.csdc

[GLOBAL: userInfoPane.html]
juan.csdc
  • Enthusiast

  • 69 posts
  • Corona SDK

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.



[TOPIC: post.html]
#8

Rob Miracle

[GLOBAL: userInfoPane.html]
Rob Miracle
  • Moderator

  • 25,786 posts
  • Enterprise

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.



[TOPIC: post.html]
#9

juan.csdc

[GLOBAL: userInfoPane.html]
juan.csdc
  • Enthusiast

  • 69 posts
  • Corona SDK

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,
      },
   },
}


[TOPIC: post.html]
#10

ubj3d.android

[GLOBAL: userInfoPane.html]
ubj3d.android
  • Contributor

  • 741 posts
  • Corona SDK

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.



[TOPIC: post.html]
#11

Rob Miracle

[GLOBAL: userInfoPane.html]
Rob Miracle
  • Moderator

  • 25,786 posts
  • Enterprise

@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.



[TOPIC: post.html]
#12

ubj3d.android

[GLOBAL: userInfoPane.html]
ubj3d.android
  • Contributor

  • 741 posts
  • Corona SDK

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.



[TOPIC: post.html]
#13

Rob Miracle

[GLOBAL: userInfoPane.html]
Rob Miracle
  • Moderator

  • 25,786 posts
  • Enterprise

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



[TOPIC: post.html]
#14

juan.csdc

[GLOBAL: userInfoPane.html]
juan.csdc
  • Enthusiast

  • 69 posts
  • Corona SDK

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?



[TOPIC: post.html]
#15

Rob Miracle

[GLOBAL: userInfoPane.html]
Rob Miracle
  • Moderator

  • 25,786 posts
  • Enterprise

Potentially.  Probably.



[TOPIC: post.html]
#16

juan.csdc

[GLOBAL: userInfoPane.html]
juan.csdc
  • Enthusiast

  • 69 posts
  • Corona SDK

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 =)



[TOPIC: post.html]
#17

joe528

[GLOBAL: userInfoPane.html]
joe528
  • Contributor

  • 800 posts
  • Corona SDK

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?



[TOPIC: post.html]
#18

tarun9

[GLOBAL: userInfoPane.html]
tarun9
  • Observer

  • 9 posts
  • Corona SDK

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 =)

 

 

@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?



[TOPIC: post.html]
#19

juan.csdc

[GLOBAL: userInfoPane.html]
juan.csdc
  • Enthusiast

  • 69 posts
  • Corona SDK

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.



[TOPIC: post.html]
#20

ubj3d.android

[GLOBAL: userInfoPane.html]
ubj3d.android
  • Contributor

  • 741 posts
  • Corona SDK

Does this work for landscape orientation?

 

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

 

Regards,

Damir.



[TOPIC: post.html]
#21

juan.csdc

[GLOBAL: userInfoPane.html]
juan.csdc
  • Enthusiast

  • 69 posts
  • Corona SDK

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.



[TOPIC: post.html]
#22

ubj3d.android

[GLOBAL: userInfoPane.html]
ubj3d.android
  • Contributor

  • 741 posts
  • Corona SDK

I need ads at the bottom (Android).

 

I see you use this for Android:

 

y = display.contentHeight

 

That does not work for me.



[TOPIC: post.html]
#23

tarun9

[GLOBAL: userInfoPane.html]
tarun9
  • Observer

  • 9 posts
  • Corona SDK

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
 
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     
        adY = display.contentHeight - display.screenOriginY - (display.contentWidth / 320 * 90)
else
        adY = display.contentHeight - display.screenOriginY - (display.contentWidth / 320 * 50)
end
 

 

The above code worked well for the devices I mentioned. 

 

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



[TOPIC: post.html]
#24

joe528

[GLOBAL: userInfoPane.html]
joe528
  • Contributor

  • 800 posts
  • Corona SDK

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?



[TOPIC: post.html]
#25

ubj3d.android

[GLOBAL: userInfoPane.html]
ubj3d.android
  • Contributor

  • 741 posts
  • Corona SDK

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

 

Smart-banners for Android in landscape are 32px.




[topic_controls]
Page 1 of 3 1 2 3
 
[/topic_controls]