Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

Final Solution to integrating AdMob into Android

Many others have struggled with integrating AdMob into your Android apps and I did as well. I spent many hours trying every suggested solution, from manipulating the returned web view html to fire off ti events to plugging in ad jar files. Every solution failed for different reasons, some because they didn't work with the latest titanium sdk version, and others because they assumed that the web view ad html would contain links when in reality the production admob ads load images into iframes.

All of this being said, here is a solution that actually works and is really simple.

  1. First you make a public web page that contains your ad mob code.
  2. Then you can load it into a webview. The trick with webview is that it loads twice. Once to load the ad, then a second time when you click the ad.
  3. When clicking the ad, the evt.url property of the load event will be equal to the ad destination url so you can capture that and launch it off in a new browser.
  4. If you stop there then if they return to your app, it will show the ad destination loaded in your webview. For that reason, you also want to reload the ad webview to contain a new ad.
var console = { log: Ti.API.info };
var adUrl = "http://tothsolutions.com/ad/mobile";
var adWebView = null;

displayAd();

function displayAd() {
    var numLoads = 0;
    if (adWebView) {
        win.remove(adWebView);
    }
    adWebView = Ti.UI.createWebView({ url: adUrl, height: 48, bottom: 0, left: 0, right: 0 });
    win.add(adWebView);
    adWebView.addEventListener('load', function (evt) {
        numLoads++;
        if (numLoads > 1) {
            var url = evt.url.split('market://').join('http://');
            console.log("clicked ad " + url);
            //launch ad url in new browser window.
            Ti.Platform.openURL(url);
            //load a new ad.
            displayAd();
        }
    });
}

Much props to my brother Dawson for figuring out the trick with the webview load event!

— asked November 8th 2010 by Justin Toth
  • admob
  • android
4 Comments
  • I want to butter you up and roll you in money. Thank you for sharing this.

    — commented November 10th 2010 by Ian Hawes
  • sorry, im abit dumb here, what's the stuff you put in the webview code?

    — commented November 30th 2010 by Julien Ho
  • In admob you will create a "mobile site" ad, then they will give you html/javascript code. You can then paste that code into a blank web page on your web server, then use the url to that web page as the adUrl above.

    — commented January 12th 2011 by Justin Toth
  • I am missing something. Upon creating a new site on admob, I don't see an option for "mobile site". If I select "mobile web", and view the code to publish it doesn't have Javascript as an option - only PHP, C#, VB.net… Upon selecting "Smartphone Web", this does present Javascript code to publish, but this is for iphone only, and I'm trying to deploy on Android. I created a .net version and deployed on a site, but when accessing the .aspx from my android phone the screen was blank. Any help greatly appreciated …

    — commented February 2nd 2011 by Software Services

