B4A Library Admob Interstitial Library

Because the old Admob SDK will be deprecated in August, I have written a library for interstitials using the new Google Play Services library. You'll need to follow initial steps noted in Erel's post for the Admob banner library to get your app ready for this library.

The new library should be compatible with your old code with the exception of a change to the AdFailedToReceive event. It should be declared in your app as follows.

B4X:
Sub mwadi_AdFailedToLoad (ErrorMessage As String)
    Log("failed to load ad: " & ErrorMessage)
End Sub

This new library also supports a new event that triggers when the user closes an ad. You can use this event to queue a new ad to load or run other code.

B4X:
Sub mwadi_AdClosed
    mwAdInterstitial.LoadAd
End Sub

The new library is called mwAdmobInterstitial2. I added the 2 to the end of the name so you can preserve your old library in case it is needed with other apps. The new library and example app for the library is attached at the end of this post.

============================================
Below is for the old version (not using Google Play Services)
============================================
I was able to put together a library to display interstitial ads from Admob. You'll need to create an interstitial ad unit in Admob and use it with the initialization process.

Make sure the following is added to your AndroidManifest.xml.
B4X:
AddApplicationText(<activity android:name="com.google.ads.AdActivity"
          android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>)

Here is code from the included sample app that shows how to use the library. Because the interstitial is running as an activity, I'm not able to raise an event in B4A when it is dismissed. Instead, you can check the status in Activity_Resume as shown in the code below.

A good practice would be to initialize the ad in Activity_Create, and then show the ad later when desired. This will allow the ad to fully load while other processes are taking place allowing you to show the ad immediately.

This is a beta version and the first library I've shared, so please report any problems.

NOTE: There is a bug in older versions of the SDK that causes a crash on some devices when you try to load the interstitial. Make sure you download an updated SDK from Admob.

1.1 Update: Fixed a bug in the library code used to raise the AdFailedToLoad event. Separated out the LoadAd routine from the Initialize routine. This allows you to load a new ad after an ad has been dismissed without re-initializing the class.

B4X:
Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Dim mwAdInterstitial As mwAdmobInterstitial
    Dim Button1 As Button
    Dim Label2 As Label
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")
    mwAdInterstitial.Initialize("mwadi","INSERT_YOUR_AD_UNIT_ID")
    mwAdInterstitial.LoadAd
    Activity.LoadLayout("1")
    Label2.Text="Attempting to load ad...  Please wait."
End Sub

Sub Activity_Resume
    If mwAdInterstitial.Status=mwAdInterstitial.Status_Dismissed Then Label2.Text="Ad dismissed by user."
End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub


Sub mwadi_AdLoaded
    Log("ad loaded")
    Label2.Text="Ad loaded - Click Show Ad to Display"

End Sub

Sub mwadi_AdFailedToLoad (ErrorCode As String)
    Label2.Text="Ad failed to load with error code: " & ErrorCode
    Log("failed to load ad: " & ErrorCode)
End Sub

Sub Button1_Click
    If mwAdInterstitial.Status=mwAdInterstitial.Status_AdReadyToShow Then mwAdInterstitial.Show
    If mwAdInterstitial.Status=mwAdInterstitial.Status_Dismissed Then
        Label2.Text="Attempting to load ad...  Please wait."
        mwAdInterstitial.LoadAd
    End If
End Sub
 

Attachments

  • interstitial.png
    interstitial.png
    86 KB · Views: 857
  • AdmobInterstitial1.1.zip
    10.6 KB · Views: 1,155
  • AdmobInterstitialPlayServices.zip
    11.5 KB · Views: 1,150
Last edited:

CyclopDroid

