B4A Tutorial AdMob Tutorial - Add ads to your application

Discussion in 'Tutorials & Examples' started by Erel, Jan 12, 2011.

  1. Erel

    Erel Administrator Staff Member Licensed User

    AdMob library requires Android SDK level 13 or above during compilation. You will need to download it with the SDK Manager and then choose Tools - Configure Paths and choose android.jar from platforms\android-13.

    Google has released a new Ads SDK based on Google Play Services. The new SDK requires B4A v3.20.

    AdMob v2.x (Google Play Services) instructions - supported by Android 2.3+:

    1. Register your app and get an ad unit id (PublisherId).
    2. Download and copy google-play-services.jar as explained in steps 1 and 2 here: http://www.b4x.com/android/forum/threads/45843/#content
    3. Add the following attribute to the project (change the path based on your installation):
    Code:
    #AdditionalRes: C:\android-sdk-windows\extras\google\google_play_services\libproject\google-play-services_lib\res, com.google.android.gms
    4. Add the following manifest editor code:
    Code:
    'AdMob
    AddApplicationText(
    <meta-data android:name=
    "com.google.android.gms.version"
      android:value=
    "@integer/google_play_services_version"/>
    <activity android:name=
    "com.google.android.gms.ads.AdActivity"
      android:configChanges=
    "keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>

    )
    'End of AdMob
    5. Add an AdView view to your project:
    Code:
    Sub Process_Globals

    End Sub

    Sub Globals
        
    Dim AdView1 As AdView
    End Sub

    Sub Activity_Create(FirstTime As Boolean)
        AdView1.Initialize(
    "Ad""<Ad Unit Id>")
        Activity.AddView(AdView1, 
    0dip0dip320dip50dip)' AdView1.LoadAd 'loads an ad
       AdView1.LoadAd
    End Sub
    Sub Activity_Pause (UserClosed As Boolean)
       AdView1.Pause
    End Sub
    Sub Activity_Resume
       AdView1.Resume
    End Sub

    Sub Ad_FailedToReceiveAd (ErrorCode As String)
        
    Log("failed: " & ErrorCode)
    End Sub
    Sub Ad_ReceiveAd
        
    Log("received")
    End Sub
    Sub Ad_AdScreenDismissed
       
    Log("screen dismissed")
    End Sub
    AdMob v1.x (old version) instructions:

    Installing AdMob libraries:

    1. Go to AdMob - Register and register to AdMob.

    2. Add an application:

    [​IMG]

    The url can be a placeholder for a future application.

    3. After creating the application you should see a screen with your "Publisher ID" and a link to "get publisher code".
    Later you will need to enter your publisher id in the program code. Now you should download AdMob SDK by pressing on "get publisher code".
    You will download a zip file with a name similar to: GoogleAdMobAdsSdk-4.0.4.zip.
    Inside the zip there is a folder with the same name. Inside this folder you will find the required jar file (GoogleAdMobAdsSdk-x.x.x.jar).
    Note that you may have problems extracting the whole zip file because it contains a folder and a file with the same name. The solution is to just drag the required file.
    Rename the jar file and change its name to GoogleAdMobAdsSdk.jar (removing the specific version suffix).

    4. Download AdMob B4A library.
    You should copy all files to the additional libraries path.

    5. Copy GoogleAdMobAdsSdk.jar to the additional libraries path.

    Adding AdMob to your application

    1. Add a reference to AdMob library.
    2. Typical code should look like:
    Code:
    Sub Process_Globals

    End Sub

    Sub Globals
        
    Dim AdView1 As AdView
    End Sub

    Sub Activity_Create(FirstTime As Boolean)
        Activity.LoadLayout(
    "1")
        AdView1.Initialize(
    "Ad""YourPublisherId"'publisher id that you received from AdMob.
        Activity.AddView(AdView1, 0dip0dip320dip50dip'[B]previously the height was 48dip. Now it is 50dip.
        [/B]AdView1.LoadAd 'loads an ad
    End Sub

    Sub Ad_FailedToReceiveAd (ErrorCode As String)
        
    Log("failed: " & ErrorCode)
    End Sub
    Sub Ad_ReceiveAd
        
    Log("received")
    End Sub
    Sub Ad_AdScreenDismissed
       
    Log("screen dismissed")
    End Sub
    - AdView1 size must be 320dip x 50dip, otherwise it will not display.

    The above code puts an ad at the top of the screen.

    [​IMG]

    You can change the Visible property and hide or show the ad.

    Manifest editor
    The following code should be added to the manifest editor:
    Code:
    'AdMob
    AddApplicationText(
    <activity android:name=
    "com.google.ads.AdActivity"
      android:configChanges=
    "keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>
    )
    'End of AdMob

    Some tips:
    - AdMob writes debugging messages to LogCat. However you will not see those messages when LogCat is filtered. Uncheck the "filter" check box and look for these messages.
    - On the emulator a test ad should appear instead of a real ad.
    - Many times, especially when you start, AdMob will not return any ads. LogCat should display something like:
    Code:
    failed. No ad to show.
    After awhile it should start working. If it doesn't work you should make sure that the package name matches the value defined in "package url" in AdMob site.
     
    Last edited: Dec 12, 2014
    Mark Zraik likes this.
  2. susu

    susu Well-Known Member Licensed User

    Update: It worked! So we need to wait about 30 minutes before AdMob active it :D

    -----------------------------

    I tried AdMob with my app. The ad banner appeared but AdMob doesn't count it. Otherwise, the status of my app is "Inactive: AdMob has never received an ad request for your site".

    I really need some help!
     
    Last edited: Jan 12, 2011
  3. Erel

    Erel Administrator Staff Member Licensed User

    Yes, the numbers are not updated immediately.
     
  4. susu

    susu Well-Known Member Licensed User

    But the ad banner is rarely appear on my app (about 1/20). And we can't "earn money by displaying ads in our applications", we only earn when user click the ad banner. I got 0.01$/click, anybody got better?
     
  5. Erel

    Erel Administrator Staff Member Licensed User

    I believe that once your application will be more popular it will be served more ads.
     
  6. susu

    susu Well-Known Member Licensed User

    I'm using emulator and tried to get test ads but I can't find device id in LogCat window. This is what I got:

    How to fix it?
     
  7. Erel

    Erel Administrator Staff Member Licensed User

    You should use the constant AdMob.EMULATOR (see the commented lines in the tutorial code).
     
  8. susu

    susu Well-Known Member Licensed User

    I tried:
    but got error. Can you show me the right code? Thanks.
     
  9. Erel

    Erel Administrator Staff Member Licensed User

    Here:
    Code:
    AdMob.SetTestDevices(Array As String(AdMob.EMULATOR))
     
  10. susu

    susu Well-Known Member Licensed User

    Another error:

     
  11. Erel

    Erel Administrator Staff Member Licensed User

  12. susu

    susu Well-Known Member Licensed User

    Thanks for your super quick "hot fix" :D
     
  13. Erel

    Erel Administrator Staff Member Licensed User

    Thank you for reporting it ;)
     
  14. etLux

    etLux Member Licensed User

    Thank you so much for the new AdMob capability, Erel!
    Well executed, well explained -- and easy to do.

    1.)

    Would it be possible eventually to have the manifest xml
    modification for AdMob inclusion automatic?

    I mention because manual mods like this are a frequent
    source of error for the dyslexics amongst us... lol.

    2.)

    Is there a practical recommendation for the number of
    AdMob keywords (or number of characters devoted thereto)?

    All the best,



    David
    -----
    David Sosnowski
    The Music of David Sosnowski
     
  15. Erel

    Erel Administrator Staff Member Licensed User

    I will add it to the feature list. It is a bit problematic as this is special for this library only. But we will check it.
    I do not know. You should try searching for it. I think that there is a google group devoted to AdMob (or try the Android google group).
     
  16. etLux

    etLux Member Licensed User

    I've been unable to locate any information on the keywords
    limits (if any)... though I would expect from general practice
    one does well not to get carried away.

    I might note that I have implemented the AdMob library in
    a little app I published on AndroidMarket this evening -- and
    everything is working flawlessly with it.

    Thank you again for a fine job on this for all of us! This was
    really extremely easy to implement.

    All the best,



    David
    -----
    David Sosnowski
    The Music of David Sosnowski
     
  17. Cableguy

    Cableguy Expert Licensed User

    I have a question and a tip to share about my experience with adMob...

    Q: Is it possible to change the adMob Parent from one activity to another???
    If so, How?

    TIP: Since AdMob needs a modified manifest, I've been using a Panel with the same dimensions as the Banner...because everytime one compiles a project, if new libs were added to it, they may be un-referenced in it...Then, after all App related coding is done, add the AdMob, edit the manifest acording to the manual and remove the panel and it's references, if any.
     
    Last edited: Jan 24, 2011
  18. Erel

    Erel Administrator Staff Member Licensed User

    It is not possible to move views between activities. Did you try to create another AdView in the second activity?
     
  19. susu

    susu Well-Known Member Licensed User

    I'm using a long keyword in my app and it works without problem. I published my app 12 days ago and got over 13.000 requests but the fill rate is quite low. I think because my users are not target of Admob advertiser. Now I got $1.02 revenue (yes, more than one dollar) but I'm happy with it :D I hope you will get more!
     
  20. Cableguy

    Cableguy Expert Licensed User

    Yes I did and all works well...

    Glad to ear about you making money...
    Were did you gett the deal, at AdMob site?, I cant find anything related to that..
    I hope to publish my first real work in to the market at the begining of the month.
     

Share This Page

Loading...