# Got Admob working for Android :)

**URL:** <https://forums.solar2d.com/t/got-admob-working-for-android/288560>\
**Category:** Android\
**Created:** [March 19, 2011, 7:12pm UTC](https://forums.solar2d.com/t/got-admob-working-for-android/288560 "2011-03-19T19:12:08Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![kam187](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/k/b9bd4f/32.png) [@kam187](https://forums.solar2d.com/u/kam187)\
**Post date:** [March 19, 2011, 7:12pm UTC](https://forums.solar2d.com/t/got-admob-working-for-android/288560/1 "2011-03-19T19:12:08Z")

</div>

EDIT: Added HoneyComb UA fix (check android ad htmls  
EDIT: Change made for latest corona.

So, I saw alot of complaints about this and people really strugling with it. I finally found a bit of time looking at it.

I tested this on Corona 243, and daily 317. Other versions may have issues I don’t know.

EDIT: This has developed to a completely working solution on all devices! Instead of leaving the code fragmented across posts I’ve moved EVERYTHING you need to this first post:

Tested on Corona upto latest daily, and also including daily 319 for android.

Step 1)

You MUST add the internet permissions to the build.settings file. For the sake of completness here’s a build.settings file that you can drop in place:

```auto
settings = {  
 orientation =  
 {  
 default = "portrait",  
 supported =  
 {  
 "portrait",  
 },  
 },  
 androidPermissions =  
 {  
 "android.permission.INTERNET",  
 "android.permission.ACCESS\_NETWORK\_STATE"  
 },  
}  
```

Step 2) Sign up for admob and create a new ‘site’. Pick Android to get an android id - you’ll use this in step 3. Create a second site and pick ‘iphone’ to get an apple id.

Ignore the sdk download, as you can’t use it with corona. Edit the site and set the refresh rate to 20 seconds (this ensures an ad is refreshed on their side ready for when you http refresh your page).

Step 3) Create the following files from the code below:

main.lua - add this code to your main.lua and use DisplayAd(delay) to show and ad after ‘delay’ milliseconds. Use RemoveAd(delay) to remove it after delay milliseconds.

android\_ad.html - This is the html file for showing ads on all android devices with zoom level \<= 1.5. Change the admob id to the one you received when you created the '‘site’ in admob.

android\_ad15.html - This is the html file for showing ads on all android devices with a zoom level \> 1.5. Change the admob id to the one you received when you created the '‘site’ in admob. It should be the same one as above.

apple\_ad.html - This handled ALL apple devices. I suggest you create a SECOND site in admob and use a seperate id for this, so you can see how much revenue you are generating from apple vs android.

You should also change the ‘test:’ line to ‘false’ when you want to see real ads.

– main.lua

```auto
local adSpace  
local isAndroid = "Android" == system.getInfo("platformName")  
  
function showAd\_Android(event)  
 -- Is the url a remote call?  
 if string.find(event.url, "android\_ad.html", 1, false) or string.find(event.url, "android\_ad15.html", 1, false) then  
 return true  
 else  
 system.openURL(string.gsub (event.url, "Corona:", ""))  
 -- Refresh ad  
 RemoveAd(0)  
 DisplayAd(0)  
 return true  
 end  
end  
  
local function showAd\_Apple(event)  
 -- Is the url a remote call?  
 if string.find(event.url, "http://", 1, false) == 1 then  
 -- Is it a call to the admob server?  
 if string.find(event.url, "c.admob.com", 1, false) == nil then  
 adSpace.url = event.url  
 else  
 -- an actual click on an ad, so open in Safari  
 system.openURL(event.url)  
 -- Refresh ad  
 RemoveAd(0)  
 DisplayAd(0)  
 end  
 else  
 -- Feb 1, 2011: if using the old version of this code, remove the  
 -- following line:  
 -- adSpace.url = event.url  
 -- and put in this line instead:  
 return true  
 end  
end  
  
function DisplayAd(t)  
 native.cancelWebPopup()  
 timer.performWithDelay(t, function()  
 local adfile = "apple\_ad.html"  
 local sizeX = 320  
 local sizeY = 48  
 local scale = 1/display.contentScaleY  
  
 if isAndroid then  
 if scale \> 1.5 then   
 adfile = "android\_ad15.html"  
 sizeX = sizeX\*(1.5/scale) + 1  
 sizeY = sizeY\*(1.5/scale) + 1  
 else  
 adfile = "android\_ad.html"  
 end  
 adSpace = native.showWebPopup((display.contentWidth - sizeX)/2, display.contentHeight - display.screenOriginY - sizeY, sizeX, sizeY, adfile, {baseUrl = system.ResourceDirectory, hasBackground = false, urlRequest = showAd\_Android})  
 elseif system.getInfo( "environment" ) == "simulator" then  
 adSpace = display.newRect( (display.contentWidth - sizeX)/2, display.contentHeight - display.screenOriginY - sizeY, sizeX, sizeY )  
 display.getCurrentStage():insert(adSpace, false)  
 else  
 if scale \> 1.5 then   
 sizeX = sizeX\*(1.5/scale) + 1  
 sizeY = sizeY\*(1.5/scale) + 1  
 end  
 adSpace = native.showWebPopup((display.contentWidth - sizeX)/2, display.contentHeight - display.screenOriginY - sizeY, sizeX, sizeY, adfile, {baseUrl = system.ResourceDirectory, hasBackground = false, urlRequest = showAd\_Apple})  
 end  
 end  
 )  
end  
  
function RemoveAd(t)  
 timer.performWithDelay(t, native.cancelWebPopup)  
 timer.performWithDelay(t, function() adSpace:removeSelf() end)  
end  
```

