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:

Rorry

Member
Licensed User
Longtime User
Great. I know already wich one it is. because mwAdmobInterstitial use GoogleAdMobAdsSdk-6.4.1.jar and the admob wrapper for b4a use GoogleAdMobAdsSdk.jar and i have the both in my library folder... to me,mwAdmobInterstitial.jar & admob.jar should call the same GoogleAdMobAdsSdk 6.4.1 jar file (i mean with the same filename) :(
 

FJS

Active Member
Licensed User
Longtime User
Fantastic Erel, Thank you very much. I had the same problem, and thanks to your aswer, I did it!
 

FJS

Active Member
Licensed User
Longtime User
Hello!

It works right, but have a question. If with
"mwAdInterstitial.Show"
the add will show
How Can I not show the add, after 4seconds for example??

thank you in advanced
 
Last edited:

icebold

Member
Licensed User
Longtime User
Hi guys,
compiling the example program for this lib I got the following error:
referenced library is missing: googleadmobadssdk-6.4.1

I finally SOLVED this issue after changing my ADDITIONAL LIBRARIES PATH with a new path that doesn't include SPACES.

This method can also help people ( like me ) that can experience the following error compiling programs on windows 8.x pc:
Path contains illegal characters. Please install Basic4android in a path without spaces.
as discussed here : http://www.b4x.com/android/forum/threads/illegal-path.10866/#post60657

This lib works well, it's a good job.
Thanks again to Jack Cole! ;)
 

holdemadvantage

Active Member
Licensed User
Longtime User
Hi Jack, thanks for your library!
I create a new Admob id forInterstitial and i run your code getting this message: "
Ad request successful, but no ad returned due to lack of ad inventory." is it normal?
---------------
Fixed: i got this message in emulator but it works on device
 
Last edited:

Magma

Expert
Licensed User
Longtime User
Hi there... this is what i am looking for...

1)as i know google ads will stop support the old version admob at august 2014... then what ?

2)Also can not show ads... :-(

errors:


B4X:
Request scenario: Online server request.
Can't get the viewWidth after the first layout
GC_CONCURRENT freed 386K, 50% free 3206K/6343K, external 438K/694K, paused 5ms+5ms


Can't get the viewWidth after the first layout


nativeDestroy view: 0x3179f8


onReceiveAd()


Failed to find provider info for com.google.plus.platform
To get test ads on this device, call adRequest.addTestDevice("F26402E054047E04BDD6F1689866FC4E");
adRequestUrlHtml: <html><head><script src="http://media.admob.com/sdk-core-v40.js"></script><script>blah blah blah....</script></head><body></body></html>


Received ad url: <url: "http://googleads.g.doubleclick.net:80/mads/gma?preqs=13&session_id=blah blah blah...&askip=3&gsb=wi&caps=interactiveVideo_th_autoplay_mediation_sdkAdmobApiForAds_di&jsv=71&urll=907" type: "admob" afmaNotifyDt: "null" activationOverlayUrl: "null" useWebViewLoadUrl: "false">

Thanks in advanced...
 
Last edited:

Magma

Expert
Licensed User
Longtime User
@Erel thought was different posts (mwad interestrial - with admob)... The true is working on project that going to end and need help to 3-5 things more.. (but as you can see are different... sound / ads / interestrial ad etc) - I know and i can understand that members of forum can not only read my problems... sorry and i hope some day i will help other members (that means forum for me).

@Jack Cole --->

1) old version of admob website ? hmmm sorry can;t understand... the admob site i am using is this:

34zkj1y.jpg


You mean go down of all these Countries and sign up again?

Only that ? That cannot be done because i am in Greece and have other countries


after that screen: seems to be new... https://apps.admob.com/#home ....

And the strange is that i can view statistics and seems that ads showing (but never seen it at my device 2.3 android zte racer / ics - never seen it at emulator too) - ofcourse nevered click because i didn;t see it - and gives error at debugger.

a. .....may be needs app to be linked first ?????? - because i didn't upload it yet to google play....????????? is it a problem that ????

2) at august 2014... then what - you library will continue working with the same jar file (6.4.1) - or i must make something else ?
 
Last edited:

Jack Cole

Well-Known Member
Licensed User
Longtime User
I recommend you download the example app in the original post and insert your IDs into the appropriate places in the code. You must be transferred over to apps.admob.com and set up an ad unit for interstitials as discussed earlier in the thread. I will update the library at some point to support the latest SDKs. I'm not seeing any message about something being discontinued in 8/2014. Where are you seeing that?
 

Magma

Expert
Licensed User
Longtime User
at this page:
https://developers.google.com/mobile-ads-sdk/download?hl=el#downloadandroid

Select the tab for your platform: Google Mobile Ads SDK v6.4.1

Deprecated. On August 1, 2014, Google Play will stop accepting new or updated apps that use the old standalone Google Mobile Ads SDK v6.4.1 or lower. You must upgrade to the Google Play version of the Mobile Ads SDK by then.

~~~~~~~~~~~~~~~~~~~~~~

FOUND the error... was two times .Loadad .... not showing.... with ".Show" !


~~~~~~~~~~~~~~~~~~~~~~

Thanks answering to me !
 
Last edited:
Top