Well-Known Member
Licensed User
Longtime User
Exuse me Jack but, in other android forum question, I see when the problem to No Fill it's caused by size of reuest or when not run AdMob in test mode. :confused:
If I run your simply example, the message appears immediately... if report in my source not enter in the Sub Activity_KeyPress and not work :eek::confused::(


B4X:
Sub Globals
Dim mwAdInterstitial As mwAdmobInterstitial 'AdMob Interstitial
......
......
......
Sub Activity_Create(FirstTime As Boolean)

mwAdInterstitial.Initialize("mwadi","ca-app-pub-xxxxxxxxxxxxxxxxx")
mwAdInterstitial.LoadAd
Activity.LoadLayout("Livelli")
------
.......
......
Sub mwadi_AdLoaded
    Log("ad loaded")
End Sub

Sub mwadi_AdFailedToLoad (ErrorCode As String)
    Log("failed to load ad: " & ErrorCode)
    Activity.Finish
    StartActivity(Main)
End Sub


Sub mwadi_AdClosed
    'you can call LoadAd again here to queue the next ad
End Sub

If I ceate, into my project, a new Module (with new visual designer) and insert here your code example.... when I call this, appears in the screen "Attempting to load ad... Please wait.", pass tp Activity_Resume and ends without going to nowhere.

Thaks you for help
 
Last edited:

Jack Cole

Well-Known Member
Licensed User
Longtime User
Did you set up a new ad unit in admob for Interstitial ads? I'm not sure, but you may need to initialize and load your interstitial in activity_resume instead of activity_create.
 

CyclopDroid

Well-Known Member
Licensed User
Longtime User
To be honest, I did not Admob Interstitial ad units, but I use only one with Admob Click on my other application.
Maybe that's the problem of the No Fill? If the answer is yes, how do I create an ID Admob Interstitial, if my game has not been published? And why, in my game, it's not recognized even the eroor, not entering into Sub mwadi_AdFailedToLoad?
Thanks again for your patience..
 

jmon

Well-Known Member
Licensed User
Longtime User
I don't see when the event "mwadi_AdOpened" is fired? Actually on my app, it's never fired.

I would like to know when a user clicked on an ad.

Thanks again for this library.

[Edit] I saw the mwadi_AdOpened event from the example.
 

Douglas Farias

Expert
Licensed User
Longtime User
i m got this error
failed to load ad: Internal error

what cause this error? i have check my appId and this its correct.

can is my google player services? i go the last now

any error or log about error, ad dont load andi got only failed to load ad: Internal error



Edit: already know the error, admob banned this app xD
 
Last edited:

Andre Souza

Member
Licensed User
Hello
I tested the example with the Google Play Services , but in time to show the announcement , only carries a black screen with a close button .
 

Douglas Farias

Expert
Licensed User
Longtime User
Hi.
I see today on google play a lot of this errors

java.lang.NoClassDefFoundError
in com.google.android.gms.ads.internal.zzs.<init>

I dont know if this error its really on your lib.
someone can give me a help to translate this?

java.lang.NoClassDefFoundError: android.support.v4.util.SimpleArrayMap
at com.google.android.gms.ads.internal.zzs.<init>(Unknown Source)
at com.google.android.gms.ads.internal.zzs.<init>(Unknown Source)
at com.google.android.gms.ads.internal.zzb.<init>(Unknown Source)
at com.google.android.gms.ads.internal.zzc.<init>(Unknown Source)
at com.google.android.gms.ads.internal.zzk.<init>(Unknown Source)
at com.google.android.gms.ads.internal.ClientApi.createInterstitialAdManager(Unknown Source)
at com.google.android.gms.ads.internal.client.zzl.createInterstitialAdManager(Unknown Source)
at com.google.android.gms.ads.internal.client.zze.zzb(Unknown Source)
at com.google.android.gms.ads.internal.client.zzac.zzH(Unknown Source)
at com.google.android.gms.ads.internal.client.zzac.zza(Unknown Source)
at com.google.android.gms.ads.InterstitialAd.loadAd(Unknown Source)
at mobi.mindware.admob.interstitial.AdmobInterstitialsAds.LoadAd(AdmobInterstitialsAds.java:91)
at leitor.df.texto.main._activity_create(main.java:425)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:169)
at leitor.df.texto.main.afterFirstLayout(main.java:102)
at leitor.df.texto.main.access$000(main.java:17)
at leitor.df.texto.main$WaitForLayout.run(main.java:80)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5602)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
at dalvik.system.NativeStart.main(Native Method)

Note: i m using Google Play Services REV 23
Note2: Try/Catch dont work here, the same log and mensage app stoped work show.

B4X:
** Service (starter) Create **
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
main_activity_create (B4A line: 121)
mwAdInterstitial.LoadAd
java.lang.NoClassDefFoundError: android.support.v4.util.SimpleArrayMap
    at com.google.android.gms.ads.internal.zzs.<init>(Unknown Source)
    at com.google.android.gms.ads.internal.zzs.<init>(Unknown Source)
    at com.google.android.gms.ads.internal.zzb.<init>(Unknown Source)
    at com.google.android.gms.ads.internal.zzc.<init>(Unknown Source)
    at com.google.android.gms.ads.internal.zzk.<init>(Unknown Source)
    at com.google.android.gms.ads.internal.ClientApi.createInterstitialAdManager(Unknown Source)
    at com.google.android.gms.ads.internal.client.zzl.createInterstitialAdManager(Unknown Source)
    at com.google.android.gms.ads.internal.client.zze.zzb(Unknown Source)
    at com.google.android.gms.ads.internal.client.zzac.zzH(Unknown Source)
    at com.google.android.gms.ads.internal.client.zzac.zza(Unknown Source)
    at com.google.android.gms.ads.InterstitialAd.loadAd(Unknown Source)
    at mobi.mindware.admob.interstitial.AdmobInterstitialsAds.LoadAd(AdmobInterstitialsAds.java:91)
    at leitor.df.texto.main._activity_create(main.java:451)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:169)
    at leitor.df.texto.main.afterFirstLayout(main.java:102)
    at leitor.df.texto.main.access$000(main.java:17)
    at leitor.df.texto.main$WaitForLayout.run(main.java:80)
    at android.os.Handler.handleCallback(Handler.java:725)
    at android.os.Handler.dispatchMessage(Handler.java:92)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:5041)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
    at dalvik.system.NativeStart.main(Native Method)

