Android Tutorial AdMob Tutorial - Add ads to your application

Status
Not open for further replies.

Erel

Administrator
Staff member
Licensed User
Do not use AdMob library. It no longer works.
You should use FirebaseAdMob instead.

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):
B4X:
#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:
B4X:
'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:
B4X:
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, 0dip, 0dip, 320dip, 50dip)' 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:



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:
B4X:
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, 0dip, 0dip, 320dip, 50dip) '[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.



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

Manifest editor
The following code should be added to the manifest editor:
B4X:
'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:
B4X:
failed. No ad to show.
After awhile it should start working.
 
Last edited:

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:

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?
 

Erel

Administrator
Staff member
Licensed User
But the ad banner is rarely appear on my app (about 1/20)
I believe that once your application will be more popular it will be served more ads.
 

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:

To get test ads on the emulator use AdManager.setTestDevices( new String[] { AdManager.TEST_EMULATOR })
How to fix it?
 

Erel

Administrator
Staff member
Licensed User
You should use the constant AdMob.EMULATOR (see the commented lines in the tutorial code).
 

Erel

Administrator
Staff member
Licensed User
Here:
B4X:
        AdMob.SetTestDevices(Array As String(AdMob.EMULATOR))
 

susu

Well-Known Member
Licensed User
Another error:

AdMob.SetTestDevices(Array As String(AdMob.EMULATOR))
javac 1.6.0_21
src\com\axa\demo\detail.java:188: cannot find symbol
symbol : method SetTestDevices(java.lang.String[])
location: class anywheresoftware.b4a.admobwrapper.AdMob
_admob.SetTestDevices(new String[]{_admob.EMULATOR});
^
1 error
 

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
 

Erel

Administrator
Staff member
Licensed User
Would it be possible eventually to have the manifest xml
modification for AdMob inclusion automatic?
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.
Is there a practical recommendation for the number of
AdMob keywords (or number of characters devoted thereto)?
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).
 

etLux

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. (Automating AdMob .xml mods)

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). (Number of words or characters in AdMob keywords.)
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
 

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:

Erel

Administrator
Staff member
Licensed User
Is it possible to change the adMob Parent from one activity to another???
If so, How?
It is not possible to move views between activities. Did you try to create another AdView in the second activity?
 

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!
 

Cableguy

Expert
Licensed User
It is not possible to move views between activities. Did you try to create another AdView in the second activity?
Yes I did and all works well...

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!
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.
 
Status
Not open for further replies.
Top