B4A Library AdMob library

This library requires some configuration. Please see the tutorial: http://www.b4x.com/forum/basic4andr...-admob-tutorial-add-ads-your-application.html

Reference: Basic4android - AdMob

AdMob v2.00 - Based on Google Play Services. This version requires B4A v3.20.

AdMob v1.40 - Add support for SIZE_SMART_BANNER.
These ads will fill the whole available width: https://developers.google.com/mobile-ads-sdk/docs/admob/smart-banners

You can use the following code:
B4X:
Adview1.Initialize2("Ad", "xxxxxxxx", AdView1.SIZE_SMART_BANNER)
Dim height As Int
If GetDeviceLayoutValues.ApproximateScreenSize < 6 Then
   'phones
   If 100%x > 100%y Then height = 32dip Else height = 50dip
Else
   'tablets
   height = 90dip
End If
Activity.AddView(AdView1, 0dip, 100%y - height, 100%x, height)

See this post for an additional requirement if using google play services v28: https://www.b4x.com/android/forum/threads/admob-library.7301/page-10#post-381519
 

Attachments

  • AdMob_old.zip
    4.3 KB · Views: 1,119
  • AdMob.zip
    4.7 KB · Views: 3,010
Last edited:

Inman

Well-Known Member
Licensed User
Longtime User
Since Admob, Analytics and other libraries now depend on Google Play Services, does that mean they won't work on devices that don't have Google Play pre-loaded (like Amazon's Kindle Fire tablets, Xiaomi and other Chinese OEM devices)?
 

achtrade

Active Member
Licensed User
Longtime User
Thanks for this tutorial. Only one thing, it works fine but I have to add this line : "AdView1.LoadAd"

Otherwise I can't see any ads.
 

Croïd

Active Member
Licensed User
Longtime User
I upgrade my old admob with google play service.
But now, if wifi is enabled, advertising does not appear ! If I let that 3/4 G only, advertising appears !

I was told that : This is a setting in your code that decides whether ads are served only on WiFi or on WiFI and 3g/4g.

what is the way to enable WiFi and 3G / 4G at the same time ?

manifest contains currently :
AddPermission (android.permission.ACCESS_NETWORK_STATE)
AddPermission(android.permission.ACCESS_WIFI_STATE)
AddPermission(android.permission.INTERNET)

and my page for help in my webview, it works in 2 circumstances !!!
 

Croïd

Active Member
Licensed User
Longtime User
I upgrade my old admob with google play service.
But now, if wifi is enabled, advertising does not appear ! If I let that 3/4 G only, advertising appears !

I was told that : This is a setting in your code that decides whether ads are served only on WiFi or on WiFI and 3g/4g.

what is the way to enable WiFi and 3G / 4G at the same time ?

manifest contains currently :
AddPermission (android.permission.ACCESS_NETWORK_STATE)
AddPermission(android.permission.ACCESS_WIFI_STATE)
AddPermission(android.permission.INTERNET)

and my page for help in my webview, it works in 2 circumstances !!!

I m not alone !

https://groups.google.com/forum/#!topic/google-admob-ads-sdk/k-sHqnJkStg


I found the problem !!!!

If you encounter this problem, My FAI that enables AdBlock function without prevention !
 

mlc

Active Member
Licensed User
Longtime User
With a trail and fail method I ended with this content of the librairy which apparently seems to work...
View attachment 25281
At least it displays:
- smart banners
- interstitial ads
I've not checked if a click is correctly attributed to my account ...
The impact on the memory footprint is in the range of 300kB
I did not try to open "internal" folder and do some gardening insisde !
==> not sure it worths to do this !!!

Hello Alain,
Have you checked if the clicks are correctly attributed to your account?
 

palmzac

Active Member
Licensed User
Longtime User
Hi,

SMART_BANNER is 320x50 . How to display LARGE_BANNER ( 320x100 ) ? Would you help me ? Thanks !
 

CyclopDroid

Well-Known Member
Licensed User
Longtime User
And for use Interstitial AdMob? What is the code? I use the mwAdmobInterstitial library but not view the AdMob and not have a error. :(
Thaks.
 

achtrade

Active Member
Licensed User
Longtime User
I'm using this:

B4X:
Sub Activity_KeyPress(KeyCode As Int) As Boolean
    If  KeyCode = KeyCodes.KEYCODE_BACK Then
        Dim result As Int
        result = Msgbox2("Exit?", "Exit", "Yes", "", "No", Null)
        If result = DialogResponse.Positive Then  
            If mwAdInterstitial.Status=mwAdInterstitial.Status_AdReadyToShow Then mwAdInterstitial.Show
           If mwAdInterstitial.Status=mwAdInterstitial.Status_AdReadyToShow Then mwAdInterstitial.Show
                Activity.Finish
                ExitApplication 
        Else
            Return True
        End If
    End If
End Sub
 

CyclopDroid

Well-Known Member
Licensed User
Longtime User
Thanks but then problem it's found.
First, I don't have create a ID Interstitial but only ADMOD
Second, LucaMs, have found that, since I was usingused the Interstital into another Main, the Manifest.xml had and error.
He made me change:
B4X:
<activity android:name="com.google.ads.AdActivity"

With

B4X:
<activity  android:name="com.google.android.gms.ads.AdActivity"

...and now start :D
 

kepler

Active Member
Licensed User
Longtime User
Hi,

I'm implementing AdMob - at least trying... I've downloaded Google Play Services (the AdMob SDK seems to no longer have a direct download).
I've placed the line in the B4A project:

B4X:
#AdditionalRes: C:\Program Files (x86)\Android\android-sdk\extras\google\google_play_services\libproject\google-play-services_lib\res, com.google.android.gms

and configured the rest as in the example.

I'm getting the following error: class file for com.google.ads.AdView not found...

Any ideas?...

Kind regards,

Kepler
 

CyclopDroid

Well-Known Member
Licensed User
Longtime User
Hi,

.....
I'm getting the following error: class file for com.google.ads.AdView not found...

......
Kepler
Are you sure when the google res it's into this directory? Or you have create another directory for put the librari and res?
Also check if you have entered into Manifest.xml the declaration:
B4X:
<activity  android:name="com.google.android.gms.ads.AdActivity"
 

JohnK

Active Member
Licensed User
Longtime User
Hi,

I'm implementing AdMob - at least trying... I've downloaded Google Play Services (the AdMob SDK seems to no longer have a direct download).
I've placed the line in the B4A project:

B4X:
#AdditionalRes: C:\Program Files (x86)\Android\android-sdk\extras\google\google_play_services\libproject\google-play-services_lib\res, com.google.android.gms

and configured the rest as in the example.

I'm getting the following error: class file for com.google.ads.AdView not found...

Any ideas?...
I am having the same problem. I have:
1. Edited the b4a ini file (MaxRamForDex=1024)
2. Added the following line to my project file (under "#Region Project Attributes")
#AdditionalRes: C:\Java\x86\Android\android-sdk\extras\google\google_play_services\libproject\google-play-services_lib\res, com.google.android.gms
which does point to my res files
2. Added the following to the manifest
'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

3. Updated and copied the "google-play-services.jar" file to libraries
4. Copied the "GoogleAdMobAdsSdk-6.4.1.jar" file to libraries
5. Downloaded and installed (ie copied into libraries) v1.4 of AdMob library
6. Selected "AdMob" in libraries tab in b4a
7. Added the following code to the project:
Sub Globals
Dim AdView1 As AdView
End Sub

Sub Activity_Create(FirstTime As Boolean)
AdView1.Initialize("Ad", "<my personal id>")
Activity.AddView(AdView1, 0dip, 50dip, 320dip, 50dip)' AdView1.LoadAd 'loads an ad
AdView1.LoadAd
End Sub


And I am getting this error:

Parsing code. 0.02
Compiling code. 0.03
Compiling layouts code. 0.00
Generating R file. 0.88
Compiling generated Java code. Error
B4A line: 138
Activity.AddView(AdView1, 0dip, 50dip, 320dip, 50dip)' AdView1.LoadAd 'loads an ad
javac 1.7.0_07
src\xxx\xxx\xxx\main.java:440: error: cannot access AdView
mostCurrent._activity.AddView((android.view.View)(mostCurrent._adview1.getObject()),anywheresoftware.b4a.keywords.Common.DipToCurrent((int) (0)),anywheresoftware.b4a.keywords.Common.DipToCurrent((int) (50)),anywheresoftware.b4a.keywords.Common.DipToCurrent((int) (320)),anywheresoftware.b4a.keywords.Common.DipToCurrent((int) (50)));
^
class file for com.google.ads.AdView not found
1 error


Now, the thing to watch out on is that I am running b4a v3.2

Any ideas? What have I missed?

I noticed in some of the tutes I read it referred to an AdMob library version of 2.x while I could only find 1.4.
I just checked and found this post refers to v2 but the download zip is 1.4.
https://www.b4x.com/android/forum/threads/admob-library.7301/

Also, the example code used something like "AdView1.Pause" which is not available in v1.4. Is there a different library somewhere?

OK I think I found out where I went wrong. I must of used an AdMob.zip library file I downloaded before instead of the latest. It now all appears to be working for me.
 
Last edited:

JohnK

Active Member
Licensed User
Longtime User
OK, maybe I spoke a little too soon.

I no longer get any issues in compiling the project (ie "class file for com.google.ads.AdView not found."), but, the app shuts down as soon as I open it.

I tried creating a new project, and as soon as I add the line "Dim AdView1 As AdView" in Globals, the application will shutdown on the mobile as soon as it is opened. ie without adding the actual ad view object.

I am just checking the logs to see if there are any clues. But I am also on low battery so it may be tomorrow when I know more (if ever).
 

JohnK

Active Member
Licensed User
Longtime User
Note that the code you posted is unreadable.

Please start a new thread and make sure to include the error message from the logs.
To read the code, you can simply zoom in with the browser, or copy and paste it into another app. I just made it a small font to differentiate it in the post, and to not take up too much room.

As for the logs, the app shuts down BEFORE any logs are generated. If set to debug and connected with b4a-bridge. As soon as it connects to the debug bridge, it shuts down.

I also checked:
cat /proc/last_kmsg
dmesg
logcat -d

After the next boot, and couldn't find anything of interest in there.

I decided to make a demo version and monetize it as a paid app.
 

aeric

Expert
Licensed User
Longtime User
To read the code, you can simply zoom in with the browser, or copy and paste it into another app. I just made it a small font to differentiate it in the post, and to not take up too much room.

As for the logs, the app shuts down BEFORE any logs are generated. If set to debug and connected with b4a-bridge. As soon as it connects to the debug bridge, it shuts down.

I also checked:
cat /proc/last_kmsg
dmesg
logcat -d

After the next boot, and couldn't find anything of interest in there.

I decided to make a demo version and monetize it as a paid app.
You may use [ CODE ] Your Codes [ /CODE ] to displays your codes inside a box with colour highlighting.
B4X:
Sub Globals
Dim AdView1 As AdView
End Sub
.

For logs, you can use [ QUOTE ] Error Logs [ /QUOTE ]
Parsing code. 0.02
Compiling code. 0.03
Compiling layouts code. 0.00
Generating R file. 0.88
Note: No blank space after "[" and before "]".
 
Top