11 Answers

  • hi Justin,

    i tried it and it worked!

    question abt the public web page used though: does admob approve of using a public web page to serve ads? Each time a user clicks on it, the admob managers will see that the referrer originates from the web page, not the app.

    anyone has any experience on this end?

    — answered January 10th 2011 by Foo Yee Chong
    permalink
    2 Comments
    • I haven't had any issues with this, I've been using admob for a few months in this manner.

      — commented January 12th 2011 by Justin Toth
    • Folks, I lost my Adsense account within 3 months of doing this. I'm not sure how sophisticated Admob's (maybe soon to be Google's) click fraud technology will be down the road but I believe when it realize that it cannot detect any content (and thus cannot effectively target ads) from the referring web page, it may suspend your account for violation of its TOS, citing protection of advertiser's interest. I have read alot that in most cases (for Adsense), suspended publishers never got their accounts back. :(

      — commented March 17th 2011 by Clifford Tan
  • Hi Justin,
    How can i make a public web page that contains ad mob code.

    — answered January 14th 2011 by Somi Bajaj
    permalink
    1 Comment
    • In admob you will create a "mobile site" ad, then they will give you html/javascript code. You can then paste that code into a new web page on your web server, then use the url to that web page as the adUrl above.

      Go to http://tothsolutions.com/ad/mobile and view page source to see a sample of what it would look like, just make sure you replace my ad code with the script that admob gives you, otherwise you will be earning me revenue. ;)

      — commented January 14th 2011 by Justin Toth
  • I'm a little new to Titanium but couldn't you just put the code in an html file included in your app project and call it from there instead of an external website? I'm at work atm and can't try it personally but you would think it would work.

    If it does work, it would most likely load faster too as you would not be dependent on your webserver to feed the html file holding the ad. Just a thought….

    Joe

    — answered January 14th 2011 by Joseph Reynolds
    permalink
    3 Comments
    • You certainly could do that, I like to keep it separate though so that I can change the ad code at any time without having to push out a new version of the app.

      — commented January 14th 2011 by Justin Toth
    • True, I was thinking of the possible lag is all from the multiple connections to pull the ad. The control factor though is good to have in my opinion too. Especially if you want to try various services out through the app…

      — commented January 16th 2011 by Joseph Reynolds
    • from an ad manager's point of view, if he/she sees an external web site appearing in the logs, it might be code red. The manager will definitely visit the external URL and be surprised to see a piece of ad there, with zero content (all content is in the app, but the manager doesn't see it in the logs). I think you need to clarify this issue with ad managers. Especially if the app is generating lots of ad clicks.

      any thoughts on this?

      — commented January 16th 2011 by Foo Yee Chong
  • Hi Justin,
    In admob i create a "mobile site" ad, then they gave me php code. I paste this php code into a new web page on my web server, but
    the publisher code is set to test mode. When i make request to server in this mode,It return the "AdMob Test Ad".But when i set the "TEST_MODE" variable to "false",it does not return anything.

    — answered January 15th 2011 by Somi Bajaj
    permalink
    1 Comment
    • It does that because it detects that you are hitting the page from a non-mobile browser. Try the same from your phone and you should see ads showing up, not every single page request serves up an ad so you may have to try a few times.

      — commented January 15th 2011 by Justin Toth
  • Thank you for sharing the details of this solution, Justin.

    I'm having difficulty getting this to work. Upon creating a new site on admob, I don't see an option for "mobile site". If I select "mobile web", and view the code to publish it, there isn't an option for Javascript - only PHP, C#, VB.net…

    Upon selecting "Smartphone Web", this does present Javascript code to publish, but this is for iphone only, and I'm trying to deploy on Android. I created a .net version and deployed on a site, but when accessing the .aspx from my android phone browser the screen was blank. Acessing from my PC browser displayed the test message.

    I also tried to use the PHP script, posting on my website and accessing the same way, and had the same results - test ad shows in PC browser but blank screen when rendered on my phone.

    Any help greatly appreciated …

    — answered February 3rd 2011 by Software Services
    permalink
    2 Comments
    • Samuel - I'm getting the same thing - blank white area in the android emulator. Were you able to get this figured out?

      — commented March 9th 2011 by D Pat
    • Never mind Samuel - the code posted by Justin does work, however it only seems to display an add like 1 out of 10 times. Perhaps not enough ads in the system, but not sure.

      — commented March 10th 2011 by D Pat
  • Does this works for android?

    — answered March 1st 2011 by Obed Adames
    permalink
    0 Comments
  • I get an error using this code it give me:
    Wrapped java.lang.NullPointerException (file://android_asset/Resources/app.js#28

    and this is my code
    /////// NO BORRAR
    var webview = Titanium.UI.createWebView({url:'/html/index.html'});
    var window = Titanium.UI.createWindow();
    window.add(webview);

    /////// ADS
    

    var console = { log: Ti.API.info };
    var adUrl = "THIS IS WHERE THE HTML AD WILL BE I HAVE REMOVE IT";
    var adWebView = null;

    displayAd();

    function displayAd() {
    var numLoads = 0;
    if (adWebView) {
    window.remove(adWebView);
    }
    adWebView = Ti.UI.createWebView({ url: adUrl, height: 48, bottom: 0, left: 0, right: 0 });
    window.add(adWebView);
    adWebView.addEventListener('load', function (evt) {
    numLoads++;
    if (numLoads > 1) {
    var url = evt.url.split('market://').join('http://');

            console.log("clicked ad " + url);
            //launch ad url in new browser window.
            Ti.Platform.openURL(url);
            //load a new ad.
            displayAd();
        }
    });
    

    }

    /////// NO BORRAR

    window.open({modal:true});
    
    — answered March 2nd 2011 by Obed Adames
    permalink
    0 Comments
  • I get an error using this code it give me:
    Wrapped java.lang.NullPointerException (file://android_asset/Resources/app.js#28

    and this is my code
    /////// NO BORRAR
    var webview = Titanium.UI.createWebView({url:'/html/index.html'});
    var window = Titanium.UI.createWindow();
    window.add(webview);

    /////// ADS
    

    var console = { log: Ti.API.info };
    var adUrl = "THIS IS WHERE THE HTML AD WILL BE I HAVE REMOVE IT";
    var adWebView = null;

    displayAd();

    function displayAd() {
    var numLoads = 0;
    if (adWebView) {
    window.remove(adWebView);
    }
    adWebView = Ti.UI.createWebView({ url: adUrl, height: 48, bottom: 0, left: 0, right: 0 });
    window.add(adWebView);
    adWebView.addEventListener('load', function (evt) {
    numLoads++;
    if (numLoads > 1) {
    var url = evt.url.split('market://').join('http://');

            console.log("clicked ad " + url);
            //launch ad url in new browser window.
            Ti.Platform.openURL(url);
            //load a new ad.
            displayAd();
        }
    });
    

    }

    /////// NO BORRAR

    window.open({modal:true});
    
    — answered March 2nd 2011 by Obed Adames
    permalink
    0 Comments
  • This Approach must have an own web server, If I haven't own web server. How can I do, Have any suggestion for me?
    Note:I have been successful integrate the iPhone App with AdMob on "Smartphone Web" approach.
    Now I want to try deployed it to Android Device. I can use Titanium "createWebView" method to
    present the AdMob banner, but If the user clicked the AD banner, I can't detect this event.
    My purpose is wanting let the click AdMob Banner can open on New Window.

    Ref1: Admob for iPhone
    http://developer.admob.com/wiki/IPhone

    • This document described, I can use "new_windos: true" to control it.

    Ref2: Admob for Android
    http://developer.admob.com/wiki/Android

    • But on the Android hasn't the "new_windos: true" method.

    Does anyone have any solution on it? I want to detect the "Click AdMob Banner" behavior to open on new window.

    Thank you!

    — answered March 19th 2011 by Potato Hwang
    permalink
    0 Comments
  • I was about to use this solution on a project I'm working on until I saw Clifford Tan's post that his account got suspended for this. Not sure if you guys saw this other solution provided on http://developer.appcelerator.com/question/39161/ad-layers. I got it working in the little sample app Ti generates. Haven't gotten it up and running in one of my apps yet. It was a little confusing to figure out at first because his directions are for linux. I can explain how i did it on windows 7.

    Download his zip: http://CrucialDivide.com/mobilesdk-dasher-ads-1.4-linux.zip and then find where your titanium sdk stuff is. For me i had to display hidden folders and it was at C:\ProgramData\Titanium\mobilesdk.

    Inside the zip file open mobilesdk/linux and copy that dasher-ads-1.4 folder into the folder on your machine(mine was C:\ProgramData\Titanium\mobilesdk\win32) along with the sdks for other releases of Ti mobile. Restart Ti and start a new project and it should pop up as an available sdk. Then refer to this page https://gist.github.com/439986 for implementation. Hope this helps.

    — answered April 7th 2011 by Bryant Pulecio
    permalink
    1 Comment
    • Doesnt work, cant even build.

      — commented April 19th 2011 by Josh Lewis
  • I apologize for posting this in several threads, but in case anyone is searching for an Android AdMob solution, I have a (simple) beta version of an android module here: https://github.com/bkurzius/Titanium-Android-AdMob-Module.

    I posted more info about it here: http://developer.appcelerator.com/question/119673/admob-for-android. Any android devs, please feel free to help improve (or rebuild) it!

    — answered June 7th 2011 by brian kurzius
    permalink
    0 Comments
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.