the error ignore the try catch and crash the app.
B4X:
            Try
    'INICIA INTERSTITIAL
        mwAdInterstitial.Initialize("mwadi","ca-app-xxxxxxxxxxxxxxxxxxxxxx")
        mwAdInterstitial.LoadAd
    'FIM
        Catch
        Log("Erro interstitial")
        End Try
 
Last edited:

Jack Cole

Well-Known Member
Licensed User
Longtime User
Douglas,

You need to include the support for android-support-v4.jar. Please see this post by Erel.

Jack


Hi.
I see today on google play a lot of this errors

java.lang.NoClassDefFoundError
in com.google.android.gms.ads.internal.zzs.<init>

I dont know if this error its really on your lib.
someone can give me a help to translate this?



Note: i m using Google Play Services REV 23
Note2: Try/Catch dont work here, the same log and mensage app stoped work show.

B4X:
** Service (starter) Create **
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
main_activity_create (B4A line: 121)
mwAdInterstitial.LoadAd
java.lang.NoClassDefFoundError: android.support.v4.util.SimpleArrayMap
    at com.google.android.gms.ads.internal.zzs.<init>(Unknown Source)
    at com.google.android.gms.ads.internal.zzs.<init>(Unknown Source)
    at com.google.android.gms.ads.internal.zzb.<init>(Unknown Source)
    at com.google.android.gms.ads.internal.zzc.<init>(Unknown Source)
    at com.google.android.gms.ads.internal.zzk.<init>(Unknown Source)
    at com.google.android.gms.ads.internal.ClientApi.createInterstitialAdManager(Unknown Source)
    at com.google.android.gms.ads.internal.client.zzl.createInterstitialAdManager(Unknown Source)
    at com.google.android.gms.ads.internal.client.zze.zzb(Unknown Source)
    at com.google.android.gms.ads.internal.client.zzac.zzH(Unknown Source)
    at com.google.android.gms.ads.internal.client.zzac.zza(Unknown Source)
    at com.google.android.gms.ads.InterstitialAd.loadAd(Unknown Source)
    at mobi.mindware.admob.interstitial.AdmobInterstitialsAds.LoadAd(AdmobInterstitialsAds.java:91)
    at leitor.df.texto.main._activity_create(main.java:451)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:169)
    at leitor.df.texto.main.afterFirstLayout(main.java:102)
    at leitor.df.texto.main.access$000(main.java:17)
    at leitor.df.texto.main$WaitForLayout.run(main.java:80)
    at android.os.Handler.handleCallback(Handler.java:725)
    at android.os.Handler.dispatchMessage(Handler.java:92)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:5041)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
    at dalvik.system.NativeStart.main(Native Method)

the error ignore the try catch and crash the app.
B4X:
            Try
    'INICIA INTERSTITIAL
        mwAdInterstitial.Initialize("mwadi","ca-app-xxxxxxxxxxxxxxxxxxxxxx")
        mwAdInterstitial.LoadAd
    'FIM
        Catch
        Log("Erro interstitial")
        End Try
 

sorex

Expert
Licensed User
Longtime User
Hello,

While this library seems to work fine I notice some issues here.

After a few ad displays I notice that it seems to be stuck somewhere in the loading.

When I ad logging in all subs I see loading ad, display ad but the "ad loaded" one doesn't happen anymore.

Anyone else noticed this behaviour?
 

sorex

Expert
Licensed User
Longtime User
can it be that an app resume pauses the ad displayer?

I see this in the logs

load ad
ad loaded
display ad?
yes display ad
** Activity (main) Pause, UserClosed = false **
sending message to waiting queue (adi_adclosed)
running waiting messages (1)
** Activity (main) Resume **

ad loaded
load ad
display ad?
yes display ad
load ad
display ad?
yes display ad


after that it starts to fail (without errors tho)
 

sorex

Expert
Licensed User
Longtime User
just got home and did another test.

I could play the game a few times and got 3 or 4 ads.

I didn't touch the phone and the screen went out.

When I then complete a level no ads show up anymore.

Do I need to reinitialize the ad or something when it resumes?
 

sorex

Expert
Licensed User
Longtime User
I seem to have found the culprit.

When I pre-load the ad by using the [play game] button it fails once the app resumed.

When I pre-load the ad during the _AdClosed event it keeps working fine probably because the ad was already loaded before the resume hits.

Something is wrong but it can be avoided by using the method above and that was originally used in the example.
 

migrec

Member
Licensed User
Longtime User
Hi, I always get Network Error, even with erel's admob lib for banner ads.
There is only this in the logs:
B4X:
failed to load ad: Network Error
not much to show really, dont know what it could be.
 

ilan

Expert
Licensed User
Longtime User
Hi, I always get Network Error, even with erel's admob lib for banner ads.
There is only this in the logs:
B4X:
failed to load ad: Network Error
not much to show really, dont know what it could be.

make sure you are not using an ad blocker ;)
 
Top