## android\_ad.html

```auto
br\> "http://www.w3.org/TR/html4/loose.dtd"\>  
  
  
  
<meta name="viewport" content="width=320; user-scalable=0;">  
<meta http-equiv="Refresh" content="30">

<title>ad</title>
  
  
  
<script type="text/javascript"><br>	var admob_vars = {<br> pubid: '000000000000000', // publisher id - FILL IN YOUR OWN ID HERE!!<br> bgcolor: '000000', // background color (hex)<br> text: 'FFFFFF', // font-color (hex)<br> ama: false, // set to true and retain comma for the AdMob Adaptive Ad Unit, a special ad type designed for PC sites accessed from the iPhone. More info: http://developer.admob.com/wiki/IPhone#Web_Integration<br> test: false // test mode, set to false to receive live ads<br>	};<br>	</script>  
<script type="text/javascript"><br> var userAgentTemp = navigator.userAgent;<br> if (!(RegExp("Android (1|2).").test(userAgentTemp))) {<br> navigator = {}<br> if (userAgentTemp.search(/Android/) != -1) {<br> navigator.userAgent = userAgentTemp.replace(/Android 3[&#092;.&#092;d]*/, "Android 2.3.3");<br> } else {<br> navigator.userAgent = userAgentTemp + "; Android 2.3.3";<br> };<br> };<br> </script>  
<script type="text/javascript" src="http://mmv.admob.com/static/iphone/iadmob.js"></script>  
<script type="text/javascript"><br> _admob.gotourl = function (V, W) {<br> document.location = "Corona:" + V;<br> return true;<br> };<br>	</script>  
  
[/html]  
```

## android\_ad15.html

```auto
br\> "http://www.w3.org/TR/html4/loose.dtd"\>  
  
  
  
<meta name="viewport" content="width=320; initial-scale=1.5; minimum-scale=1.5; maximum-scale=1.5; user-scalable=0;">
  
<meta http-equiv="Refresh" content="30">
  
<title>ad</title>
  
  
  
<script type="text/javascript"><br>	var admob_vars = {<br> pubid: '000000000000000', // publisher id - FILL IN YOUR OWN ID HERE!!<br> bgcolor: '000000', // background color (hex)<br> text: 'FFFFFF', // font-color (hex)<br> ama: false, // set to true and retain comma for the AdMob Adaptive Ad Unit, a special ad type designed for PC sites accessed from the iPhone. More info: http://developer.admob.com/wiki/IPhone#Web_Integration<br> test: false // test mode, set to false to receive live ads<br>	};<br>	</script>  
<script type="text/javascript"><br> var userAgentTemp = navigator.userAgent;<br> if (!(RegExp("Android (1|2).").test(userAgentTemp))) {<br> navigator = {}<br> if (userAgentTemp.search(/Android/) != -1) {<br> navigator.userAgent = userAgentTemp.replace(/Android 3[&#092;.&#092;d]*/, "Android 2.3.3");<br> } else {<br> navigator.userAgent = userAgentTemp + "; Android 2.3.3";<br> };<br> };<br> </script>  
<script type="text/javascript" src="http://mmv.admob.com/static/iphone/iadmob.js"></script>  
<script type="text/javascript"><br> _admob.gotourl = function (V, W) {<br> document.location = "Corona:" + V;<br> return true;<br> };<br>	</script>  
  
[/html]  
```

## apple\_ad.html

```auto
br\> "http://www.w3.org/TR/html4/loose.dtd"\>  
  
  
  
<meta name="viewport" content="width=320; user-scalable=0;">
  
<meta http-equiv="Refresh" content="30">
  
<title>ad</title>
  
  
  
<script type="text/javascript"><br>	var admob_vars = {<br> pubid: '000000000000000', // publisher id - FILL IN YOUR OWN ID HERE!!<br> bgcolor: '000000', // background color (hex)<br> text: 'FFFFFF', // font-color (hex)<br> test: false // test mode, set to false to receive live ads<br>	};<br>	</script>  
<script type="text/javascript" src="http://mmv.admob.com/static/iphone/iadmob.js"></script>  
  
[/html]  
```

[import]uid: 8872 topic\_id: 8021 reply\_id: 308021[/import]

---

<div class="post-metadata">

**Author:** ![rxmarccall](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/r/13edae/32.png) [@rxmarccall](https://forums.solar2d.com/u/rxmarccall)\
**Post date:** [March 20, 2011, 3:24pm UTC](https://forums.solar2d.com/t/got-admob-working-for-android/288560/2 "2011-03-20T15:24:42Z")

</div>

Thanks for the tutorial, i will be giving it a shot once my app gets close to release. ill report how it goes. [import]uid: 19620 topic\_id: 8021 reply\_id: 28651[/import]

---

<div class="post-metadata">

**Author:** ![maxbarrow](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/m/22d042/32.png) [@maxbarrow](https://forums.solar2d.com/u/maxbarrow)\
**Post date:** [March 20, 2011, 10:07pm UTC](https://forums.solar2d.com/t/got-admob-working-for-android/288560/3 "2011-03-20T22:07:10Z")

</div>

Thanks dude this seems to have nailed it. Now if only corona would fix multi-touch (only two touches allowed at once on android) , openAL and most of the other stuff that doesnt work on android! [import]uid: 11459 topic\_id: 8021 reply\_id: 28703[/import]

---

<div class="post-metadata">

**Author:** ![kam187](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/k/b9bd4f/32.png) [@kam187](https://forums.solar2d.com/u/kam187)\
**Post date:** [March 21, 2011, 11:40am UTC](https://forums.solar2d.com/t/got-admob-working-for-android/288560/4 "2011-03-21T11:40:00Z")

</div>

Multi touch on android is a bit of a mess tho actually. Different devices support different things in their driver. Some only handle two touches and anything more is a ‘palm’ touch. [import]uid: 8872 topic\_id: 8021 reply\_id: 28793[/import]

---

<div class="post-metadata">

**Author:** ![AlenB](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/a/ecccb3/32.png) [@AlenB](https://forums.solar2d.com/u/AlenB)\
**Post date:** [March 23, 2011, 11:04am UTC](https://forums.solar2d.com/t/got-admob-working-for-android/288560/5 "2011-03-23T11:04:14Z")

</div>

Hi kam187,

it works … thx for sharing…  
it opens in a new webpopup!

Did you use it in any android app? [import]uid: 9592 topic\_id: 8021 reply\_id: 29200[/import]

---

<div class="post-metadata">

**Author:** ![bruno\_gallien](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/b/4bbf92/32.png) [@bruno\_gallien](https://forums.solar2d.com/u/bruno_gallien)\
**Post date:** [March 26, 2011, 12:47pm UTC](https://forums.solar2d.com/t/got-admob-working-for-android/288560/6 "2011-03-26T12:47:18Z")

</div>

It works fine but it is very, very slow.  
And the function is never called.

Bruno from [http://blueglutton.com](http://blueglutton.com) [import]uid: 9097 topic\_id: 8021 reply\_id: 29613[/import]

---

<div class="post-metadata">

**Author:** ![kam187](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/k/b9bd4f/32.png) [@kam187](https://forums.solar2d.com/u/kam187)\
**Post date:** [March 26, 2011, 1:29pm UTC](https://forums.solar2d.com/t/got-admob-working-for-android/288560/7 "2011-03-26T13:29:54Z")

</div>

alen.brkicic8: Not yet. I was going to add it to the lite version of Walkabout but decided not to just yet.

bruno\_gallien: It works fine here, and definatly calls the function. What version of corona do you use? and which OS version of android do you build for?  
[import]uid: 8872 topic\_id: 8021 reply\_id: 29624[/import]

---

<div class="post-metadata">

**Author:** ![bruno\_gallien](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/b/4bbf92/32.png) [@bruno\_gallien](https://forums.solar2d.com/u/bruno_gallien)\
**Post date:** [March 26, 2011, 3:02pm UTC](https://forums.solar2d.com/t/got-admob-working-for-android/288560/8 "2011-03-26T15:02:15Z")

</div>

Hi,  
I use the last daily build and I build for 2.2 with a 2.3.2 phone.

Bruno from [http://blueglutton.com](http://blueglutton.com) [import]uid: 9097 topic\_id: 8021 reply\_id: 29642[/import]

---

<div class="post-metadata">

**Author:** ![bruno\_gallien](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/b/4bbf92/32.png) [@bruno\_gallien](https://forums.solar2d.com/u/bruno_gallien)\
**Post date:** [March 28, 2011, 4:57am UTC](https://forums.solar2d.com/t/got-admob-working-for-android/288560/9 "2011-03-28T04:57:23Z")

</div>

My bad I discovered I had a typo.  
But now the function is called even if i don’t click on the banner and in addition the banner is not appearing.

Bruno from [http://blueglutton.com](http://blueglutton.com) [import]uid: 9097 topic\_id: 8021 reply\_id: 29802[/import]

---

<div class="post-metadata">

**Author:** ![khanh.dq](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/k/ad7895/32.png) [@khanh.dq](https://forums.solar2d.com/u/khanh.dq)\
**Post date:** [March 28, 2011, 6:41am UTC](https://forums.solar2d.com/t/got-admob-working-for-android/288560/10 "2011-03-28T06:41:09Z")

</div>

Hi bruno\_gallien, what is your device name? Could you post your project here? [import]uid: 9190 topic\_id: 8021 reply\_id: 29814[/import]

---

<div class="post-metadata">

**Author:** ![kam187](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/k/b9bd4f/32.png) [@kam187](https://forums.solar2d.com/u/kam187)\
**Post date:** [March 28, 2011, 10:39am UTC](https://forums.solar2d.com/t/got-admob-working-for-android/288560/11 "2011-03-28T10:39:06Z")

</div>

Remember the ad.html has to be called exactly that. This line here checks if its loading the ad.html or its a click:

if not string.find(event.url, “ad.html”, 1, false) then

If you change the name of your ad.html you need to change this string too. If you renamed the file it will think there was a click as soon as the ad loads. [import]uid: 8872 topic\_id: 8021 reply\_id: 29842[/import]

---

<div class="post-metadata">

**Author:** ![bruno\_gallien](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/b/4bbf92/32.png) [@bruno\_gallien](https://forums.solar2d.com/u/bruno_gallien)\
**Post date:** [March 28, 2011, 10:56am UTC](https://forums.solar2d.com/t/got-admob-working-for-android/288560/12 "2011-03-28T10:56:15Z")

</div>

In fact there’s something I don’t understand : should the function be called only if I click or is it also used to get the banner ?

Bruno from [http://blueglutton.com](http://blueglutton.com) [import]uid: 9097 topic\_id: 8021 reply\_id: 29846[/import]

---

<div class="post-metadata">

**Author:** ![kam187](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/k/b9bd4f/32.png) [@kam187](https://forums.solar2d.com/u/kam187)\
**Post date:** [March 28, 2011, 10:57am UTC](https://forums.solar2d.com/t/got-admob-working-for-android/288560/13 "2011-03-28T10:57:53Z")

</div>

Its called when the page loads (ad.html) to display the banner. and ALSO if its clicked.

That line of code checks if the url is the ad page. This is how it knows if the ad page is loading, or its been clicked.

You should use the code template I posted. If you modified it or removed/renamed the file it wont work. [import]uid: 8872 topic\_id: 8021 reply\_id: 29847[/import]

---

<div class="post-metadata">

**Author:** ![bruno\_gallien](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/b/4bbf92/32.png) [@bruno\_gallien](https://forums.solar2d.com/u/bruno_gallien)\
**Post date:** [March 28, 2011, 11:12am UTC](https://forums.solar2d.com/t/got-admob-working-for-android/288560/14 "2011-03-28T11:12:10Z")

</div>

Ok so I messed up somewhere because it was loading the banner.

Bruno from [http://blueglutton.com](http://blueglutton.com) [import]uid: 9097 topic\_id: 8021 reply\_id: 29856[/import]

---

<div class="post-metadata">

**Author:** ![Daniel\_W](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/d/8dc957/32.png) [@Daniel\_W](https://forums.solar2d.com/u/Daniel_W)\
**Post date:** [April 7, 2011, 9:04pm UTC](https://forums.solar2d.com/t/got-admob-working-for-android/288560/15 "2011-04-07T21:04:18Z")

</div>

I got the ad working, but it seems very slow. It looks like bruno\_gallien is experiencing the same problem. Any ideas as to why it’s slow? [import]uid: 14218 topic\_id: 8021 reply\_id: 31177[/import]

---

<div class="post-metadata">

**Author:** ![joshdobbs](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/j/51bf81/32.png) [@joshdobbs](https://forums.solar2d.com/u/joshdobbs)\
**Post date:** [April 20, 2011, 7:14pm UTC](https://forums.solar2d.com/t/got-admob-working-for-android/288560/16 "2011-04-20T19:14:31Z")

</div>

Thanks for the example…i got it working but now im wondering if there is a way to remove the banner or if theres a way to add it to a group. [import]uid: 27671 topic\_id: 8021 reply\_id: 33330[/import]

---

<div class="post-metadata">

**Author:** ![joshdobbs](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/j/51bf81/32.png) [@joshdobbs](https://forums.solar2d.com/u/joshdobbs)\
**Post date:** [April 20, 2011, 8:37pm UTC](https://forums.solar2d.com/t/got-admob-working-for-android/288560/17 "2011-04-20T20:37:41Z")

</div>

I figured out a way to remove the ad banner…it’s probably not the best way but it works for me.

In addition to ad.html i also have a file named noad.html which is the same file provided here([http://www.ludicroussoftware.com/corona/admob-ad-support-in-corona/](http://www.ludicroussoftware.com/corona/admob-ad-support-in-corona/)) that has a bunch of x’s instead of my actual pubid. it’s more of a hack but it works. [import]uid: 27671 topic\_id: 8021 reply\_id: 33336[/import]

---

<div class="post-metadata">

**Author:** ![Daniel\_W](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/d/8dc957/32.png) [@Daniel\_W](https://forums.solar2d.com/u/Daniel_W)\
**Post date:** [April 21, 2011, 6:00am UTC](https://forums.solar2d.com/t/got-admob-working-for-android/288560/18 "2011-04-21T06:00:47Z")

</div>

What’s your fill rate with AdMob? [import]uid: 14218 topic\_id: 8021 reply\_id: 33385[/import]

---

<div class="post-metadata">

**Author:** ![joshdobbs](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/j/51bf81/32.png) [@joshdobbs](https://forums.solar2d.com/u/joshdobbs)\
**Post date:** [April 21, 2011, 9:30pm UTC](https://forums.solar2d.com/t/got-admob-working-for-android/288560/19 "2011-04-21T21:30:55Z")

</div>

My fill rate for the last 30 days is 89%. it’s 56% for the lats 90 days and its 89% for the last 2 years. Currently all of my apps are for Android. [import]uid: 27671 topic\_id: 8021 reply\_id: 33520[/import]

---

<div class="post-metadata">

**Author:** ![Daniel\_W](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/d/8dc957/32.png) [@Daniel\_W](https://forums.solar2d.com/u/Daniel_W)\
**Post date:** [April 22, 2011, 4:41am UTC](https://forums.solar2d.com/t/got-admob-working-for-android/288560/20 "2011-04-22T04:41:09Z")

</div>

Was your fill rate low when you first started with admob? I just placed ads in my app last week and my fill rate is at 30%. With your high fill rate, I wonder if it just takes a little bit to get the fill rate up. [import]uid: 14218 topic\_id: 8021 reply\_id: 33565[/import]

[Next page](https://forums.solar2d.com/t/got-admob-working-for-android/288560.md?page=2)
